Upon trying following code on Telethon 1.11.3 , python 3.7.3
from telethon import TelegramClient
api_id = xxx #i give my id
api_hash = xxx##i give my
client = TelegramClient(name, api_id, api_hash)
async def main():
# Now you can use all client methods listed below, like for example...
await client.send_message('me', 'Hello to myself!')
with client:
client.loop.run_until_complete(main())
I get error as RuntimeError: You must use “async with” if the event loop is running (i.e. you are inside an “async def”)
2
Answers
The problem is not the code itself but the shell you used. IPython and similar run the
asyncio
event loop which break Telethon’ssync
magic.To work around this, you can use a normal
python
shell or writeasync
andawait
in the right places:Of course, in this scenario,
main()
is not really necessary either: