{"_id":"57e3cf3e4d659c0e0006e93b","version":{"_id":"5703d527bb69fc1700553ce3","hasDoc":true,"__v":6,"hasReference":true,"project":"5703d527bb69fc1700553ce0","createdAt":"2016-04-05T15:09:27.620Z","releaseDate":"2016-04-05T15:09:27.620Z","categories":["5703d527bb69fc1700553ce4","5703d8b7aceacc17003ef303","5703e60b6116142000db25f6","57e0db616a1c2e0e0081fe64","57e0f141ff540c22007b45fa","57e0f14b8929550e00f1d9bc"],"is_deprecated":false,"is_hidden":false,"is_beta":true,"is_stable":true,"codename":"","version_clean":"0.0.6","version":"0.0.6"},"__v":1,"project":"5703d527bb69fc1700553ce0","user":"5631f962c3b04b0d00ba9bf1","githubsync":"","parentDoc":null,"category":{"_id":"5703d8b7aceacc17003ef303","version":"5703d527bb69fc1700553ce3","__v":0,"project":"5703d527bb69fc1700553ce0","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-04-05T15:24:39.108Z","from_sync":false,"order":1,"slug":"samples","title":"Samples"},"updates":["59e1ff2759d941003015fdd2"],"next":{"pages":[],"description":""},"createdAt":"2016-09-22T12:31:58.907Z","link_external":false,"link_url":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":0,"body":"[block:callout]\n{\n  \"type\": \"info\",\n  \"title\": \"Open source, participate!\",\n  \"body\": \"I decided to implement a few **simple** example so that you can easily get started.\\nBut 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).\"\n}\n[/block]\nBefore starting, you'll need to define some variables and implement some classes:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// Get them from Telegram's console\\npublic static final int API_ID = 0;\\npublic static final String API_HASH = \\\"<YOUR_HASH_HERE>\\\";\\n\\n// What you want to appear in the \\\"all sessions\\\" screen\\npublic static final String APP_VERSION = \\\"AppVersion\\\";\\npublic static final String MODEL = \\\"Model\\\";\\npublic static final String SYSTEM_VERSION = \\\"SysVer\\\";\\npublic static final String LANG_CODE = \\\"en\\\";\\n\\npublic static TelegramApp application = new TelegramApp(API_ID, API_HASH, MODEL, SYSTEM_VERSION, APP_VERSION, LANG_CODE);\\n\\n// Phone number used for tests\\npublic static final String PHONE_NUMBER = \\\"+00000000000\\\"; // International format\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\nA simple implementation of `TelegramApiStorage` to persist the connection (not having to ask a code at each launch). \n\nNote : You don't need to create the auth.key and dc.save manually, it'll be automatically generated\n[block:callout]\n{\n  \"type\": \"info\",\n  \"title\": \"Commons io\",\n  \"body\": \"The following implementation is using Apache's Commons IO to easily read/write files.\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"compile 'commons-io:commons-io:2.5'\",\n      \"language\": \"groovy\",\n      \"name\": \"Gradle\"\n    }\n  ]\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"public static class ApiStorage implements TelegramApiStorage {\\n\\n   //Create File variable for auth.key and dc.save\\n    public static final File AUTH_KEY_FILE = new File(\\\"Properties/auth.key\\\");\\n    public static final File NEAREST_DC_FILE = new File(\\\"Properties/dc.save\\\");\\n  \\n    :::at:::Override\\n    public void saveAuthKey(@NotNull AuthKey authKey) {\\n        try {\\n            FileUtils.writeByteArrayToFile(AUTH_KEY_FILE, authKey.getKey());\\n        } catch (IOException e) {\\n            e.printStackTrace();\\n        }\\n    }\\n\\n    @Nullable\\n    @Override\\n    public AuthKey loadAuthKey() {\\n        try {\\n            return new AuthKey(FileUtils.readFileToByteArray(AUTH_KEY_FILE));\\n        } catch (IOException e) {\\n            if (!(e instanceof FileNotFoundException))\\n                e.printStackTrace();\\n        }\\n\\n        return null;\\n    }\\n\\n    @Override\\n    public void saveDc(@NotNull DataCenter dataCenter) {\\n        try {\\n            FileUtils.write(NEAREST_DC_FILE, dataCenter.toString());\\n        } catch (IOException e) {\\n            e.printStackTrace();\\n        }\\n    }\\n\\n    @Nullable\\n    @Override\\n    public DataCenter loadDc() {\\n        try {\\n            String[] infos = FileUtils.readFileToString(NEAREST_DC_FILE).split(\\\":\\\");\\n            return new DataCenter(infos[0], Integer.parseInt(infos[1]));\\n        } catch (IOException e) {\\n            if (!(e instanceof FileNotFoundException))\\n                e.printStackTrace();\\n        }\\n\\n        return null;\\n    }\\n\\n    @Override\\n    public void deleteAuthKey() {\\n        try {\\n            FileUtils.forceDelete(AUTH_KEY_FILE);\\n        } catch (IOException e) {\\n            e.printStackTrace();\\n        }\\n    }\\n\\n    @Override\\n    public void deleteDc() {\\n        try {\\n            FileUtils.forceDelete(NEAREST_DC_FILE);\\n        } catch (IOException e) {\\n            e.printStackTrace();\\n        }\\n    }\\n\\n    @Override\\n    public void saveSession(@Nullable MTSession session) {\\n\\n    }\\n\\n    @Nullable\\n    @Override\\n    public MTSession loadSession() {\\n        return null;\\n    }\\n}\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]","excerpt":"See all samples [on github](https://github.com/badoualy/kotlogram/tree/master/sample/src/main/java/com/github/badoualy/telegram/sample)","slug":"get-started","type":"basic","title":"Get started"}

Get started

See all samples [on github](https://github.com/badoualy/kotlogram/tree/master/sample/src/main/java/com/github/badoualy/telegram/sample)

[block:callout] { "type": "info", "title": "Open source, participate!", "body": "I decided to implement a few **simple** example so that you can easily get started.\nBut 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)." } [/block] Before starting, you'll need to define some variables and implement some classes: [block:code] { "codes": [ { "code": "// Get them from Telegram's console\npublic static final int API_ID = 0;\npublic static final String API_HASH = \"<YOUR_HASH_HERE>\";\n\n// What you want to appear in the \"all sessions\" screen\npublic static final String APP_VERSION = \"AppVersion\";\npublic static final String MODEL = \"Model\";\npublic static final String SYSTEM_VERSION = \"SysVer\";\npublic static final String LANG_CODE = \"en\";\n\npublic static TelegramApp application = new TelegramApp(API_ID, API_HASH, MODEL, SYSTEM_VERSION, APP_VERSION, LANG_CODE);\n\n// Phone number used for tests\npublic static final String PHONE_NUMBER = \"+00000000000\"; // International format", "language": "java" } ] } [/block] 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 [block:callout] { "type": "info", "title": "Commons io", "body": "The following implementation is using Apache's Commons IO to easily read/write files." } [/block] [block:code] { "codes": [ { "code": "compile 'commons-io:commons-io:2.5'", "language": "groovy", "name": "Gradle" } ] } [/block] [block:code] { "codes": [ { "code": "public static class ApiStorage implements TelegramApiStorage {\n\n //Create File variable for auth.key and dc.save\n public static final File AUTH_KEY_FILE = new File(\"Properties/auth.key\");\n public static final File NEAREST_DC_FILE = new File(\"Properties/dc.save\");\n \n @Override\n public void saveAuthKey(@NotNull AuthKey authKey) {\n try {\n FileUtils.writeByteArrayToFile(AUTH_KEY_FILE, authKey.getKey());\n } catch (IOException e) {\n e.printStackTrace();\n }\n }\n\n @Nullable\n @Override\n public AuthKey loadAuthKey() {\n try {\n return new AuthKey(FileUtils.readFileToByteArray(AUTH_KEY_FILE));\n } catch (IOException e) {\n if (!(e instanceof FileNotFoundException))\n e.printStackTrace();\n }\n\n return null;\n }\n\n @Override\n public void saveDc(@NotNull DataCenter dataCenter) {\n try {\n FileUtils.write(NEAREST_DC_FILE, dataCenter.toString());\n } catch (IOException e) {\n e.printStackTrace();\n }\n }\n\n @Nullable\n @Override\n public DataCenter loadDc() {\n try {\n String[] infos = FileUtils.readFileToString(NEAREST_DC_FILE).split(\":\");\n return new DataCenter(infos[0], Integer.parseInt(infos[1]));\n } catch (IOException e) {\n if (!(e instanceof FileNotFoundException))\n e.printStackTrace();\n }\n\n return null;\n }\n\n @Override\n public void deleteAuthKey() {\n try {\n FileUtils.forceDelete(AUTH_KEY_FILE);\n } catch (IOException e) {\n e.printStackTrace();\n }\n }\n\n @Override\n public void deleteDc() {\n try {\n FileUtils.forceDelete(NEAREST_DC_FILE);\n } catch (IOException e) {\n e.printStackTrace();\n }\n }\n\n @Override\n public void saveSession(@Nullable MTSession session) {\n\n }\n\n @Nullable\n @Override\n public MTSession loadSession() {\n return null;\n }\n}", "language": "java" } ] } [/block]