rubenlagus / TelegramApi

Java library to create Telegram Clients
MIT License
300 stars 108 forks source link

Guide to make a client! #50

Open applke opened 6 years ago

applke commented 6 years ago

Hi i want to make my first request with MTproto and Telegram api! I included your lib in my project! What should i do to make RPC request? Can you show me code which make auth by phone and code and getting username of account! Pls! I can't find this info!

sadeghpro commented 6 years ago

you can use this to starting. in this example, every message sends to your phone send back to the sender. for auth, it's so simple and the interesting part of that is saving your session in a file beside of your jar file and the second time you want to start if the file exists it use the previous session and don't need new auth code I hope it's help you

applke commented 6 years ago

Is it possible to use this lib with proxy?

applke commented 6 years ago

Okey, if i don't want to make my account bot. I want to make rpc calls. What start class i need to use ?

sadeghpro commented 6 years ago

I think you can't use a proxy in this lib. for your second question if you want to send a nonauth rpc call I have a code that checks a number is signup or not. I hope this helps you.


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.telegram;

import java.io.IOException;
import java.util.concurrent.TimeoutException;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.telegram.api.TLConfig;
import org.telegram.api.auth.TLCheckedPhone;
import org.telegram.api.engine.ApiCallback;
import org.telegram.api.engine.AppInfo;
import org.telegram.api.engine.RpcException;
import org.telegram.api.engine.TelegramApi;
import org.telegram.api.engine.storage.AbsApiState;
import org.telegram.api.functions.auth.TLRequestAuthCheckPhone;
import org.telegram.api.functions.help.TLRequestHelpGetConfig;
import org.telegram.api.updates.TLAbsUpdates;
import org.telegram.bot.kernel.IKernelComm;
import org.telegram.bot.kernel.KernelComm;
import org.telegram.bot.kernel.engine.MemoryApiState;
import org.telegram.bot.services.BotLogger;

/**
 *
 * @author sadeghpro
 */
public class NewClass {

    private static final int APIKEY = 0; // your api key
    private static final String APIHASH = ""; // your api hash

    public static void main(String[] args) {
//        Logger.getGlobal().addHandler(new ConsoleHandler());
//        Logger.getGlobal().setLevel(Level.ALL);
        AbsApiState apiState = new MemoryApiState("test.log");
//        apiState.setPrimaryDc(4);
//        IKernelComm kernelComm = new KernelComm(APIKEY, apiState);
//        kernelComm.getApi().;
        TelegramApi api = new TelegramApi(apiState, new AppInfo(APIKEY, "desc", "test", "1", "en"), new ApiCallback() {

            @Override
            public void onAuthCancelled(TelegramApi api) {
                System.err.println("onAuthCancelled");
            }

            @Override
            public void onUpdatesInvalidated(TelegramApi api) {
                System.err.println("onUpdatesInvalidated");
            }

            @Override
            public void onUpdate(TLAbsUpdates updates) {
                System.err.println("onUpdate");
            }
        });
        try {
            final TLConfig config = api.doRpcCallNonAuth(new TLRequestHelpGetConfig());
            apiState.setPrimaryDc(config.getThisDc());
            apiState.updateSettings(config);
        } catch (IOException | TimeoutException e) {

        }
        new Thread(() -> {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException ex) {
                Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
            }
            TLRequestAuthCheckPhone tl = new TLRequestAuthCheckPhone();
            tl.setPhoneNumber("989104964383");
            try {
                TLCheckedPhone t = api.doRpcCallNonAuth(tl);
                System.err.println(t.isPhoneRegistered());
            } catch (RpcException ex) {
                Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
            } catch (TimeoutException ex) {
                Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
            }
        }).start();
    }
}
applke commented 6 years ago

Thanks a lot! But in theory, if it possible to modify this lib to use with proxy?

sadeghpro commented 6 years ago

I don't know maybe possible but I work on a project and telegram ban me for 86400 seconds (FLOOD_WAIT_86400) I change phone number and api key and api hash and use Kerio and l2tp for changing IP but it does not work but in the afternoon when I back home and test it work correctly then I guess maybe IP changer not work on telegram api the second thing, I think proxy only change IP of HTTP request, not other protocols

aibeke commented 6 years ago

Hello. TLCheckedPhone#isPhoneRegistered method always returns "true". Even for any string.

soufee commented 4 years ago

where to get apiKey and apiState ?

aroaddy commented 4 years ago

hi please the app crashes on this line AbsApiState apiState = new MemoryApiState("test.log"); where do i create or access the test.log file