Telegram MTProto Java: how to get user token
Asked Answered
A

1

6

This says that to get a user's token I need to send a request

auth.sentCode#efed51d9 phone_registered:Bool phone_code_hash:string send_call_timeout:int is_password:Bool = auth.SentCode;

somewhere? I've read it's a MTProto request, but I'm not sure how to send it. Is it possible to send with Postman? With teletgrambots library by rubenlagus?

Adenine answered 26/8, 2019 at 15:48 Comment(0)
G
10

Telegram Protocol

MTProto is embedded into the transport protocol (TCP, HTTP,...) so it's not impossible to use Postman but I's really hard to do (so it's not the best option). Think that your http call must to implement MTProto 2.0 so your http message must to have this structure/format: structure

  • 64-bit key_fingerprint
  • 128-bit mesg_key
  • encrypted_data

See schematic presentation of messages

Only a very limited number of messages of special types can be transmitted as plain text.

TelegramBots

About TelegramBots is a lib that helps to build a Bot using the Telegram Bots Api.

It supports both techniques: Long polling and Webhook.

The project has a good tutorial that you can try: quickstart

Also, if you are a Spring boot user, it has a Spring Boot starter too (spring boot starter.

How to implement your own authentication client?

If you want to learn how to interact with telegram api to authenticate, you need to read this: auth section

After that, you can understand how to interact with Telegram to authenticate so... I suggest you read how a real app uses the Telegram API. Look at all usages of Telegram Api in this sample implementation (references to classes of package: org.telegram.api.requests.TLRequestAuth*):

ActivationController impl

As you can see in the above example, usually telegram clients uses TDLib.

Basically, Telegram provides two APIs:

Telegram API and TDLib

This API allows you to build your own customized Telegram clients. It is 100% open for all developers who wish to create Telegram applications on our platform. Feel free to study the open source code of existing Telegram applications for examples of how things work here. Don't forget to register your application in our system.

Bot API

This API allows you to connect bots to our system. Telegram Bots are special accounts that do not require an additional phone number to set up. These accounts serve as an interface for code running somewhere on your server.

If you want to create or use Telegram as a service to send and receive messages and interact from a server (without required of a telephone number)... Bot API should be your choice.

If you want to create your own customized Telegram app (your user will require a telephone number to register like one that uses telegram in a mobile phone)... Telegram API/TDLib should be your choice.

TDLib is:

a tool for third-party developers that makes it easy to build fast, secure and feature-rich Telegram apps. TDLib takes care of all network implementation details, encryption and local data storage, so that you can dedicate more time to design, responsive interfaces and beautiful animations.

TDLib supports all Telegram features and makes developing Telegram apps a breeze on any platform. It can be used on Android, iOS, Windows, macOS, Linux and virtually any other system. The library is open source and compatible with virtually any programming language.

So you don't need to take care about low level details like MTProtocol protocol, network, encryption, etc. You only need to use TDLib in that case.

The example class that I sent you, is a Telegram Application for Android (telegram app). It uses TDLib.

If you want to learn how to create your own TDLib... source is here: tdlib source

It's developed in C and can be used with Java, .NET and others.

Basically, it send messages over transport protocol (http,tcp or udp). E.g. for http using a post request to submit messages with the format above mentioned MTProto. Messages uses to be encoded.

Text about how MTProto supported transports works: supported transports

Other references:

Telegram APIs: Telegram APIs TDLib: TDLib


Example of TDLib usage in Java

Java example repo: TDLib example client

  1. First checkout TDLib repo TDLib repo As I said, TDLib is developed in C language, so you need to use JNI (Java Native Interface) to interact with it. So, you need to compile TDLib (with cmake).

  2. Follow Example instructions (readme.md) to build/prebuild TDLib in your platform (readme).

TDLib should be prebuilt for using with Java and installed to local subdirectory td/ as follows:

cd <path to TDLib sources>
mkdir jnibuild
cd jnibuild
cmake -DCMAKE_BUILD_TYPE=Release -DTD_ENABLE_JNI=ON -DCMAKE_INSTALL_PREFIX:PATH=../example/java/td ..
cmake --build . --target install

If you want to compile TDLib for 64-bit Java on Windows using MSVC, you will also need to add -A x64 option to CMake.

In Windows, use Vcpkg toolchain file by adding parameter -DCMAKE_TOOLCHAIN_FILE=/scripts/buildsystems/vcpkg.cmake

Then you can build this example:

cd <path to TDLib sources>/example/java
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DTd_DIR=<full path to TDLib sources>/example/java/td/lib/cmake/Td -DCMAKE_INSTALL_PREFIX:PATH=.. ..
cmake --build . --target install

Compiled TDLib shared library and Java example after that will be placed in bin/ and Javadoc documentation in docs/.

  1. Then you can run the Java example
cd <path to TDLib sources>/example/java/bin
java '-Djava.library.path=.' org/drinkless/tdlib/example/Example

If you get "Could NOT find JNI ..." error from CMake, you need to specify to CMake path to the installed JDK, for example, "-DJAVA_HOME=/usr/lib/jvm/java-8-oracle/".

If you get java.lang.UnsatisfiedLinkError with "Can't find dependent libraries", you may also need to copy some dependent shared OpenSSL and zlib libraries to bin/.

In case you compiled the example as 32-bit version, you may need to give -d32 parameter to Java.

If you want to open with an IDE like IntelliJ or import this sample in your project look at this ticket that talks about it: how to use IntelliJ with TDLib

Try to run the example! is very easy, it only have three Java classes (Example, Client and Log).

Client: To interact with TDLib with "native" methods.
Example: Application that uses Client.

E.g. to see how to send an Authorization Request: auth request

Glindaglinka answered 30/8, 2019 at 5:48 Comment(4)
Thank you for answering. The thing is, I already read quite a bit. I've made my own polling bot, it works fine. And I've seen that auth page. That page simply lists some commands I need to use to get a user's (not bot's!) token, but doesn't provide any examples. Since it's different from sending GET/POST requests via HTTP protocal (like one does when getting a bot's token, I'm confused how to do it without any examples. Unfortunatelly, there don't seem to be any examples about the Activationcontroller.java either.Adenine
Hi @parsecer, the class ActionController.java is not an util/lib that you should use, it's only a sample, a real usage of the TDLib. You need to use TDLib, don't try to implement your own low level implementation. It's not impossible but do you really need it? I will update my answer to clarify some points.Glindaglinka
If you could provide an example of a simplest request using TDLib, I would be very grateful. I just need an example for, say, getting a token (sending SMS to a given phone number?).Adenine
Hi @Adenine I updated the answer with link to a TDLib Java example (Look at the bottom) and some things that you need to do to compile/include TDLib in a java project.Glindaglinka

© 2022 - 2024 — McMap. All rights reserved.