Answer the question
In order to leave comments, you need to log in
How to use Telethon in streams?
Hello! I need to write an application using flask that will send a message to the user in a telegram. To work with the telegram, I decided to use the Telethon library. When calling the send message function, the program throws an error There is no current event loop in thread 'Thread-1'
The official documentation has an example of teleson working with threads
import asyncio
import threading
async def actual_work():
client = TelegramClient(..., loop=loop)
... # can use `await` here
def go():
asyncio.run(actual_work())
threading.Thread(target=go).start()
from telethon import TelegramClient
import asyncio
import threading
loop=asyncio.new_event_loop()
api_id = 11111111
api_hash = '911111111111'
client = TelegramClient('puat_notification.session', api_id, api_hash, loop=loop)
async def send_message():
await client.send_message('+7901111', '123')
def go():
with client:
asyncio.run(send_message())
threading.Thread(target=go).start()
Answer the question
In order to leave comments, you need to log in
Thanks to all. Figured it out myself.
Might be useful to someone:
from telethon import TelegramClient
import asyncio
import threading
async def send_message():
loop = asyncio.new_event_loop()
api_id = 555
api_hash = '555555555'
client = TelegramClient('55555555.session', api_id, api_hash, loop=loop)
async with client:
await client.send_message('+55555555', '123')
def go():
asyncio.run(send_message())
threading.Thread(target=go).start()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question