M
M
matvey2022-03-23 23:10:17
Python
matvey, 2022-03-23 23:10:17

How to enter text into the command line through a bot in a telegram?

To create a client file, you must fill in the phone number and authentication code. I want to implement their input through a bot in telegrams. How can you enter text on the command line? In which direction to move? telethon library.
623b7c792f69a236107949.png
The creation of such sessions occurs through:

client = TelegramClient('session_test', api_id, api_hash)
client.start()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
i3a4em, 2022-03-24
@mogilyoy

client.start() can take a coroutine as an argument. I use it just the way you describe.
I don't have the code, but I'll try

async def GetCode(self, timeout):
        while not self.code and timeout > int(time.time()):
            self.project = await self.getDB()
            bot_last_message = self.project.bot_last_message.split(":")
            if bot_last_message:
                message_type = bot_last_message[0]
                if message_type == "telegram_code":
                    telegram_code = bot_last_message[2]
                    self.code = telegram_code
                    await self.clear_bot_last_message()
                    return self.code

            await asyncio.sleep(1)
        return None

client.start(phone, code_callback=self.GetCode)

first we get phone from the bot, then we create a client and wait for the code. As soon as it appears in the database, the GetCode coroutine will end and client.start will continue executing
bot_last_message looks like this: telegram_code:phone:code

L
LXSTVAYNE, 2022-03-23
@lxstvayne

You can throw a line into the input stream

import sys
import io

sys.stdin = io.StringIO("Hello")
a = input()
print(a)

sys.stdin = io.StringIO("World!")
b = input()
print(b)

But, I'm not sure if this is the best solution...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question