Spaces:
Runtime error
Runtime error
File size: 1,381 Bytes
d9d21f7 2cd7197 d9d21f7 bda88a2 d9d21f7 bda88a2 d9d21f7 |
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 |
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
ptb = (
Application.builder()
.updater(None)
.token('6770617809:AAEhytQUOl3uZOFINVE7-o0KkIoAz8perGU') # replace <your-bot-token>
.read_timeout(7)
.get_updates_read_timeout(42)
.build()
)
@asynccontextmanager
async def lifespan(_: FastAPI):
await ptb.bot.setWebhook('https://matteoscript-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)) |