Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
14.8k views
in Technique[技术] by (71.8m points)

c# - Steps after creating Authorization Key for Telegram API

I've been messing with implementing a C# version of the Telegram API, but I am stuck. I've successfully figured out how to create an Authorization Key, but I don't know where to go from here. Does anyone know the next step after creating an Authorization Key? The documentation is so difficult to follow.

For Reference: Telegram API

Note: I am not using the Bot API. I'm trying to use the regular API.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

After creating your Auth_key I find it easiest to ensure I am connected to my nearest DataCenter before I proceed. Also you should send an InitConnection command along with the current layer (API version) that your code will be working with.

Here an example of what I send:

msg = TL.invokewithlayer(@layer, TL.initconnection(@app_id, @device_model, @system_version, @app_version, @lang_code, TL.help_getnearestdc))

Now before you send this here is some more background:

1) Telegram Servers communicate in TL, what it is basically is a custom developed encoding scheme that Telegram uses to express everything: from commands to all it's types. You would need to build yourself a decoder and encoder for converting raw bytes into TL and vice-versa.

2) Telegram updates their API versions from time to time, but the version on their site is outdated. You can easily get the latest API spec of their Official Open-source projects. In particular this and this from Webogram are quite useful for generating your own TL parser. The current layer version is 45

3) So when you send your init + nearestDc request, this is most likely your first MTproto Encrypted message, so you would need to create a new random 64-bit number as your session key, but also you need a valid server_salt...

4) You might have skipped this while creating your Auth_Key, but you can create a valid initial server_salt from this:

server_salt = substr(new_nonce, 0, 8) XOR substr(server_nonce, 0, 8)

you can look that up here: STEP 9) DH key exchange complete

5) You now have your server_salt, a new random 64-bit session_id and you want to send the following:

msg = TL.invokewithlayer(@layer, TL.initconnection(@app_id, @device_model, @system_version, @app_version, @lang_code, TL.help_getnearestdc))

MTProto format is: auth_id + msg_key + enc_payload

enc_payload = AES_IGE_enc(payload)

payload = salt + session_id + msg_id + seq_no + len(msg) + msg + padding

You can get the above from here

6) now you expect a result that tells you your nearest dc_id, if this is different from dc_id = 2 (usually dc_id = 2 is the default you start with) then you need to disconnect and start up a new connection to the your new dc_id and re-generate your auth_key connected to this new dc. Your dc_ids point to a list of Telegram Data-center IP addresses ['149.154.175.50', '149.154.167.51', '149.154.175.100', '149.154.167.91', '149.154.171.5']

7) once connected to the correct "nearest dc" you can now carry out the User Authorization, to authorize a (your) mobile number to access telegram using your new Telegram client

8) all subsequent messages you send to Telegram will be sent using the same session_id and salt, and following the MTProto encryption steps like before

9) note that salt is usually good for just 24 hours. The server will send you a new salt, which you can use to replace expired salts. session_id is usually long lived.

10) once you get the hang of these few steps you can then try sending messages or getting your list of contacts and message history

Cheers.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...