Billy
Python Telegram Bot Webhooks Error
2023-10-25 21:23:05,028 - telegram.ext.Application - INFO - Application started
INFO: Started server process [1]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:80 (Press CTRL+C to quit)
28 replies
Python Telegram Bot Webhooks Error
webserver = uvicorn.Server(
config=uvicorn.Config(
app=starlette_app,
port=PORT,
use_colors=False,
log_level='info'
#host="0.0.0.0",
)
)
# Run application and webserver together
async with application:
await application.start()
await webserver.serve()
await application.stop()
if name == "main":
asyncio.run(init_db())
asyncio.run(main())
28 replies
Python Telegram Bot Webhooks Error
async def main() -> None:
"""Set up PTB application and a web application for handling the incoming requests."""
context_types = ContextTypes(context=CustomContext)
# Here we set updater to None because we want our custom webhook server to handle the updates
# and hence we don't need an Updater instance
application = (
Application.builder().token(TOKEN).updater(None).context_types(context_types).build()
)
# register handlers
application.add_handler(CommandHandler("start", start))
application.add_handler(CommandHandler("add", add))
application.add_handler(TypeHandler(type=WebhookUpdate, callback=webhook_update))
# Pass webhook settings to telegram
await application.bot.set_webhook(url=f"{URL}/telegram", allowed_updates=Update.ALL_TYPES)
# Set up webserver
async def telegram(request: Request) -> Response:
"""Handle incoming Telegram updates by putting them into the
update_queue
"""
await application.update_queue.put(
Update.de_json(data=await request.json(), bot=application.bot)
)
return Response()
async def custom_updates(request: Request) -> PlainTextResponse:
data = await request.json()
await application.updatequeue.put(WebhookUpdate(data=data))
return PlainTextResponse("Thank you for the submission! It's being forwarded.")
async def health(: Request) -> PlainTextResponse:
"""For the health endpoint, reply with a simple plain text message."""
return PlainTextResponse(content="The bot is still running fine :)")
starlette_app = Starlette(
routes=[
Route("/telegram", telegram, methods=["POST"]),
Route("/healthcheck", health, methods=["GET"]),
Route("/helius", custom_updates, methods=["POST", "GET"]),
]
)28 replies