Reputation: 65
I am using telethon to automate some tasks on Telegram. I am trying to create an API where third party users can provide phone number and enter code through an api. I have got the phone number part working, As I allow users to input their phone number through a webservice, this gets wrote to a file, then I am opening that file and fetching the phone number in python which is {number}, I then connect to the client using below.
client = TelegramClient(f'{number}', API_ID, API_KEY)
try:
await client.connect()
except Exception as e:
print('Failed to connect', e, file=sys.stderr)
return
Once the code is run the user enters the verification code (not in the python app) which gets wrote to a file.
And in python the following is returned
Please enter the code you received:
I can open the file which contains the verification code which as {code} but how do I use {code} to reply to 'Please enter the code you received:'
Thanks
Upvotes: 2
Views: 3016
Reputation: 71
You can try this:
phone = "+123456789"
client = TelegramClient(phone, api_id, api_hash)
client.connect()
client.sign_in(phone) # send code
try:
code = input('enter code: ') # here instead of using input('enter code: ') you can just pass in the code they gave.
client.sign_in(phone=phone, code=code)
print(f'Successfully signed in as: {client.get_me().first_name} {client.get_me().last_name}')
except Exception as e:
print(f'Sign-in failed: {e}')
Upvotes: 0
Reputation: 282
It is possible, but this will be very complex as the code is sent to another device. You can write custom Telegram client that will send this code to your program, but it is too complex and in 99.9% of cases you won't need it.
Edit:
If you already have this code, let's say in code
variable, you can try to use method sign_in()
instead of connect()
try:
await client.sign_in(number, code)
except Exception as e:
print('Failed to connect', e, file=sys.stderr)
return
Reference for sign_in() in docs
Upvotes: 0