-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_example.py
50 lines (43 loc) · 1.46 KB
/
simple_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from contextlib import asynccontextmanager
from http import HTTPStatus
from telegram import Update
from telegram.ext import Application, CommandHandler
from telegram.ext._contexttypes import ContextTypes
from fastapi import FastAPI, Request, Response
from dotenv import load_dotenv
import os
# Load environment variables from .env file
load_dotenv()
BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
WEBHOOK_URL = os.getenv("WEBHOOK_URL") # Your webhook URL
# Initialize python telegram bot
ptb = (
Application.builder()
.updater(None)
.token(BOT_TOKEN) # replace <your-bot-token>
.read_timeout(7)
.get_updates_read_timeout(42)
.build()
)
@asynccontextmanager
async def lifespan(_: FastAPI):
await ptb.bot.setWebhook(f"{WEBHOOK_URL}/webhook") # replace <your-webhook-url>
async with ptb:
await ptb.start()
yield
await ptb.stop()
# Initialize FastAPI app (similar to Flask)
app = FastAPI(lifespan=lifespan)
@app.post("/webhook")
async def process_update(request: Request):
req = await request.json()
print(req)
update = Update.de_json(req, ptb.bot)
await ptb.process_update(update)
return Response(status_code=HTTPStatus.OK)
# Example handler
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Send a message when the command /start is issued."""
await update.message.reply_text("starting...")
# Add the command handler to the application
ptb.add_handler(CommandHandler("start", start))