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:
Another way to authenticate a user would be to use
send_code_request
,sign_in
methods as documented in this sectionRead the section fully to understand how to handle edge cases such as when 2FA password is required.