R
Railway14mo ago
Billy

Python Telegram Bot Webhooks Error

Hey, I'm deploying a telegram bot (python-telegram-bot library) with a server/webhooks. It works locally, but not when deployed to railway. I'm getting an error on this line it seems: await application.bot.set_webhook(url=f"{URL}/telegram", allowed_updates=Update.ALL_TYPES) Logs attached below.
20 Replies
Percy
Percy14mo ago
Project ID: N/A
Billy
BillyOP14mo ago
N/A
Brody
Brody14mo ago
have you read the deployment logs? doesnt really look like an issue caused by railway, looks more so like a code issue
Billy
BillyOP14mo ago
It works locally tho. Must be an issue with HTTP/ports/webhooks/etc.
Brody
Brody14mo ago
working locally does not rule out a code issue
Billy
BillyOP14mo ago
I see. Well ive tried a bunch of stuff and cant find any updated examples on the internet for hosting a python telegram bot on railway. Posting my server setup code here if anyone has ideas. 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"]), ] ) 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())
Brody
Brody14mo ago
where does PORT come from
Billy
BillyOP14mo ago
its an env variable, set at 80
Billy
BillyOP14mo ago
Marvin's Marvellous Guide to All Things Webhook
We currently support two ways of processing bot updates, getUpdates and setWebhook. getUpdates is a pull mechanism, setwebhook…
Brody
Brody14mo ago
show me the url you tell telegram to call
Billy
BillyOP14mo ago
https:// vizion-production.up.railway.app 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) weird cuz this is my logs now says its running, but when i go to my railway URL its down
Brody
Brody14mo ago
whats your start command
Billy
BillyOP14mo ago
python bot/main2.py
Billy
BillyOP14mo ago
"web: python bot/main2.py" in my profile
Billy
BillyOP14mo ago
Dude thank u! Had to listen on 0.0.0.0
Brody
Brody14mo ago
I don't what what the first error was though but glad this error was solved
Want results from more Discord servers?
Add your server