telegram-s / telegram-api-old

Telegram Api library for java
MIT License
138 stars 64 forks source link

How to do basic RPC #12

Open brain56 opened 10 years ago

brain56 commented 10 years ago

Hi! First and foremost, I'm new here. So if this is the wrong place to be writing this, please guide me accordingly. Please be kind!

How do I make a basic remote procedure call in Telegram? I've been searching for a tutorial for quite a while now but they all seem to be insufficient for beginners, even the readme on the Github page.

Please provide a guide from set up to finish on how to initialize a TelegramApi instance, how to use the doRpcCall method and how to properly extend from TLMethod. Please provide code on how to make an auth.sendCode call from start to finish. Thank you very much! I'm sure this will be helping a lot of beginners.

P.S.

I also posted a question in StackOverflow: http://stackoverflow.com/questions/23055525/how-to-do-a-basic-remote-procedure-call-rpc-in-telegram

brain56 commented 10 years ago

I tried the sample works of @SireInsectus (https://github.com/SireInsectus/telegram-api-samples) but they end with UNKNOWN_ERROR for me.

SireInsectus commented 10 years ago

I haven't had time to get follow up on the samples - the main problem being that registration wasn't even working from the server's end... I'm going on vacation for a week and can look into it when I get back.

If you want to give me a friendly poke, I can take another look at it then.

-/\/---------/\/\ //\/\UNCHIE-//\/\ONSTER.com Jacob-D-Parr--|--559-640-7277 General-Manager-/-Owner www.MunchieMonster.com http://www.munchiemonster.com/

On Mon, Apr 14, 2014 at 4:45 AM, Brian Lee notifications@github.com wrote:

I tried the sample works of @SireInsectushttps://github.com/SireInsectus( https://github.com/SireInsectus/telegram-api-samples) but they end with UNKNOWN_ERROR for me.

— Reply to this email directly or view it on GitHubhttps://github.com/ex3ndr/telegram-api/issues/12#issuecomment-40357239 .

brain56 commented 10 years ago

@SireInsectus , thanks! I would love to see a follow up on your work. Please, do so. And if possible, update us here, via this thread for others to see. Thanks!

saturngod commented 10 years ago

@brain56 , you can use like this

private static void login() throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                System.in));
        System.out.print("Loading fresh DC list...");
        TLConfig config = api.doRpcCallNonAuth(new TLRequestHelpGetConfig());
        apiState.updateSettings(config);
        System.out.println("completed.");
        System.out.print("Phone number for bot:");
        String phone = reader.readLine();
        System.out.print("Sending sms to phone " + phone + "...");
        TLSentCode sentCode;
        try {
            sentCode = api.doRpcCallNonAuth(new TLRequestAuthSendCode(phone, 0,
                    5, "1c5c96d5edd401b1ed40db3fb5633e2d", "en"));
        } catch (RpcException e) {
            if (e.getErrorCode() == 303) {
                int destDC;
                if (e.getErrorTag().startsWith("NETWORK_MIGRATE_")) {
                    destDC = Integer.parseInt(e.getErrorTag().substring(
                            "NETWORK_MIGRATE_".length()));
                } else if (e.getErrorTag().startsWith("PHONE_MIGRATE_")) {
                    destDC = Integer.parseInt(e.getErrorTag().substring(
                            "PHONE_MIGRATE_".length()));
                } else if (e.getErrorTag().startsWith("USER_MIGRATE_")) {
                    destDC = Integer.parseInt(e.getErrorTag().substring(
                            "USER_MIGRATE_".length()));
                } else {
                    throw e;
                }
                api.switchToDc(destDC);
                sentCode = api.doRpcCallNonAuth(new TLRequestAuthSendCode(
                        phone, 0, 5, "1c5c96d5edd401b1ed40db3fb5633e2d", "en"));
            } else {
                throw e;
            }
        }
        System.out.println("sent.");
        System.out.print("Activation code:");
        String code = reader.readLine();
        TLAuthorization auth = api.doRpcCallNonAuth(new TLRequestAuthSignIn(
                phone, sentCode.getPhoneCodeHash(), code));
        apiState.setAuthenticated(apiState.getPrimaryDc(), true);
        System.out.println("Activation complete.");
        System.out.print("Loading initial state...");
        TLState state = api.doRpcCall(new TLRequestUpdatesGetState());
        System.out.println("loaded.");
    }

code from https://github.com/yarneo/telegram-trivia-bot/blob/master/src/org/telegram/bot/Application.java

brain56 commented 10 years ago

@saturngod, I'm sorry, but I'm having a java.lang.NullPointerException error in the api.switchToDc(destDC); call inside the catch clause of the function.

