【发布时间】:2022-01-24 15:42:02
【问题描述】:
我在 python-telgram-bot 中遇到问题。 RuntimeError:线程'Bot:chat_id:dispatcher'中没有当前事件循环。我不知道这个运行时错误是什么。请帮助我 我的代码是这样的:
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:
RuntimeError: There is no current event loop in thread 'Bot:chat_id:dispatcher'
我该怎么办??????
【问题讨论】:
-
我有我的 CHANNELID 、 API_ID 、 API_HASH 、 PHONE NUMBER ,你应该自己试试这些。
-
如果可能,我建议不要混合线程和
asyncio代码。您应该使用 Telethon 的线程替代品,或 PTB 的asyncio替代品。例如,如果您也将 Telethon 用于机器人,则不会出现此问题。
标签: python runtime-error python-telegram-bot telethon