Signing In to Telegram Client with Telethon automatically (python)

18k views Asked by At

I am trying to program a telegram bot which accesses the Telegram Client, using the Telethon library. Everything is working correctly in the code below, but when running the code, the Telegram Auth procedure is run through the Terminal. Is there a way to automate the process so that I can sign In the client with Python (without having to type in the Terminal).

The Auth procedure asks for:

  • phone number
  • Password
  • Security Code

What I am trying to achieve is that when the user calls a certain command, the bot initiates the client login procedure and asks the user to input the password and the security code, which than it uses to login into the client. The bot would use the python-telegram-bot library to manage the Conversation with the user, while it would take use of the Telethon library to connect to the client. Is that even possible? Thank you

Here is the main file: (a working test example, trying to login a Telethon Telegram Client while using python-telegram-bot)

from telethon import TelegramClient
from karim.secrets import secrets
import asyncio

# this def gets called when the /telethon command is sent by the user to the bot
def telethonMessage(update, context):
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    api_id = secrets.get_var('API_ID')
    api_hash = secrets.get_var('API_HASH')
    client = TelegramClient('anon', api_id, api_hash, loop=loop)
    with client:
        loop.run_until_complete(send_telethon_message(client, update.effective_user.id))
     

async def send_telethon_message(client, user_id):
    me = await client.get_me()
    print('TELETHON: {}', me.username)
    await client.send_message(user_id, 'Testing Telethon')

with the code above, I receive the following procedure in the Terminal:

  • Please enter your phone (or bot token):
  • Please enter the code you received:
  • Please enter your password:
3

There are 3 answers

0
Tibebes. M On BEST ANSWER

Another way to authenticate a user would be to use send_code_request, sign_in methods as documented in this section

As a full example:

async def main():
    client = TelegramClient('anon', api_id, api_hash)
    assert await client.connect()
    if not client.is_user_authorized():
        await client.send_code_request(phone_number)
        me = await client.sign_in(phone_number, input('Enter code: '))

All of this, however, can be done through a call to .start():

async def main():
    client = TelegramClient('anon', api_id, api_hash)
    await client.start()

The code shown is just what .start() will be doing behind the scenes (with a few extra checks), so that you know how to sign in case you want to avoid using input() (the default) for whatever reason.

Read the section fully to understand how to handle edge cases such as when 2FA password is required.

0
painor On

you can do that in multiple ways with the easiest being .start.

When you do with client: telethon is doing .start in the background with default values

doc reference : https://docs.telethon.dev/en/latest/modules/client.html?telethon.client.auth.AuthMethods.start

So to answer your question the way to do it would be instead of calling with client:

await client.start(phone=your_phone_callback,password=your_password_callback,code_callback=your_code_callback)

All of them need to be either a callable that returns a string or a string.

0
Seba Vasile On

You may want to use the bot_token for the auth procedure, which is complementary to phone_number.

bot = await TelegramClient(username, api_id, api_hash).start(bot_token=token, max_attempts=10)