I have a problem in python-telgram-bot . RuntimeError: There is no current event loop in thread 'Bot:chat_id:dispatcher'.I don't know what is this runtime error.Please help me my code is this :
from threading import Thread
from telegram import update
import io
from telethon.sync import TelegramClient
from telethon.tl.functions.messages import GetDialogsRequest
from telethon.tl.types import InputPeerEmpty
import csv
import telethon
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
token = "TOKEN"
def start_handler(bot, update):
chat_id = update.message.chat_id
first_name = update.message.chat.first_name
last_name = update.message.chat.last_name
# --------------------------------------------------------------------------
api_id = API_ID
api_hash = 'API_HASH'
phone = 'PHONENUMBER'
client = TelegramClient(phone, api_id, api_hash)
client.connect()
if not client.is_user_authorized():
client.send_code_request(phone)
client.sign_in(phone, input('Enter the code: '))
# get all the channels that I can access
channels = {d.entity.username: d.entity
for d in client.get_dialogs()
if d.is_channel}
# choose the one that I want list users from
channel = channels['CHANNELID']
# get all the users and print them
member_list = []
for u in client.get_participants(channel):
member_list.append([u.id, u.first_name, u.last_name, u.username])
print(member_list)
# --------------------------------------------------------------------------
updater = Updater(token)
start_command = CommandHandler('start', start_handler)
updater.dispatcher.add_handler(start_command)
updater.start_polling()
updater.idle()
but because of this code :
# --------------------------------------------------------------------------
api_id = API_ID
api_hash = 'API_HASH'
phone = 'PHONENUMBER'
client = TelegramClient(phone, api_id, api_hash)
client.connect()
if not client.is_user_authorized():
client.send_code_request(phone)
client.sign_in(phone, input('Enter the code: '))
# get all the channels that I can access
channels = {d.entity.username: d.entity
for d in client.get_dialogs()
if d.is_channel}
# choose the one that I want list users from
channel = channels['CHANNELID']
# get all the users and print them
member_list = []
for u in client.get_participants(channel):
member_list.append([u.id, u.first_name, u.last_name, u.username])
print(member_list)
# --------------------------------------------------------------------------
i have RuntimeError like this:
RuntimeError: There is no current event loop in thread 'Bot:chat_id:dispatcher'
what should i do?????
as said in the error there is no event loop for asyncio to run in
so the solution is to simply create one in your thread you should do this
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
make sure you do this INSIDE the thread
the thread is created by pytelegrambot so i suggest monkey-patching the dispatch function for this or purely use telethon for your bot as well