1

I wrote a telegram bot in python using the telepot library. The bot itself is working. Since I also have some other stuff I need to do periodically while running the bot (namely writing stuff in logs), I want the bot to run in its own thread.

The telegram bot's code is a file called telebot.py while the threading code is in one called plantbot.py

I first tried simply creating a new thread that calls my run_bot() function, which didn't work and gave me the following RuntimeError:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.5/threading.py", line 862, in run
    self._target(*self._args, **self._kwargs)
  File "plantbot.py", line 12, in start_bot
    telebot.run_bot()
  File "/srv/plantbot/telebot.py", line 108, in run_bot
    loop = asyncio.get_event_loop()
  File "/usr/lib/python3.5/asyncio/events.py", line 671, in get_event_loop
    return get_event_loop_policy().get_event_loop()
  File "/usr/lib/python3.5/asyncio/events.py", line 583, in get_event_loop
    % threading.current_thread().name)
RuntimeError: There is no current event loop in thread 'Thread-1'.

Here is the code I used for that:

plantbot.py

def start_bot():
    telebot.run_bot()

bot = threading.Thread(target=start_bot, daemon=True)
bot.start()
print("Bot started...")
bot.join()

telebot.py

[lots of unrelated code]

bot = telepot.aio.Bot(TOKEN)

def run_bot():
    loop = asyncio.get_event_loop()
    loop.create_task(MessageLoop(bot, {'chat': on_chat_message, 'callback_query': on_callback_query}).run_forever())

    print('Listening ...')
    loop.run_forever()

I then tried around a little with the event loops until I ended up creating the event loop in my main file (plantbot.py) which looked like this:

plantbot.py

def start_bot(loop):
    telebot.run_bot(loop)

loop = asyncio.new_event_loop()
bot = threading.Thread(target=start_bot, args=(loop,),  daemon=True)
bot.start()
print("Bot started...")
bot.join()

telebot.py

[...]
bot = telepot.aio.Bot(TOKEN)

def run_bot(loop):
    loop.create_task(MessageLoop(bot, {'chat': on_chat_message, 'callback_query': on_callback_query}).run_forever())

    print('Listening ...')
    loop.run_forever()

I also tried another variation:

plantbot.py

def start_bot(loop):
    asyncio.set_event_loop(loop)
    loop.create_task(telebot.task.run_forever())
    loop.run_forever()

loop = asyncio.new_event_loop()
bot = threading.Thread(target=start_bot, args=(loop,),  daemon=True)
bot.start()
print("Bot started...")
bot.join()

telebot.py

[...]
bot = telepot.aio.Bot(TOKEN)

task = MessageLoop(bot, {'chat': on_chat_message, 'callback_query': on_callback_query})

Unfortunately, none of these worked.

My question is thus, how do I properly run an event loop in its own designated thread? Specifically: How do I run a telegram bot in its own thread?

pppery
  • 3,731
  • 22
  • 33
  • 46
Lithimlin
  • 542
  • 1
  • 6
  • 24

1 Answers1

0

I did end up finding a solution, though it's not a satisfying one:

I am now running the bot in the main thread and put everything else into other threads. It does work that way around.

Lithimlin
  • 542
  • 1
  • 6
  • 24