@perfect_fsb

Как использовать telebot + telethon?

Я задавал вопрос на счет ошибки, и оказывается что telebot не может работать c telethon.
Можно ли как-то интегрировать их всех в один в код? Или через костыли только?

Код:
import telebot, sqlite3, requests, time
from telebot import types
from datetime import datetime
# -*- coding: utf-8 -*-
from telethon import TelegramClient, events, sync
from telethon.tl.types import InputPhoneContact
from telethon import functions, types
import random, string

client = TelegramClient('+125', '140', '22')
client.connect()
if not client.is_user_authorized():
    client.send_code_request('')
    client.sign_in('+125', input('Enter code: '))

TOKEN = '1239885502:s'
bot = telebot.TeleBot(TOKEN)

def sex(message):
    num = message.text
    randoma = random.choice(string.ascii_letters)
    randomb = random.choice(string.ascii_letters)
    contact = InputPhoneContact(client_id = 0, phone = num, first_name=randoma, last_name=randomb)
    contacts = client(functions.contacts.ImportContactsRequest([contact]))
    username = contacts.to_dict()['users'][0]['username']
    bot.send_message(message.chat.id, f'@{username}')

@bot.message_handler(content_types=['text'])
def referer(message):
        a = bot.send_message(message.chat.id, '''отправь мне номер: ''')
        bot.register_next_step_handler(a, sex)

while True:
    try:
        bot.infinity_polling(interval=0)
    except:
        bot.infinity_polling(interval=0)


Ошибка:
2020-08-13 20:40:43,608 (util.py:68 WorkerThread1) ERROR - TeleBot: "RuntimeError occurred, args=("There is no current event loop in thread 'WorkerThread1'.",)
Traceback (most recent call last):
File "C:\Users\admin\AppData\Local\Programs\Python\Python37\lib\site-packages\telebot\util.py", line 62, in run
task(*args, **kwargs)
File "C:\Users\admin\Desktop\testbot.py", line 24, in sex
contacts = client(functions.contacts.ImportContactsRequest([contact]))
File "C:\Users\admin\AppData\Local\Programs\Python\Python37\lib\site-packages\telethon\sync.py", line 35, in syncified
loop = asyncio.get_event_loop()
File "C:\Users\admin\AppData\Local\Programs\Python\Python37\lib\asyncio\events.py", line 644, in get_event_loop
% threading.current_thread().name)
RuntimeError: There is no current event loop in thread 'WorkerThread1'.


Помогите пожалуйста!!!
  • Вопрос задан
  • 997 просмотров
Пригласить эксперта
Ответы на вопрос 1
@agent_2203
from asyncio import set_event_loop, new_event_loop
set_event_loop(new_event_loop())
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы