Python Telegram Bot fails to deliver messages - Bot.send_message coroutine awaiting error

My telegram bot stopped working around early May 2023 and I can’t figure out why. The code was working fine before but now it throws an error about coroutines not being awaited.

I’m running this on a Synology NAS with Ubuntu VM and using Spyder through Anaconda. I tried reinstalling everything and creating new environments but nothing worked.

Here’s my code:

import telegram

API_TOKEN = "456"
user_id = "789"

def notify(text, user_id, token=API_TOKEN):
    telegram_bot = telegram.Bot(token=token)
    telegram_bot.sendMessage(chat_id=user_id, text=text)

test_message = "Test notification"
notify(test_message, user_id)

The error I get is:

RuntimeWarning: coroutine 'Bot.send_message' was never awaited
telegram_bot.sendMessage(chat_id=user_id, text=text)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

I tried using async/await but then I get a different error about event loops already running. Has anyone encountered this issue? What’s the proper way to fix this without breaking the message formatting?

This broke my production bot too around the same timeframe. The library switched to async-only architecture in v20 which explains the coroutine warnings. I ended up using nest_asyncio to handle the event loop conflicts instead of downgrading. Install it with pip install nest_asyncio then add these lines at the top of your script:

import nest_asyncio
nest_asyncio.apply()

Then wrap your function call in asyncio.run(). This approach lets you keep the newer telegram bot version while avoiding the event loop already running errors you mentioned. Been using this solution for months without issues on my server setup.

had same exact issue on my ubuntu setup! try pip uninstall python-telegram-bot then pip install python-telegram-bot==13.7 instead. worked for me when the newer versions kept giving coroutine errors. the async stuff in v20+ is kinda messy if you dont need it

The issue you’re experiencing is due to a major version update in the python-telegram-bot library. Around April-May 2023, version 20.x introduced breaking changes that made the library fully asynchronous. Your old synchronous code won’t work anymore with the newer version.

I faced the exact same problem when my bot suddenly stopped working after an automatic update. The quickest solution is to downgrade to version 13.15 which still supports synchronous operations. Run pip install python-telegram-bot==13.15 and your existing code should work again.

If you want to stick with the newer version, you’ll need to rewrite your function to be fully async, but based on your description about event loop errors, the downgrade approach will save you significant debugging time. The v13.15 is still stable and receives security updates.