# Configuring and Testing Telegram Bot Webhooks with Pinggy

Responsive Telegram bots rely on receiving updates promptly. Although long polling works during early development, webhooks are preferred for their efficiency and real-time delivery—making them ideal for production environments.

This guide outlines the process for setting up and testing Telegram bot webhooks using [**Pinggy**](https://pinggy.io/), a command-line utility that makes your local server accessible via a secure public URL—without requiring external deployment.

## Overview: Telegram Bot Webhooks

Telegram bots can receive updates in two main ways:

* **Long Polling**: The bot regularly queries Telegram servers for new updates.
    
* **Webhooks**: Telegram pushes updates directly to a defined server endpoint.
    

Advantages of using webhooks:

* Immediate delivery of user updates
    
* Less server and bandwidth usage
    
* Easier scaling under load
    

Since Telegram requires a publicly accessible HTTPS endpoint for webhooks, development can be challenging. [Pinggy](https://pinggy.io/) helps address this by exposing your local server.

## Step 1: Create a Telegram Bot

1. Open Telegram and search for `@BotFather`.
    
    ![Bot Father Chat](https://pinggy.io/images/how_to_set_up_and_test_telegram_bot_webhook/start_chat_with_god_bot.webp align="left")
    
2. Start a chat and use the `/newbot` command.
    
    ![newbot](https://pinggy.io/images/how_to_set_up_and_test_telegram_bot_webhook/newbot_chat.webp align="left")
    
3. Set a name and username as prompted.
    
    ![name](https://pinggy.io/images/how_to_set_up_and_test_telegram_bot_webhook/bot_name.webp align="left")
    
4. BotFather provides a token in the format `123456789:ABCDefGhIJKlmNoPQRsTUVwxyZ`.
    
    ![bot token](https://pinggy.io/images/how_to_set_up_and_test_telegram_bot_webhook/bot_token.png align="left")
    

Save this token securely; it grants full control over your bot.

## Step 2: Set Up a Local Webhook Server

Use Flask with Python to create a simple server to handle incoming updates.

```python
from flask import Flask, request, jsonify
import logging

app = Flask(__name__)
logging.basicConfig(level=logging.INFO)

@app.route('/webhook', methods=['POST'])
def webhook():
    update = request.get_json()
    logging.info(f"Update received: {update}")

    if 'message' in update:
        chat_id = update['message']['chat']['id']
        text = update['message'].get('text', '')
        logging.info(f"Message from {chat_id}: {text}")

    return jsonify({'status': 'ok'})

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000)
```

Install Flask:

```bash
pip install flask
```

Run the server:

```bash
python telegram_webhook.py
```

This sets up a local server at `http://localhost:8000/webhook`.

![python code](https://pinggy.io/images/how_to_set_up_and_test_telegram_bot_webhook/python_code_is_running.webp align="left")

## Step 3: Make Your Server Public with Pinggy

Telegram needs to reach your webhook endpoint. Use [Pinggy](https://pinggy.io/) to expose your local server:

```bash
ssh -p 443 -R0:localhost:8000 qr@a.pinggy.io
```

![pinggy command](https://pinggy.io/images/how_to_set_up_and_test_telegram_bot_webhook/pinggy_tunnel_command.webp align="left")

[Pinggy](https://pinggy.io/) provides a temporary public HTTPS URL such as:

```bash
https://abcdefghij.a.pinggy.link
```

![pinggy publiv url](https://pinggy.io/images/how_to_set_up_and_test_telegram_bot_webhook/pinggy_public_url.webp align="left")

## Step 4: Register the Webhook with Telegram

Use the following command to register your webhook:

```bash
curl -X POST "https://api.telegram.org/bot<YOUR_BOT_TOKEN>/setWebhook" \
     -d "url=https://abcdefghij.a.pinggy.link/webhook"
```

Replace `<YOUR_BOT_TOKEN>` and the URL with your actual values. A successful response confirms the webhook is active.

![postman api call](https://pinggy.io/images/how_to_set_up_and_test_telegram_bot_webhook/postman_call.png align="left")

## Step 5: Test Your Webhook Setup

1. Send a message to your bot on Telegram.
    
2. Check your server logs.
    
3. You should see an update payload indicating that Telegram successfully delivered the message.
    

Sample output:

```bash
INFO:root:Update received: {...}
INFO:root:Message from 123456789: Hello
```

![chat with new pinggy bot](https://pinggy.io/images/how_to_set_up_and_test_telegram_bot_webhook/chat_with_pinggy_bot.webp align="left")

![webhook logs](https://pinggy.io/images/how_to_set_up_and_test_telegram_bot_webhook/webhook_logs.webp align="left")

## Optional: Sending Replies from the Server

To send a response from the bot, use the following helper function:

```python
import requests

def send_message(chat_id, text, token):
    url = f"https://api.telegram.org/bot{token}/sendMessage"
    payload = {"chat_id": chat_id, "text": text}
    response = requests.post(url, json=payload)
    return response.json()
```

Integrate this function into your webhook logic to respond dynamically.

## Conclusion

Webhook-based communication allows Telegram bots to receive and process messages efficiently and in real time. During development, tools like [Pinggy](https://pinggy.io/) enable you to test these interactions using a temporary public endpoint, removing the need for external hosting.

This setup facilitates faster development cycles, better debugging, and a smooth transition to production deployments.

### References

1. [Telegram Bot API documentation](https://core.telegram.org/bots/api).
    
2. [Pinggy's Official Website](https://pinggy.io/)
    
3. [How to Set Up and Test Telegram Bot Webhook](https://pinggy.io/blog/how_to_set_up_and_test_telegram_bot_webhook/)
