TelegramBOT / main.py
Manishx's picture
Update main.py
9b2bff5 verified
raw
history blame
1.33 kB
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
# Initialize python telegram bot
bot_token = '7163529766:AAGGGlSjVqtcm_b0vJJu7sU9Y7VzSzeYNZg' # replace <your-bot-token>
ptb = Application(bot_token=bot_token, read_timeout=7, get_updates_read_timeout=42)
@asynccontextmanager
async def lifespan(_: FastAPI):
await ptb.bot.setWebhook('https://manishx-telegrambot.hf.space') # 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.get("/")
def read_general():
return {"response": "Started"}
@app.post("/")
async def process_update(request: Request):
print('entrato')
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, _: ContextTypes.DEFAULT_TYPE):
"""Send a message when the command /start is issued."""
await update.message.reply_text("starting...")
ptb.add_handler(CommandHandler("start", start))