连接 aiogram 和 telethon

问题描述 投票:0回答:1

我正在编写一个电报机器人,需要使用 telethon 客户端发送一些消息,并使用 aiogram 发送一些消息。他犯了一个我不明白的错误

我试过这段代码:

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())

当机器人收到消息时,我会得到这么长的回溯:

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)

我如何连接和使用这两个库

python python-telegram-bot telethon aiogram
1个回答
0
投票

asyncio.run
创建一个新的事件循环。建立连接后(使用 Telethon 的
.start()
进行连接),您无法更改循环(
.start()
线使用默认循环,但
asyncio.run
创建一个新循环)。

删除

.start()
。当您使用
async with client
时,也会调用
.start()
,因此第一个是多余的并会导致问题。

另请注意,Telethon 也能够控制机器人帐户。如果您同时需要用户和机器人,使用单个库可能会更简单。

© www.soinside.com 2019 - 2024. All rights reserved.