I initialized api and apiState like so:

ConnectionInfo[] connections = new ConnectionInfo[]{
    new ConnectionInfo(1, 0, "173.240.5.253", 443)
};
SampleApiState apiState = new SampleApiState(connections);
TelegramApi api = new TelegramApi(apiState, new SampleAppInfo(), new SampleApiCallback());

SampleAppInfo and SampleApiCallback are defined here: https://github.com/SireInsectus/telegram-api-samples/tree/master/src/main/java/org/telegram/api/samples/support

Can you please tell me what I could be doing wrong? Thanks!

saturngod commented 10 years ago

@brain56 , try to add TLConfig for apiState after init the state like following

TLConfig config = api.doRpcCallNonAuth(new TLRequestHelpGetConfig());
apiState.updateSettings(config);
brain56 commented 10 years ago

@saturngod

I did what you told me, so that now my intialization code looks like:

ConnectionInfo[] connections = new ConnectionInfo[]{
     new ConnectionInfo(1, 0, "173.240.5.1", 443)
};
SampleApiState apiState = new SampleApiState(connections);
TelegramApi api = new TelegramApi(apiState, new SampleAppInfo(), new SampleApiCallback());

TLConfig config = api.doRpcCallNonAuth(new TLRequestHelpGetConfig());
apiState.updateSettings(config);

The problem now is that I timeout at:

TLConfig config = api.doRpcCallNonAuth(new TLRequestHelpGetConfig());

Any thoughts on what's happening? Is my connection to 173.240.5.1:443 in order?

saturngod commented 10 years ago

I tested now and time out problem , too.

brain56 commented 10 years ago

Do you think it's a problem with our code or with the servers?

saturngod commented 10 years ago

no idea . Look like we miss something in the code. only @ex3ndr can answer this question.

saturngod commented 10 years ago

@brain56 , because of the layer12 api. I tested with layer 11 api and it's working fine.

You can download at http://cl.ly/V9it/download/telegram-v11.zip and try again. API key from my.telegram.org is not working. Please use other api key and hash code.

brain56 commented 10 years ago

@saturngod Thanks for the help! But how do I get another API key and hash code if I'm not going to use the one provided in my.telegram.org? There was an issue about that and the answer to the question was to get the API key and hash code from my.telegram.org.

saturngod commented 10 years ago

@brain56 , now I am using APPI ID and hash code from https://github.com/SireInsectus/telegram-api-samples/blob/master/src/main/java/org/telegram/api/samples/support/SampleAppInfo.java. from It always response not authorise when I use app id and hash from http://my.telegram.org ,

brain56 commented 10 years ago

@saturngod, thanks! I'll give it a shot. I also tweeted to Telegram weeks ago for some support, but they haven't replied yet.

brain56 commented 10 years ago

@saturngod , did you use the the same IP as the Telegram Samples project (109.239.131.195)?

saturngod commented 10 years ago

@brain56 no . 109.239.131.195 for register the developer account and it's not require anymore.

tonymitcher commented 10 years ago

Hi!@saturngod, @ex3ndr I test the telegram-api-samples with telegram-api-1.1.127-shadow.jar , but I get a problem, the problem is that I timeout at: TLState state = api.doRpcCall(new TLRequestUpdatesGetState());

ilanta commented 10 years ago

i have this problem: i get this Error: " org.telegram.api.engine.RpcException: PHONE_NUMBER_UNOCCUPIED" when i try : "TLAuthorization auth = api.doRpcCallNonAuth(new TLRequestAuthSignIn( phone, sentCode.getPhoneCodeHash(), code));"

ashokcoolguys commented 9 years ago

@ilanta PHONE_NUMBER_UNOCCUPIED means that the number is not registered. Though we have the app installed and signed up in our mobiles ,it will ask us for signup from application also.. Try this once .... and later ur code itself work from the second time TLAuthorization auth = api.doRpcCallNonAuth(new TLRequestAuthSignUp( phone, sentCode.getPhoneCodeHash(), code,"FirstName","Lastname"));

ashokcoolguys commented 9 years ago

@saturngod / @brain56 , Have you tried sending messages ..I have registered my application. But I ve tried to retrieve the user/chat id for sending messages .but I couldn't. Can you share me some ideas over this .. Plz share some samples also

ilanta commented 9 years ago

thanks ashokcoolguys !

Raakh commented 8 years ago

Hi can anybody send me compiled working code? I shall be thankful to you from the bottom of my heart. My email is ghayel@gmail.com

starbug1 commented 8 years ago

Hi. I will very thank to who that share a working code with me. i try to write it myself. but i need a good man's help. this is my email address programbug0@gmail.com