Get started

See all samples on github

📘

Open source, participate!

I decided to implement a few simple example so that you can easily get started.
But I would like to remember that this is an open source project, so if you spent some time trying to do something specific, and you think it might help other, please, submit a snippet following the samples format by email (or by opening a ticket on github).

Before starting, you'll need to define some variables and implement some classes:

// Get them from Telegram's console
public static final int API_ID = 0;
public static final String API_HASH = "<YOUR_HASH_HERE>";

// What you want to appear in the "all sessions" screen
public static final String APP_VERSION = "AppVersion";
public static final String MODEL = "Model";
public static final String SYSTEM_VERSION = "SysVer";
public static final String LANG_CODE = "en";

public static TelegramApp application = new TelegramApp(API_ID, API_HASH, MODEL, SYSTEM_VERSION, APP_VERSION, LANG_CODE);

// Phone number used for tests
public static final String PHONE_NUMBER = "+00000000000"; // International format

A simple implementation of TelegramApiStorage to persist the connection (not having to ask a code at each launch).

Note : You don't need to create the auth.key and dc.save manually, it'll be automatically generated

📘

Commons io

The following implementation is using Apache's Commons IO to easily read/write files.

compile 'commons-io:commons-io:2.5'
public static class ApiStorage implements TelegramApiStorage {

   //Create File variable for auth.key and dc.save
    public static final File AUTH_KEY_FILE = new File("Properties/auth.key");
    public static final File NEAREST_DC_FILE = new File("Properties/dc.save");
  
    @Override
    public void saveAuthKey(@NotNull AuthKey authKey) {
        try {
            FileUtils.writeByteArrayToFile(AUTH_KEY_FILE, authKey.getKey());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Nullable
    @Override
    public AuthKey loadAuthKey() {
        try {
            return new AuthKey(FileUtils.readFileToByteArray(AUTH_KEY_FILE));
        } catch (IOException e) {
            if (!(e instanceof FileNotFoundException))
                e.printStackTrace();
        }

        return null;
    }

    @Override
    public void saveDc(@NotNull DataCenter dataCenter) {
        try {
            FileUtils.write(NEAREST_DC_FILE, dataCenter.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Nullable
    @Override
    public DataCenter loadDc() {
        try {
            String[] infos = FileUtils.readFileToString(NEAREST_DC_FILE).split(":");
            return new DataCenter(infos[0], Integer.parseInt(infos[1]));
        } catch (IOException e) {
            if (!(e instanceof FileNotFoundException))
                e.printStackTrace();
        }

        return null;
    }

    @Override
    public void deleteAuthKey() {
        try {
            FileUtils.forceDelete(AUTH_KEY_FILE);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteDc() {
        try {
            FileUtils.forceDelete(NEAREST_DC_FILE);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void saveSession(@Nullable MTSession session) {

    }

    @Nullable
    @Override
    public MTSession loadSession() {
        return null;
    }
}

Did this page help you?