Spaces:
Runtime error
Runtime error
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() | |
) | |
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) | |
def read_general(): | |
return {"response": "Started"} | |
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)) |