Answer the question
In order to leave comments, you need to log in
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()
Answer the question
In order to leave comments, you need to log in
Because there is no need to mix multi-threaded code with asynchronous. Use either one library or another.
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 questionAsk a Question
731 491 924 answers to any question