簡體   English   中英

RuntimeError:python-telegram-bot 中的線程 'Bot:chat_id:dispatcher' 中沒有當前事件循環

[英]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'中沒有當前事件循環。我不知道這個運行時錯誤是什么。請幫助我,我的代碼是這樣的:

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'

我應該怎么辦?????

如錯誤中所述,沒有用於異步運行的事件循環

所以解決方案是簡單地在你的線程中創建一個你應該這樣做

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

確保你在線程內部執行此操作

該線程是由 pytelegrambot 創建的,所以我建議為此修補調度 function 或純粹為您的機器人使用 Telethon

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM