首页 > 解决方案 > RuntimeWarning: coroutine 'main' was never awaited error

问题描述

I run this test code:

import telethon.sync
from telethon import TelegramClient 
from telethon.tl.functions.messages import AddChatUserRequest 
from telethon.tl.functions.contacts import ImportContactsRequest 

api_id = XXXXXXX
api_hash = 'XXXXXXXXXXXXXXC'

with TelegramClient('anon', api_id, api_hash) as client:
async def main():

client(AddChatUserRequest(-XXXXXXXXXXXXXX, ['username'], fwd_limit=10))
main()

And it gives me this:

/data/data/ru.iiec.pydroid3/files/temp_iiec_codefile.py:19: RuntimeWarning: coroutine 'main' was never awaited
main()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

What should I do to make the program work?

标签: pythonasync-awaitpython-asynciotelethon

解决方案


看一下telethon文档,看看它是如何启动事件循环的:

from telethon import TelegramClient


api_id = 12345
api_hash = '0123456789abcdef0123456789abcdef'
client = TelegramClient('anon', api_id, api_hash)


async def main():
    me = await client.get_me()
    # etc.


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

推荐阅读