Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Signing In to Telegram Client with Telethon automatically (python)

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:
like image 479
David Wicker Avatar asked Oct 25 '25 02:10

David Wicker


1 Answers

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.

like image 114
Tibebes. M Avatar answered Oct 27 '25 17:10

Tibebes. M