将 Telegram API 用于 Java 桌面应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22635355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:58:27  来源:igfitidea点击:

Using Telegram API for Java Desktop App?

javaeclipseswingapitelegram

提问by LukeLR

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.

我对 Java 编程并不陌生,但我从来没有使用过外部库等。现在我想为“电报”开源消息传递平台开发一个桌面客户端,但在 API 使用方面我陷入了困境.

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.

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

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?

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

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

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

Thanks in Advance, Lukas

提前致谢,卢卡斯

采纳答案by grkvlt

Essentially you will have to fill out the blanks in the code given on GitHub in the ex3ndr/telegram-apirepository. 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 Callssection of the README and

本质上,您必须填写 GitHub 上ex3ndr/telegram-api存储库中给出的代码中的空白。如果你有你构建的库 Jar 文件和tl-api-v12.jarEclipse 项目的 Java 构建路径上的文件,然后查看自述文件的RPC 调用部分和

First you need to set up an AppInfoobject with your API credentials, then you will also have to create some new classes that implement the AbsApiStateand ApiCallbackinterfaces. Once these are available, you can create the TelegramApiobject and make an RPC call to the Telegram service as follows; in this case using the suggested auth.checkPhonemethod:

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

// 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

The TelegramApiobject represents your connection to the remote service, which is a request response style of API. RPC calls are made via the doRpcCallmethod, which takes a request object from the org.telegram.api.requestspackage (the TLRequestAuthCheckPhonetype in the example) filled in with the appropriate parameters. A response object (TLCheckedPhoneabove) is then returned with the result when it is available.

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

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

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

// 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) { }
});

回答by red

Or just look at this API https://github.com/pengrad/java-telegram-bot-api

或者只是看看这个 API https://github.com/pengrad/java-telegram-bot-api

It is really simple to use

使用起来真的很简单