Hey everyone! I’m having trouble with my Telegram bot. It was working fine on my computer with Python 3.12, but now I’m trying to host it online and I’m running into issues. Here’s what’s happening:
import asyncio
from telebot.async_telebot import AsyncTeleBot
my_bot = AsyncTeleBot('BOT_TOKEN_HERE')
# Other bot setup code...
asyncio.run(my_bot.start_polling())
When I try to run this, I get a warning saying the coroutine was never awaited, and then an error about asyncio.run() not being able to run in an existing event loop. I think it might be because the hosting service is using an older Python version, but I’m not sure how to fix it. Has anyone run into this before? Any ideas on how to make it work on the hosting platform? Thanks!
Encountering event loop issues on hosting platforms can be quite challenging. I experienced a similar problem when my bot worked perfectly in a local environment but failed online. In my case, the hosting service was using an older version of Python, which did not support asyncio.run() properly. To resolve the issue, I switched to managing the event loop manually by using get_event_loop and run_until_complete. This approach proved to be more compatible with the version on the server. Testing your environment and aligning the Python version can make a significant difference.
I’ve encountered this issue before when deploying bots on various hosting platforms. The root cause is often a mismatch between your local Python environment and the one on the hosting service. To resolve this, you might need to adjust your code to be compatible with older Python versions. Here’s a potential solution:
import asyncio
from telebot.async_telebot import AsyncTeleBot
my_bot = AsyncTeleBot('BOT_TOKEN_HERE')
# Other bot setup code...
loop = asyncio.get_event_loop()
loop.run_until_complete(my_bot.polling())
This approach should work on most hosting services, even with older Python versions. It explicitly creates and manages the event loop, which can bypass the issues you’re experiencing with asyncio.run(). Remember to check your hosting service’s Python version and adjust your code accordingly.