RuntimeError: There is no current event loop in thread 'Bot:chat_id:dispatcher' in python-telegram-bot
RuntimeError: There is no current event loop in thread 'Bot:chat_id:dispatcher' in python-telegram-bot
我在 python-telgram-bot 中遇到问题。 RuntimeError:线程 'Bot:chat_id:dispatcher' 中没有当前事件循环。我不知道这个运行时是什么 error.Please 帮帮我
我的代码是这样的:
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()
但是因为这段代码 :
# --------------------------------------------------------------------------
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)
# --------------------------------------------------------------------------
我有这样的运行时错误:
RuntimeError: There is no current event loop in thread 'Bot:chat_id:dispatcher'
我该怎么办?????
如错误中所述,asyncio 没有事件循环到 运行 in
所以解决办法就是简单地创建一个
在你的线程中你应该这样做
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
确保你这样做在线程中
该线程是由 pytelegrambot 创建的,因此我建议为此对调度函数进行猴子修补,或者也为您的机器人纯粹使用 telethon
我在 python-telgram-bot 中遇到问题。 RuntimeError:线程 'Bot:chat_id:dispatcher' 中没有当前事件循环。我不知道这个运行时是什么 error.Please 帮帮我 我的代码是这样的:
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()
但是因为这段代码 :
# --------------------------------------------------------------------------
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)
# --------------------------------------------------------------------------
我有这样的运行时错误:
RuntimeError: There is no current event loop in thread 'Bot:chat_id:dispatcher'
我该怎么办?????
如错误中所述,asyncio 没有事件循环到 运行 in
所以解决办法就是简单地创建一个 在你的线程中你应该这样做
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
确保你这样做在线程中
该线程是由 pytelegrambot 创建的,因此我建议为此对调度函数进行猴子修补,或者也为您的机器人纯粹使用 telethon