Using Telegram API for Java Desktop App?

29,271

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

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

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.

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) { }
});
Share:
29,271

Related videos on Youtube

LukeLR
Author by

LukeLR

Full-time computer enthusiast and computer science student in Duesseldorf, Germany. Engaged for digital rights, privacy and an open+free internet. Interested in Raspberry Pi projects, general Linux and coding in various languages. Fixing computers of others in my freetime. Power user. Also interested in graphic design, photography, typography, and nearly everything computer-related.

Updated on July 09, 2022

Comments

  • LukeLR
    LukeLR almost 2 years

    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.

    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.

    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.

    Thanks in Advance, Lukas

  • LukeLR
    LukeLR over 9 years
    Hey, sorry that it's been a while since my last reply, I gave up working on this project until I saw that someone has answered my old thread. I think, I understood the most of how to get started with this project, but now I'm stuck at the "apiStorage"-point. In the readme.md of ex3ndr's-gitHub-page it says, the "Telegram-Api"-Object requires an "ApiStorage"-object, created by "new MyApiStorage". But I can't find that "MyApiStorage"-class, so what should I put there?
  • LukeLR
    LukeLR over 9 years
    Furthermore, I am unable to build the libraries with gradle. I downloaded the resources from GitHub and renamed the folders as told in the readme, but gradle always fails with an '* What went wrong: Could not determine the dependencies of task ':test'. > Configuration with name 'default' not found.' I don't know how to fix this, can you help? Right now I have copied the raw resource files into my eclipse project, but there must be a way to fix that gradle problem. I would appreciate any answers, Thanks in advance, Lukas
  • grkvlt
    grkvlt over 9 years
    The MyApiStorage bit is the complicated part. You'll need to make a class that implements the org.telegram.api.engine.storage.AbsApiState interface, and use that as the first argument to the constructor. Your IDE should be able to help build a skeleton class to get you started...
  • LukeLR
    LukeLR over 9 years
    Thanks for all your help, but I think I'll give up here, since I don't want to ask you every single step from now on... I can't find any manual that tells me what the methods within my ApiStorage should do, or what any structure in my application should do, and therefore Telegram will have to live without my furious new client... I don't know how to understand all this stuff, if it was explained somewhere, but the API Docs on telegram.org didn't even help me out a single second here... I'm very grateful that you took the time to answer my questions, thanks ;)
  • ndomanyo
    ndomanyo over 9 years
    hello grkvlt! i have been able to figure this out but i'm stuck here on how to send and receive messages. please advise how to proceed. LukeLR, check the telegram-bot sample app! it resolves some of these issues of urs!
  • Raul Luna
    Raul Luna about 9 years
    Hello, I've read and tried to implement the example given here and unfortunately it doesn't work. It says also that the only thing I have to do is to fullfill the blanks in the example application but id doesn't seem to have any "blanks" to fullfill. The example in command line is in C, so I'm afraid it doesn't match for the cases I need. I solved the problem of the AbsApiState, but I get an TimeoutException. Again, it seems that one of these timeouts is caused by Linux (java must use /dev/urandom instead of /dev/random), but after that I get another Timeout. Sorry but this is too much for me
  • slfan
    slfan over 7 years
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes