Telegram MTProto Java: 如何获取用户令牌
Telegram MTProto Java: how to get user token
This 说要获得用户的令牌我需要发送请求
auth.sentCode#efed51d9 phone_registered:Bool phone_code_hash:string send_call_timeout:int is_password:Bool = auth.SentCode;
在什么地方?我读过这是一个 MTProto 请求,但我不确定如何发送它。可以用Postman寄吗? teletgrambots
library 通过 rubenlagus
?
电报协议
MTProto 嵌入到传输协议(TCP、HTTP、...)中,因此使用 Postman 并非不可能,但我真的很难做到(所以这不是最佳选择)。
认为您的 http 调用必须实现 MTProto 2.0 因此您的 http 消息必须具有此 structure/format: structure
- 64 位 key_fingerprint
- 128 位 mesg_key
- encrypted_data
见schematic presentation of messages
只有非常有限的特殊类型的消息可以作为纯文本传输。
TelegramBots
关于 TelegramBots 是一个帮助使用 Telegram Bots Api.[=40= 构建机器人的库]
它支持两种技术:长轮询和Webhook。
该项目有很好的教程,您可以试试:quickstart
此外,如果您是 Spring 引导用户,它也有一个 Spring 引导程序 (spring boot starter.
如何实现自己的认证客户端?
如果您想了解如何与电报api交互以进行身份验证,您需要阅读此内容:auth section
之后,您可以了解如何与 Telegram 交互以进行身份验证,所以...我建议您阅读真实应用程序如何使用 Telegram API。
查看此示例实现中 Telegram Api 的所有用法(引用包的 classes:org.telegram.api.requests.TLRequestAuth*):
正如您在上面的示例中看到的,通常电报客户端使用 TDLib。
基本上,Telegram 提供了两个API:
Telegram API 和 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.
机器人 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.
如果您想创建或使用 Telegram 作为服务来发送和接收消息并与服务器交互(不需要电话 phone 号码)...Bot API 应该是你的选择。
如果您想创建自己的定制 Telegram 应用程序(您的用户将需要一个电报phone 号码来注册,就像在手机 phone 中使用电报一样)... Telegram API/TDLib 应该是你的选择。
TDLib 是:
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.
因此您无需关心 MTProtocol 协议、网络、加密等底层细节。在这种情况下您只需要使用 TDLib。
我发送给您的示例 class 是 Android (telegram app) 的电报应用程序。它使用 TDLib。
如果您想学习如何创建自己的 TDLib...源代码在这里:
tdlib source
它是用 C 语言开发的,可以与 Java、.NET 等一起使用。
基本上,它通过传输协议(http、tcp 或 udp)发送消息。例如。对于 http,使用 post 请求提交具有上述 MTProto 格式的消息。消息用于编码。
关于 MTProto 支持的传输如何工作的文本:supported transports
其他参考资料:
电报 APIs: Telegram APIs
TDLib:TDLib
Java
中的 TDLib 使用示例
Java 示例回购:TDLib example client
首先检查 TDLib 存储库 TDLib repo
正如我所说,TDLib 是用 C 语言开发的,因此您需要使用 JNI(Java Native Interface)与之交互。因此,您需要编译 TDLib(使用 cmake)。
按照示例说明 (readme.md) 在您的平台 (readme) 中 build/prebuild TDLib。
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/
.
- 然后你可以运行Java例子
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.
如果您想使用像 IntelliJ 这样的 IDE 打开或在您的项目中导入此示例,请查看这张票:how to use IntelliJ with TDLib
试试运行的例子!很简单,只有三个 Java classes (Example, Client and Log).
客户端:使用"native"方法与TDLib交互。
示例:使用客户端的应用程序。
例如查看如何发送授权请求:
auth request
This 说要获得用户的令牌我需要发送请求
auth.sentCode#efed51d9 phone_registered:Bool phone_code_hash:string send_call_timeout:int is_password:Bool = auth.SentCode;
在什么地方?我读过这是一个 MTProto 请求,但我不确定如何发送它。可以用Postman寄吗? teletgrambots
library 通过 rubenlagus
?
电报协议
MTProto 嵌入到传输协议(TCP、HTTP、...)中,因此使用 Postman 并非不可能,但我真的很难做到(所以这不是最佳选择)。 认为您的 http 调用必须实现 MTProto 2.0 因此您的 http 消息必须具有此 structure/format: structure
- 64 位 key_fingerprint
- 128 位 mesg_key
- encrypted_data
见schematic presentation of messages
只有非常有限的特殊类型的消息可以作为纯文本传输。
TelegramBots
关于 TelegramBots 是一个帮助使用 Telegram Bots Api.[=40= 构建机器人的库]
它支持两种技术:长轮询和Webhook。
该项目有很好的教程,您可以试试:quickstart
此外,如果您是 Spring 引导用户,它也有一个 Spring 引导程序 (spring boot starter.
如何实现自己的认证客户端?
如果您想了解如何与电报api交互以进行身份验证,您需要阅读此内容:auth section
之后,您可以了解如何与 Telegram 交互以进行身份验证,所以...我建议您阅读真实应用程序如何使用 Telegram API。 查看此示例实现中 Telegram Api 的所有用法(引用包的 classes:org.telegram.api.requests.TLRequestAuth*):
正如您在上面的示例中看到的,通常电报客户端使用 TDLib。
基本上,Telegram 提供了两个API:
Telegram API 和 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.
机器人 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.
如果您想创建或使用 Telegram 作为服务来发送和接收消息并与服务器交互(不需要电话 phone 号码)...Bot API 应该是你的选择。
如果您想创建自己的定制 Telegram 应用程序(您的用户将需要一个电报phone 号码来注册,就像在手机 phone 中使用电报一样)... Telegram API/TDLib 应该是你的选择。
TDLib 是:
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.
因此您无需关心 MTProtocol 协议、网络、加密等底层细节。在这种情况下您只需要使用 TDLib。
我发送给您的示例 class 是 Android (telegram app) 的电报应用程序。它使用 TDLib。
如果您想学习如何创建自己的 TDLib...源代码在这里: tdlib source
它是用 C 语言开发的,可以与 Java、.NET 等一起使用。
基本上,它通过传输协议(http、tcp 或 udp)发送消息。例如。对于 http,使用 post 请求提交具有上述 MTProto 格式的消息。消息用于编码。
关于 MTProto 支持的传输如何工作的文本:supported transports
其他参考资料:
电报 APIs: Telegram APIs TDLib:TDLib
Java
中的 TDLib 使用示例Java 示例回购:TDLib example client
首先检查 TDLib 存储库 TDLib repo 正如我所说,TDLib 是用 C 语言开发的,因此您需要使用 JNI(Java Native Interface)与之交互。因此,您需要编译 TDLib(使用 cmake)。
按照示例说明 (readme.md) 在您的平台 (readme) 中 build/prebuild TDLib。
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/
.
- 然后你可以运行Java例子
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.
如果您想使用像 IntelliJ 这样的 IDE 打开或在您的项目中导入此示例,请查看这张票:how to use IntelliJ with TDLib
试试运行的例子!很简单,只有三个 Java classes (Example, Client and Log).
客户端:使用"native"方法与TDLib交互。
示例:使用客户端的应用程序。
例如查看如何发送授权请求: auth request