K
K
Kiril-cloud2021-06-05 18:42:19
Python
Kiril-cloud, 2021-06-05 18:42:19

Python telebot and telethon error There is no current event loop in thread 'WorkerThread1'?

Here is my code:

from telethon import TelegramClient, sync
import telebot
import asyncio
from telebot.types import ReplyKeyboardMarkup, KeyboardButton, InlineKeyboardMarkup, InlineKeyboardButton


menu = ReplyKeyboardMarkup(resize_keyboard=True, row_width=2)
oder = KeyboardButton('Заказать рассылку')
info = KeyboardButton('Информация о сервисе')
tarif = KeyboardButton('Тарифы')
menu.add(oder, tarif).add(info)

bot = telebot.TeleBot(токен)


@bot.message_handler(commands=['start'])
def start(message):
  bot.send_message(message.chat.id, 'Привет, я бот который поможет тебе сделать рассылку сообщений по целевой аудитории и привлечь заказчиков в аш бизнес.', reply_markup=menu)
  

@bot.message_handler(content_types = ['text'])
def messages(message):	
  if message.text == 'Заказать рассылку':
    bot.send_message(message.chat.id, 'Пришлите канал или чат с целевой аудиторией для рассылки')
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    bot.register_next_step_handler(message,send_message)
    
    
def send_message(message):
  try:
    api_id = апи ид
    api_hash = 'апи хеш'
    client = TelegramClient('имя', api_id, api_hash)
    client.start()
    channel = client.get_entity(message.text)
    c = client.get_participants(channel, limit=1000)
      
    for user in c:
      print(user)
      client.send_message(user.username, 'Hello. This is test message.')
    #bot.send_message(message.chat.id, 'Сообщения отправлены!')
  except Exception as e:
    print(e)
    
bot.polling()

When it comes to the send_message function it throws an error: There is no current event loop in thread 'WorkerThread1

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
agent_2203, 2021-06-11
@Kiril-cloud

Because there is no need to mix multi-threaded code with asynchronous. Use either one library or another.

Or as a last resort
from asyncio import set_event_loop, new_event_loop
set_event_loop(new_event_loop())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question