|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +Asynchronous Telegram Echo Bot example. |
| 4 | +
|
| 5 | +This is a simple bot that echoes each message that is received onto the chat. |
| 6 | +It uses the Starlette ASGI framework to receive updates via webhook requests. |
| 7 | +""" |
| 8 | + |
| 9 | +import uvicorn |
| 10 | +from starlette.applications import Starlette |
| 11 | +from starlette.requests import Request |
| 12 | +from starlette.responses import PlainTextResponse, Response |
| 13 | +from starlette.routing import Route |
| 14 | +from telebot.async_telebot import AsyncTeleBot |
| 15 | +from telebot.types import Message, Update |
| 16 | + |
| 17 | +API_TOKEN = "TOKEN" |
| 18 | + |
| 19 | +WEBHOOK_HOST = "<ip/domain>" |
| 20 | +WEBHOOK_PORT = 8443 # 443, 80, 88 or 8443 (port need to be 'open') |
| 21 | +WEBHOOK_LISTEN = "0.0.0.0" |
| 22 | +WEBHOOK_URL = f"https://{WEBHOOK_HOST}:{WEBHOOK_PORT}/telegram" |
| 23 | +WEBHOOK_SECRET_TOKEN = "SECRET_TOKEN" |
| 24 | + |
| 25 | +logger = telebot.logger |
| 26 | +telebot.logger.setLevel(logging.INFO) |
| 27 | + |
| 28 | +bot = AsyncTeleBot(token=API_TOKEN) |
| 29 | + |
| 30 | +# BOT HANDLERS |
| 31 | +@bot.message_handler(commands=["help", "start"]) |
| 32 | +async def send_welcome(message: Message): |
| 33 | + """ |
| 34 | + Handle '/start' and '/help' |
| 35 | + """ |
| 36 | + await bot.reply_to( |
| 37 | + message, |
| 38 | + ("Hi there, I am EchoBot.\n" "I am here to echo your kind words back to you."), |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | +@bot.message_handler(func=lambda _: True, content_types=["text"]) |
| 43 | +async def echo_message(message: Message): |
| 44 | + """ |
| 45 | + Handle all other messages |
| 46 | + """ |
| 47 | + await bot.reply_to(message, message.text) |
| 48 | + |
| 49 | + |
| 50 | +# WEBSERVER HANDLERS |
| 51 | +async def telegram(request: Request) -> Response: |
| 52 | + """Handle incoming Telegram updates.""" |
| 53 | + token_header_name = "X-Telegram-Bot-Api-Secret-Token" |
| 54 | + if request.headers.get(token_header_name) != WEBHOOK_SECRET_TOKEN: |
| 55 | + return PlainTextResponse("Forbidden", status_code=403) |
| 56 | + await bot.process_new_updates([Update.de_json(await request.json())]) |
| 57 | + return Response() |
| 58 | + |
| 59 | + |
| 60 | +async def startup() -> None: |
| 61 | + """Register webhook for telegram updates.""" |
| 62 | + webhook_info = await bot.get_webhook_info(30) |
| 63 | + if WEBHOOK_URL != webhook_info.url: |
| 64 | + logger.debug( |
| 65 | + f"updating webhook url, old: {webhook_info.url}, new: {WEBHOOK_URL}" |
| 66 | + ) |
| 67 | + if not await bot.set_webhook( |
| 68 | + url=WEBHOOK_URL, secret_token=WEBHOOK_SECRET_TOKEN |
| 69 | + ): |
| 70 | + raise RuntimeError("unable to set webhook") |
| 71 | + |
| 72 | + |
| 73 | +app = Starlette( |
| 74 | + routes=[ |
| 75 | + Route("/telegram", telegram, methods=["POST"]), |
| 76 | + ], |
| 77 | + on_startup=[startup], |
| 78 | +) |
| 79 | + |
| 80 | + |
| 81 | +uvicorn.run( |
| 82 | + app, |
| 83 | + host=WEBHOOK_HOST, |
| 84 | + port=WEBHOOK_LISTEN, |
| 85 | +) |
0 commit comments