Home

Awesome

Telegram Bot Updates Receiver Service

The objective of this project is to build a microservice that receives updates from a Telegram Bot through Webhook, and publishes them on a message broker or queue system (like MQTT, AMQP, Kafka or Redis), to be consumed by one or more processing microservices.

This project is currently a Proof of Concept.

Architecture

A very basic architecture would involve three services:

Architecture diagram

Why

Advantages

Disadvantages

Getting started

Webhook

The following steps will start running the service out of the box, using Docker and ngrok:

  1. You must own a Telegram bot. If not, create it from BotFather. You will need the bot token.
  2. Download ngrok, and start running it as following:
    ./ngrok http 8025
    
  3. Copy the sample.env file as .env, and complete the following settings
    • TELEGRAM_TOKEN with you bot token
    • WEBHOOK_DOMAIN with the domain that ngrok is currently using, including https:// (notice that ngrok free plan will change the domain periodically)
  4. Start running the webhook receiver service:
    docker run --rm -it -p 8025:8025 -e GIT_REPOSITORY="https://github.com/David-Lor/TelegramBot-Webhook-Updates-Receiver-Service" --env-file=".env" davidlor/python-git-app:slim
    
  5. Send something to your bot. You should see some output on the container

Example: pytelegrambotapi + redis (webhook)

A Telegram Bot backend example is available here. It can be deployed with the following commands:

# Start ngrok
./ngrok http 8025

# Add the REDIS_URL & REDIS_QUEUE_NAME settings (for both server & client) - Ensure you already have copied and modified a .env file, including the webhook endpoint with the current ngrok domain!
echo "REDIS_URL=redis://telegrambot-redis:6379" >> .env
echo "REDIS_QUEUE_NAME=TelegramBotQueue" >> .env

# Create a docker network for the services
docker network create telegrambot-net

# Start the Redis server
docker run -d --name=telegrambot-redis --network=telegrambot-net redis

# Start the webhook server (receive updates through webhook, enqueue on Redis)
docker run -d --name=telegrambot-receiver -p 8025:8025 --net=telegrambot-net -e GIT_REPOSITORY="https://github.com/David-Lor/TelegramBot-Webhook-Updates-Receiver-Service" --env-file=".env" davidlor/python-git-app:slim

# Start the Telegram bot backend (read updates from Redis queue, process them)
docker run -d --name=telegrambot-backend --net=telegrambot-net -e GIT_REPOSITORY="https://github.com/David-Lor/TelegramBot-Webhook-Updates-Receiver-Service" -e GIT_BRANCH="example/pytelegrambotapi+redis" --env-file=".env" davidlor/python-git-app:slim

Alternatively, you can run the self-hosted Telegram Bot API with the --local argument, to avoid dealing with SSL certificates.

Polling

The following steps will start running the service out of the box, using Docker and fetching the updates using the long-polling method, instead of setting up a webhook:

  1. You must own a Telegram bot. If not, create it from BotFather. You will need the bot token.
  2. Copy the sample.env file as .env, and complete the following settings
    • TELEGRAM_TOKEN with you bot token
  3. Start running the receiver service:
    docker run --rm -it -e GIT_REPOSITORY="https://github.com/David-Lor/TelegramBot-Webhook-Updates-Receiver-Service" --env-file=".env" davidlor/python-git-app:slim
    
  4. Send something to your bot. You should see some output on the container

Example: pytelegrambotapi + redis (polling)

# Add the REDIS_URL & REDIS_QUEUE_NAME settings (for both server & client) - Ensure you already have copied and modified a .env file!
echo "REDIS_URL=redis://telegrambot-redis:6379" >> .env
echo "REDIS_QUEUE_NAME=TelegramBotQueue" >> .env

# Create a docker network for the services
docker network create telegrambot-net

# Start the Redis server
docker run -d --name=telegrambot-redis --network=telegrambot-net redis

# Start the receiver service (receive updates through long-polling, enqueue on Redis)
docker run -d --name=telegrambot-receiver --net=telegrambot-net -e GIT_REPOSITORY="https://github.com/David-Lor/TelegramBot-Webhook-Updates-Receiver-Service" --env-file=".env" davidlor/python-git-app:slim

# Start the Telegram bot backend (read updates from Redis queue, process them)
docker run -d --name=telegrambot-backend --net=telegrambot-net -e GIT_REPOSITORY="https://github.com/David-Lor/TelegramBot-Webhook-Updates-Receiver-Service" -e GIT_BRANCH="example/pytelegrambotapi+redis" --env-file=".env" davidlor/python-git-app:slim

Settings

Settings are defined using environment variables, or a .env file. Variables defined as environment variables will override those defined in the .env file.

Upcoming features...

Changelog

Versions prior 1.0.0 are considered experimental and breaking changes may occur on MINOR versions (0.x)