Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Telegram MTProto Java: how to get user token

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?

like image 725
parsecer Avatar asked Aug 26 '19 15:08

parsecer


1 Answers

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

like image 118
Ariel Carrera Avatar answered Sep 24 '22 11:09

Ariel Carrera