将 Telegram API 用于 Java 桌面应用程序? [英] Using Telegram API for Java Desktop App?

查看:23
本文介绍了将 Telegram API 用于 Java 桌面应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Java 编程并不陌生,但我从来没有使用过外部库等.现在我想为电报"开源消息传递平台开发一个桌面客户端,但在涉及到时我被卡住了API 使用.

I am not that new to Java Programming, but I have never worked with external libraries etc. Now I want to develop a desktop client for the "Telegram" open-source messaging platform, and I'm stuck when it comes to API-Usage.

有很多关于 Telegram API 的文档,可以在 https://core.telegram.org/api 中找到,并且我已经从 github 下载了 mtproto、telegram-api 和 tl-core,并使用 gradle 从源代码编译了我自己的库 jar.同样,我已经编写了一个小应用程序,其中用户单击一个按钮并被提示输入他的电话号码,为此我使用了 Java-swing-Libraries 和一个 ActionListener.

There is pretty much documentation about the Telegram API, found at https://core.telegram.org/api, and I've already downloaded mtproto, telegram-api and tl-core from github, and compiled my own library jar from source by using gradle. As well, I've already written a small application, where the user clicks a button and is promted to enter his phone number, I'm using the Java-swing-Libraries and an ActionListener for this.

如果用户输入的电话号码已经注册,现在应该检查它,auth.checkPhone 方法似乎能够做到这一点.但是如何在我的 eclipse 项目中引用它呢?我在任何课程中都没有看到任何方法checkPhone"!我该怎么办?

The phone number entered by the user should now be checked if it is already registered, the auth.checkPhone method seems to be capable for that. But how can I refer to it within my eclipse project? I don't see any method "checkPhone" in any of the classes! What should I do?

请帮助我,我无法自拔,我拼命地卡在我的项目中.即使是一个小小的提示也会有所帮助.

Please help me, I can't help myself and I am desperately stuck in my project. Even a small hint would help.

提前致谢,卢卡斯

推荐答案

基本上,您必须填写 GitHub 上 ex3ndr/telegram-api 存储库.如果您在 Eclipse 项目的 Java 构建路径上有您构建的库 Jar 文件和 tl-api-v12.jar 文件,请查看 RPC 调用 部分的 README 和

Essentially you will have to fill out the blanks in the code given on GitHub in the ex3ndr/telegram-api repository. If you've got the library Jar file you built and the tl-api-v12.jarfile on your Eclipse project's Java build path, then look at the RPC Calls section of the README and

首先,您需要使用您的 API 凭据设置一个 AppInfo 对象,那么您还必须创建一些实现 AbsApiStateApiCallback 接口的新类.一旦这些可用,您可以创建 TelegramApi 对象并对 Telegram 服务进行 RPC 调用,如下所示;在这种情况下,使用建议的 auth.checkPhone 方法:

First you need to set up an AppInfo object with your API credentials, then you will also have to create some new classes that implement the AbsApiState and ApiCallback interfaces. Once these are available, you can create the TelegramApi object and make an RPC call to the Telegram service as follows; in this case using the suggested auth.checkPhone method:

// TODO set up AbsApiState, AppInfo and ApiCallback objects
TelegramApi api = new TelegramApi(state, appInfo, apiCallback);

// Create request
String phoneNumber = "1234567890";
TLRequestAuthCheckPhone checkPhone = new TLRequestAuthCheckPhone(phoneNumber);

// Call service synchronously
TLCheckedPhone checkedPhone = api.doRpcCall(checkPhone);
boolean invited = checkedPhone.getPhoneInvited();
boolean registered = checkedPhone.getPhoneRegistered();
// TODO process response further

TelegramApi 对象表示您与远程服务的连接,这是 API 的请求响应样式.RPC 调用通过 doRpcCall 方法进行,该方法从 org.telegram.api.requests 包中获取一个请求对象(TLRequestAuthCheckPhone 输入示例)填充了适当的参数.当结果可用时,响应对象(上面的TLCheckedPhone)将与结果一起返回.

The TelegramApi object represents your connection to the remote service, which is a request response style of API. RPC calls are made via the doRpcCall method, which takes a request object from the org.telegram.api.requests package (the TLRequestAuthCheckPhone type in the example) filled in with the appropriate parameters. A response object (TLCheckedPhone above) is then returned with the result when it is available.

在异步调用的情况下,方法立即返回,当结果可用时执行onResult回调方法:

In the case of an asynchronous call the method returns immediately, and the onResult callback method is executed when the result is available:

// Call service aynchronously
api.doRpcCall(checkPhone, new RpcCallbackEx<TLCheckedPhone>() {
    public void onConfirmed() { }
    public void onResult(TLCheckedPhone result) {
        boolean invited = checkedPhone.getPhoneInvited();
        boolean registered = checkedPhone.getPhoneRegistered();
        // TODO process response further
    }
    public void onError(int errorCode, String message) { }
});

这篇关于将 Telegram API 用于 Java 桌面应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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