测试Telethon的示例代码时出现错误“任务已销毁,但它处于挂起状态!”

2024-04-23 14:59:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用Telethon 1.19.1和Telethon文档中的示例代码,100%副本用于学习。 但是当我开始运行时,错误会弹出。 我尝试导入asyncio库,但无效。 你能帮我一下吗。 谢谢

这是我的代码

from telethon.sync import TelegramClient

api_id = xxxxxxx
api_hash = 'xxxxxxxxxxxxxxxxxxxxxxx'
client = TelegramClient('anon', api_id, api_hash)


async def main():
    me = await client.get_me()
    print(me.stringify())
    username = me.username
    print(username)
    print(me.phone)
    async for dialog in client.iter_dialogs():
        print(dialog.name, 'has ID', dialog.id)
    await client.send_message('me', 'Hello, myself!')
    await client.send_message(-100123456, 'Hello, group!')
    await client.send_message('+34600123123', 'Hello, friend!')
    await client.send_message('username', 'Testing Telethon!')
    message = await client.send_message(
        'me',
        'This message has **bold**, `code`, __italics__ and '
        'a [nice website](https://example.com)!',
        link_preview=False
    )
    print(message.raw_text)
    await message.reply('Cool!')
    await client.send_file('me', '/home/me/Pictures/holidays.jpg')
    async for message in client.iter_messages('me'):
        print(message.id, message.text)
        if message.photo:
            path = await message.download_media()
            print('File saved to', path)

with client:
    client.loop.run_until_complete(main())

Tags: 代码clientsendapiidmessagehelloasync