Async function warning in Python Telegram bot - send_message not awaited

I’m having trouble with my Telegram bot. It used to work fine but now it stopped sending messages completely. When I run my code, I keep getting this warning message: “RuntimeWarning: coroutine ‘Bot.send_message’ was never awaited”.

I’m not sure what this means or how to fix it. The bot connects fine but the messages just don’t get sent. Here’s my current code:

from telegram import Bot

api_key = 'my_bot_token_here'
user_id = 'target_chat_id'
text_content = 'Hello World'

telegram_bot = Bot(token=api_key)

telegram_bot.send_message(chat_id=user_id, text=text_content)

Can someone help me understand what’s wrong and how to make it work again?

This happens because the telegram library switched to async operations. You can skip asyncio.run() and event loops by using the Application class - it handles the async stuff automatically. Here’s how:

from telegram.ext import Application

async def main():
    application = Application.builder().token('my_bot_token_here').build()
    await application.bot.send_message(chat_id='target_chat_id', text='Hello World')
    await application.shutdown()

import asyncio
asyncio.run(main())

This approach works great if you’re planning to add handlers later. The Application class manages everything properly and prevents resource leaks.

i had a similar prob! after recent changes, send_message def needs await. if u wanna keep it simple, u can use asyncio.get_event_loop().run_until_complete(telegram_bot.send_message(chat_id=user_id, text=text_content)). works for quick fixes!

You’re encountering a common asyncio issue with python-telegram-bot. The newer versions made send_message return a coroutine that needs to be awaited. Right now, you’re creating the coroutine but never actually executing it. To fix this, wrap your code in an async function and await the call:

import asyncio
from telegram import Bot

async def send_telegram_message():
    api_key = 'my_bot_token_here'
    user_id = 'target_chat_id'
    text_content = 'Hello World'
    
    telegram_bot = Bot(token=api_key)
    await telegram_bot.send_message(chat_id=user_id, text=text_content)

asyncio.run(send_telegram_message())

The library switched to async methods for better performance in recent updates.