Skip to content

Set up Telegram webhook receiver

This document walks you through how to set up Telegram as an input source for your live blog and set a webhook connection to receive new updates.

Create a Telegram bot

First, you will need to create a Telegram bot. After creating a bot, an authorization token will be genrated for your new bot.

The token is a string along the lines of 110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw that is required to authorize the bot and send requests to the Bot API. Keep your token secure and store it safely, it can be used by anyone to control your bot.

Add your bot token to settings

Add the following in your settings.base file:

TELEGRAM_BOT_TOKEN = "your-bot-token"

Receiving Telegram events

In order to receive events like when a message is sent in a channel, you need to hook a Webhook URL that Telegram uses to send HTTP POST requests corresponding to events you specify. This URL needs to be publicly accessible.

Configure TELEGRAM_WEBHOOK_URL

In the development phase, you will need a development proxy that creates a public URL and tunnels requests to your local server when you run python3 manage.py runserver in order to receive events from Telegram.

If you are comfortable with ngrok, start ngrok on port 8000. Otherwise you can see how to Set up a local web server with ngrok.

Make sure ngrok is running on port 8000 and add the URL generated by ngrok to your settings file:

```python
    TELEGRAM_WEBHOOK_URL = "https://ngrok-generated-url.com"
```

Configure TelegramWebhookReceiver

  • Add the following in your urls.py file if not done yet:

        from wagtail_live import urls as live_urls
    
        urlpatterns += [
            path('wagtail_live/', include(live_urls)),
        ]
    

  • Add the following in your settings file:

        WAGTAIL_LIVE_RECEIVER = "wagtail_live.receivers.telegram.TelegramWebhookReceiver"
    

That's all we need to receive events from Telegram. Run the following:

    $ python3 manage.py runserver

If everything went well, the server should start without errors.

Channel configuration

We can send messages from the bot's channel or invite the bot in another channel. For this tutorial, we will post to the bot's channel directly. You can find the link to your bot's channel in BotFather where you created the bot.

Retrieve chat ID

In the chat where you want to post messages, type the following: /get_chat_id. Your bot will answer with the ID of the current chat.

You can then create a live page and add the chat ID to the channel_id field of that page. From then, all messages added or edited in the chat will be synced with your live page!

Note

Telegram doesn't send updates when a message is deleted. This means that when you delete a message in a Telegram chat it won't be automatically deleted from your LivePage.