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
Project ID:
N/A
N/A
have you read the deployment logs? doesnt really look like an issue caused by railway, looks more so like a code issue
It works locally tho. Must be an issue with HTTP/ports/webhooks/etc.
working locally does not rule out a code issue
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())where does
PORT
come fromits an env variable, set at 80
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…
show me the url you tell telegram to call
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
whats your start command
python bot/main2.py
build logs please https://bookmarklets.up.railway.app/log-downloader/
"web: python bot/main2.py" in my profile
please read this page https://docs.railway.app/troubleshoot/fixing-common-errors
Dude thank u! Had to listen on 0.0.0.0
I don't what what the first error was though
but glad this error was solved