Search code examples
pythontelethonaiogram

Connecting aiogram and telethon


I am writing a telegram bot that needs to send some messages using the telethon client and some using aiogram. He makes a mistake that I don't understand

I ve tried this code:

bot = Bot(token=bot_token)
dp = Dispatcher()
client = TelegramClient("session", api_id, api_hash).start(bot_token=bot_token)


@dp.message(CommandStart())
async def start(message: types.Message):
    await message.answer("text aiogram")
    await client_send(message)


async def client_send(message: types.Message):
    async with client:
        await client.send_message(message.chat.id, "text telethon")


async def main():
    await dp.start_polling(bot)

if __name__ == '__main__':
    asyncio.run(main())

when the bot receives a message, I get this long traceback:

Cause exception while process update id=xxx by bot id=xxx
RuntimeError: The asyncio event loop must not change after connection (see the FAQ for details)
Traceback (most recent call last):
  File "C:\Users\Artyxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\aiogram\dispatcher\dispatcher.py", line 309, in _process_update
    response = await self.feed_update(bot, update, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Artyxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\aiogram\dispatcher\dispatcher.py", line 158, in feed_update
    response = await self.update.wrap_outer_middleware(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...

  File "C:\Users\Artyxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\aiogram\dispatcher\event\handler.py", line 43, in call
    return await wrapped()
           ^^^^^^^^^^^^^^^
  File "C:\Users\Artyxx\Documents\Python\bot\bot-check-in\testcode.py", line 19, in start
    await client_send(message)
  File "C:\Users\Artyxx\Documents\Python\bot\bot-check-in\testcode.py", line 23, in client_send
    async with client:
  File "C:\Users\Artyxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\telethon\client\auth.py", line 657, in __aenter__
    return await self.start()
           ^^^^^^^^^^^^^^^^^^
  File "C:\Users\Artyxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\telethon\client\auth.py", line 141, in _start
    me = await self.get_me()
         ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Artyxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\telethon\client\users.py", line 165, in get_me
    me = (await self(
          ^^^^^^^^^^^
  File "C:\Users\Artyxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\telethon\client\users.py", line 30, in __call__
    return await self._call(self._sender, request, ordered=ordered)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Artyxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\telethon\client\users.py", line 34, in _call
    raise RuntimeError('The asyncio event loop must not change after connection (see the FAQ for details)')
RuntimeError: The asyncio event loop must not change after connection (see the FAQ for details)

How can i connect and use this 2 libraries


Solution

  • asyncio.run creates a new event loop. After you establish a connection (which you're doing with Telethon's .start()), you can't change the loop (the .start() line is using the default loop, but asyncio.run creates a new one).

    Remove .start(). When you use async with client, that is also calling .start(), so the first one is redundant and causing problems.

    Also note that Telethon is capable of controlling bot accounts too. If you need both user and bot, using a single library might be simpler.