A
A
Alex Lukyanets2020-05-11 17:17:57
Python
Alex Lukyanets, 2020-05-11 17:17:57

How to fix error in streams (telethon library or pytelegrambotapi)?

In a bot running on the pytelegrambotapi library, I try to find an id using the telethon library (by adding it to contacts). When I call, in a separate telethon file, then everything works. When I try to call a method from a script with the telethon library in the pytelegrambotapi bot through a button and a class, an error occurs, can it be fixed?

class GetIdbyPhone():
    @staticmethod
    def getIdUserByPhone(phone):
        client = TelegramClient(username, api_id, api_hash)
        result = client(functions.contacts.ImportContactsRequest(
            contacts=[types.InputPhoneContact(
                client_id=0,
                phone=phone,
                first_name='Alex',
                last_name=""
            )]
        ))

        for i in result.users:
            data = str(i.id)
        return data

    @classmethod
    def tryThread(cls, phone):
        loop.create_task(cls.getIdUserByPhone(phone))
        loop.run_until_complete()


Error after executing
5eb95ef4e97d9939878277.png

RuntimeError: There is no current event loop in thread 'WorkerThread8'.

Answer the question

In order to leave comments, you need to log in

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

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