0
import telepot, time, threading, asyncio
from telepot.loop import MessageLoop
from datetime import datetime
from telepot.namedtuple import ReplyKeyboardMarkup, KeyboardButton, InlineKeyboardMarkup, InlineKeyboardButton
from telethon.sync import TelegramClient
from telethon.tl.types import PeerChannel
...
all_participants = []
pts_organ = {}
...
client = TelegramClient(number, api_id, api_hash).start(bot_token = bot_token)
def getMembros(canal_id):
global all_participants, pts_organ
...
all_participants = client.get_participants(PeerChannel(canal_id), aggressive=True)
...
return pts_organ
...
def handle(msg):
...
else:
if (msg[content_type] == '/todos_os_membros'):
getMembros(chat_id)
MessageLoop(bot, handle).run_as_thread()
When I run the script, it shows the following error:
Traceback (most recent call last):
File "C:\Users\Marcelo\AppData\Local\Programs\Python\Python38-32\lib\site-packages\telepot\loop.py", line 37, in run_forever
self._handle(msg)
File "C:\Users\Marcelo\Desktop\bot.py", line 103, in handle
getMembros(chat_id)
File "C:\Users\Marcelo\Desktop\bot.py", line 26, in getMembros
all_participants = client.get_participants(PeerChannel(canal_id), aggressive=True)
File "C:\Users\Marcelo\AppData\Local\Programs\Python\Python38-32\lib\site-packages\telethon\sync.py", line 35, in syncified
loop = asyncio.get_event_loop()
File "C:\Users\Marcelo\AppData\Local\Programs\Python\Python38-32\lib\asyncio\events.py", line 639, in get_event_loop
raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'Thread-2'.
Warning (from warnings module):
File "C:\Users\Marcelo\AppData\Local\Programs\Python\Python38-32\lib\site-packages\telepot\loop.py", line 39
traceback.print_exc()
RuntimeWarning: coroutine 'ChatMethods.get_participants' was never awaited
I just want my method getMembros(canal_id)
return the list of members of a channel, but it seems that I am not succeeding by the fact that the method get_participants()
of API Telethon is asynchronous. I don’t know for sure. I don’t understand much about the module asy and Thread.
I’ve tried to:
...
async def getMembros(canal_id):
...
all_participants = await client.get_participants(PeerChannel(canal_id), aggressive=True)
The mistake:
Warning (from warnings module):
File "C:\Users\Marcelo\Desktop\bot.py", line 103
getMembros(chat_id)
RuntimeWarning: coroutine 'getMembros' was never awaited
Afterward:
...
async def handle(msg):
...
await getMembros(chat_id)
And the following mistake:
Warning (from warnings module):
File "C:\Users\Marcelo\AppData\Local\Programs\Python\Python38-32\lib\site-packages\telepot\loop.py", line 37
self._handle(msg)
RuntimeWarning: coroutine 'handle' was never awaited
I don’t know what else to do. I know that client.get_participants()
it takes some time to finish, so I wanted to know how I can leave it in parallel, for what handle(msg)
keep running or something. Or find a way to get the dictionary back pts_organ
, of getMembros
, who is responsible for returning it, within the handle(msg)