Answer the question
In order to leave comments, you need to log in
How to use telebot + telethon?
I asked a question about the error, and it turns out that telebot cannot work with telethon.
Is it possible to somehow integrate them all into one in the code? Or through crutches only?
The code:
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'.
Answer the question
In order to leave comments, you need to log in
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