Telegram MTProto Java:如何获取用户令牌 [英] Telegram MTProto Java: how to get user token

查看:53
本文介绍了Telegram MTProto Java:如何获取用户令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

表示要获取用户的令牌,我需要发送请求

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

某处?我读过它是一个 MTProto 请求,但我不确定如何发送它.可以用邮递员寄吗?使用 teletgrambots 图书馆 by rubenlagus?

解决方案

Telegram Protocol

MTProto 被嵌入到传输协议(TCP、HTTP 等)中,所以使用 Postman 并非不可能,但我真的很难做到(所以它不是最好的选择).认为您的 http 调用必须实现 MTProto 2.0,因此您的 http 消息必须具有以下结构/格式:结构

  • 64 位密钥指纹
  • 128 位 mesg_key
  • 加密数据

参见消息示意图

只有非常有限数量的特殊类型的消息可以作为纯文本传输.

电报机器人

关于 TelegramBots 是一个使用 Telegram Bots Api 帮助构建机器人的库.

它支持两种技术:长轮询Webhook.

该项目有一个很好的教程,你可以试试:quickstart

此外,如果您是 Spring Boot 用户,它也有一个 Spring Boot starter (spring boot starter.

如何实现自己的身份验证客户端?

如果你想学习如何与电报api交互进行身份验证,你需要阅读这个:auth 部分

之后,您可以了解如何与 Telegram 交互以进行身份​​验证……我建议您阅读真正的应用程序如何使用 Telegram API.查看此示例实现中 Telegram Api 的所有用法(对包类的引用:org.telegram.api.requests.TLRequestAuth*):

ActivationController impl

如上例所示,通常电报客户端使用 TDLib.

基本上,Telegram 提供了两个 API:

Telegram API 和 TDLib

<块引用>

此 API 允许您构建自己的自定义 Telegram 客户端.它对所有希望创建 Telegram 的开发人员 100% 开放我们平台上的应用程序.随意研究开源代码现有 Telegram 应用程序的示例,以了解事物的工作原理这里.不要忘记在我们的系统中注册您的应用程序.

机器人 API

<块引用>

此 API 允许您将机器人连接到我们的系统.电报机器人是不需要额外电话号码来设置的特殊账户向上.这些帐户用作代码运行的接口您的服务器.

如果您想创建或使用 Telegram 作为服务来发送和接收消息并与服务器交互(不需要电话号码)... Bot API 应该是您的选择.

如果您想创建自己的自定义 Telegram 应用程序(您的用户将需要一个电话号码来注册,就像在手机中使用电报一样)... Telegram API/TDLib 应该是您的选择.

TDLib 是:

<块引用>

第三方开发人员的工具,可以轻松快速地构建,安全且功能丰富的 Telegram 应用程序.TDLib 负责所有网络实施细节、加密和本地数据存储,让您可以将更多时间用于设计、响应式界面和美观动画.

TDLib 支持所有 Telegram 功能,并使开发 Telegram应用程序在任何平台上都轻而易举.它可以在安卓、iOS、Windows、macOS、Linux 和几乎任何其他系统.图书馆是开源并与几乎任何编程语言兼容.

因此您无需关心 MTProtocol 协议、网络、加密等底层细节.在这种情况下,您只需要使用 TDLib.

我发送给您的示例类是一个 Android 电报应用程序(电报应用程序).它使用 TDLib.

如果您想学习如何创建自己的 TDLib...源代码在这里:tdlib 源码

它是用 C 语言开发的,可以与 Java、.NET 和其他语言一起使用.

基本上,它通过传输协议(http、tcp 或 udp)发送消息.例如.对于 http,使用 post 请求以上述 MTProto 格式提交消息.消息用于编码.

关于 MTProto 支持的传输如何工作的文本:支持的传输

其他参考:

电报 API:电报 APITDLib:TDLib

<小时>

TDLib 在 Java 中的使用示例

Java 示例存储库:TDLib 示例客户端

  1. 首先结帐 TDLib 存储库 TDLib 存储库正如我所说,TDLib是用C语言开发的,所以需要使用JNI(Java Native Interface)与之交互.因此,您需要编译 TDLib(使用 cmake).

  2. 按照示例说明 (readme.md) 在您的平台中构建/预构建 TDLib (自述文件).

<块引用>

TDLib 应预先构建以与 Java 一起使用并安装到本地子目录td/如下:

cd mkdir jnibuildcd jnibuildcmake -DCMAKE_BUILD_TYPE=发布 -DTD_ENABLE_JNI=ON -DCMAKE_INSTALL_PREFIX:PATH=../example/java/td ..cmake --build .--目标安装

<块引用>

如果您想在 Windows 上使用 MSVC 为 64 位 Java 编译 TDLib,您还需要向 CMake 添加 -A x64 选项.

在Windows中,通过添加参数使用Vcpkg工具链文件-DCMAKE_TOOLCHAIN_FILE=/scripts/buildsystems/vcpkg.cmake

然后你可以构建这个例子:

cd /example/javamkdir 构建光盘构建cmake -DCMAKE_BUILD_TYPE=Release -DTd_DIR=/example/java/td/lib/cmake/Td -DCMAKE_INSTALL_PREFIX:PATH=.. ..cmake --build .--目标安装

<块引用>

编译后的 TDLib 共享库和 Java 示例将是放在 docs/ 中的 bin/和 Javadoc 文档中.

  1. 然后您可以运行 Java 示例

cd /example/java/binjava '-Djava.library.path=.'org/drinkless/tdlib/example/示例

<块引用>

如果您从 CMake 收到Could NOT find JNI ..."错误,您需要指定到已安装 JDK 的 CMake 路径,例如,-DJAVA_HOME=/usr/lib/jvm/java-8-oracle/".

如果你得到 java.lang.UnsatisfiedLinkError 和找不到依赖库",您可能还需要复制一些依赖的共享 OpenSSL和 zlib 库到 bin/.

如果您将示例编译为 32 位版本,您可能需要给 Java 提供 -d32 参数.

如果您想使用 IntelliJ 等 IDE 打开或在您的项目中导入此示例,请查看有关它的票证:如何在 TDLib 中使用 IntelliJ

尝试运行示例!很简单,它只有三个Java类(Example、Client和Log).

客户端:使用本机"方法与 TDLib 交互.
示例:使用客户端的应用程序.

例如查看如何发送授权请求:身份验证请求

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?

解决方案

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

这篇关于Telegram MTProto Java:如何获取用户令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