0

我正在尝试使用第 15 页文档中的异步模块运行电报客户端

import telethon
from telethon import TelegramClient, events
import asyncio

client = TelegramClient(USERNAME, API_ID, API_HASH)

async def main():
    await client.start()

if __name__ == '__main__':
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

尝试运行时await client.start()出现以下错误。

Task was destroyed but it is pending!
task: <Task pending coro=<MTProtoSender._send_loop() running at 
/home/myuser/.local/lib/python3.5/site-packages/telethon/network/mtprotosender.py:347> wait_for=<Future pending cb=[Task._wakeup()]>>
Task was destroyed but it is pending!
task: <Task pending coro=<MTProtoSender._recv_loop() running at /home/myuser/.local/lib/python3.5/site-packages/telethon/network/mtprotosender.py:411> wait_for=<Future pending cb=[Task._wakeup()]>>
Exception ignored in: <coroutine object MTProtoSender._send_loop at 0x7f27c688b468>
Traceback (most recent call last):
  File "/home/myuser/.local/lib/python3.5/site-packages/telethon/network/mtprotosender.py", line 347, in _send_loop
  File "/home/myuser/.local/lib/python3.5/site-packages/telethon/network/mtprotosender.py", line 720, in get
  File "/usr/lib/python3.5/asyncio/queues.py", line 170, in get
  File "/usr/lib/python3.5/asyncio/futures.py", line 227, in cancel
  File "/usr/lib/python3.5/asyncio/futures.py", line 242, in _schedule_callbacks
  File "/usr/lib/python3.5/asyncio/base_events.py", line 497, in call_soon
  File "/usr/lib/python3.5/asyncio/base_events.py", line 506, in _call_soon
  File "/usr/lib/python3.5/asyncio/base_events.py", line 334, in _check_closed
RuntimeError: Event loop is closed
Task was destroyed but it is pending!
task: <Task pending coro=<UpdateMethods._update_loop() running at /home/myuser/.local/lib/python3.5/site-packages/telethon/client/updates.py:206> wait_for=<Future pending cb=[Task._wakeup()]>>

有没有办法在不进入模块代码的情况下解决这个问题,或者我对 asyncio 做错了什么?

4

1 回答 1

0
api_id = your id
api_hash = ''
bot_token = ''

with TelegramClient('session name', api_id, api_hash).start(bot_token=bot_token) as client:
     client.send_message('id', message)

如果您不使用机器人并使用您的帐户删除启动机器人代码并且也不需要机器人令牌

使用 Telethon 的新闻版本并测试两者(telethon,telethon.sync)

如果它不能帮助使用 celery 并且它为你做我在我的 django 项目和工作中使用它

于 2019-06-22T07:18:07.183 回答