All right, so I've been intending to use Telethon to automate several things on telegram with Python, but I'm not sure I understand the gist of it.
First of all, you need an api_id and an api_hash. To do so, you go to my.telegram and go to API development tools. There you are sent a code to your telegram android phone and after submitting, you receive an unique id/hash. First question, is this code you are sent in order to generate an app necessary any more?
Now in python, you can start a client as follows.
from telethon import TelegramClient
api_id=12345
api_hash='abcdef12345ghij'
client=TelegramClient('name of the session',api_id,api_hash)
You can try to connect the client, but it can lead to it not being authorized or the phone not being signed up, so you can use start, which will decide between sign in, or sign up.
Among the parameters you can set in start, there is force_sms (bool, optional) to force telegram to share the code needed to register and log in by sms.
My question here are, if the phone is not signed up, what other means could have telegram used? I mean, they can't send it to the mobile app as that phone doesn't have one.
If a phone not being signed up is a possibility, does this mean the phone with which you got your id/hash is is not necessarily the same with which you create the client?
As this method has a callback, you can input the code sent to your phone and connect to telegram.
Another way to connect a client is using StringSession. I found this piece of code:
from telethon.sync import TelegramClient
from telethon.sessions import StringSession
# Generating a new one
with TelegramClient(StringSession(), api_id, api_hash) as client:
print(client.session.save())
# Converting SQLite (or any) to StringSession
with TelegramClient(name, api_id, api_hash) as client:
print(StringSession.save(client.session))
# Usage
string = '1aaNk8EX-YRfwoRsebUkugFvht6DUPi_Q25UOCzOAqzc...'
with TelegramClient(StringSession(string), api_id, api_hash) as client:
client.loop.run_until_complete(client.send_message('me', 'Hi'))
This bring several questions on its own. According to the docs, this is a way to storing in a string the credentials needed to login, including the authorization key and the phone.
How is this obtaining the authorization key? In the other method, it was sent to your phone for you to input, but here? How can you specify the phone to which you want to connect? Is this a method you can only, or you should only use after the phone has been given access?
In code, is this a possibility?
#Obtain an api_id, api_hash from phone 777777777
from telethon import TelegramClient
from telethon.sessions import StringSession
api_id=12345
api_hash='abcdef12345ghij'
client=TelegramClient('name of the session',api_id,api_hash)
client.start(phone='5555555',force_sms=True,code_callback=True,first_name='John',last_name='Doe')
#Asked to input the code sent to the phone 555555 by sms. Is this code the authentication key?
string = StringSession.save(client.session) #Now I can connect whenever I want using string session.
Two last questions
Can you have more than one session for the same number, even if they don't try to connect at the same time? Like for example, with different api/hash starting the same phone at different times, or as the first session is stored within telegram, creating the second severes the link to telegram of the first?
Can you skip in any way the verification code using for signing up?
Kind regards