My Telegram bot has stopped functioning. I run into this warning that says: “RuntimeWarning: coroutine ‘Bot.send_message’ was never awaited” whenever I try to send a message. It used to work well, but now nothing gets sent.
I encountered a similar issue after updating the telegram library. The newer versions of python-telegram-bot default to async/await, which changes how you call functions. If you call an async function in a synchronous way, you’ll see the RuntimeWarning indicating that it wasn’t awaited. A straightforward fix is to wrap your telegram_bot.send_message in an async function and invoke it with asyncio.run(). Alternatively, you can revert to using synchronous methods available in the Bot class to avoid this problem altogether. Refactoring my bot was necessary, but the improved performance was definitely worth the effort.
You’re getting this warning because you’re mixing sync and async code. The send_message method returns a coroutine that needs to be awaited or run through an event loop. Since you’re not in an async context, the coroutine never executes. I hit this same issue when my production bot broke after a dependency update. Quick fix: use asyncio.run() to execute the coroutine: asyncio.run(telegram_bot.send_message(chat_id=chat_id, text=message_text)). Don’t forget to import asyncio. This works great for simple scripts where you just need to send messages without building a full async app.
Same thing happened to me after upgrading. The Telegram library switched to async but you’re calling it like it’s synchronous. Try asyncio.get_event_loop().run_until_complete() if asyncio.run() doesn’t work on older Python versions. Fixed my bot when nothing else worked.