{"_id":"5703e51eaceacc17003ef336","__v":1,"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"},"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"},"githubsync":"","project":"5703d527bb69fc1700553ce0","parentDoc":null,"user":"5631f962c3b04b0d00ba9bf1","updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-04-05T16:17:34.206Z","link_external":false,"link_url":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":2,"body":"This snippet will get the 10 most recent conversations and display the title + the latest message\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// This is a synchronous client, that will block until the response arrive (or until timeout)\\nTelegramClient client = Kotlogram.getDefaultClient(application, new ApiStorage());\\n\\n// Number of recent conversation you want to get (Telegram has an internal max, your value will be capped)\\nint count = 10;\\n\\n// You can start making requests\\ntry {\\n    TLAbsDialogs tlAbsDialogs = client.messagesGetDialogs(0, 0, new TLInputPeerEmpty(), count);\\n\\n    // Map peer id to displayable string\\n    HashMap<Integer, String> nameMap = createNameMap(tlAbsDialogs);\\n\\n    // Map message id to message\\n    HashMap<Integer, TLAbsMessage> messageMap = new HashMap<>();\\n    tlAbsDialogs.getMessages().forEach(message -> messageMap.put(message.getId(), message));\\n\\n    tlAbsDialogs.getDialogs().forEach(dialog -> {\\n        System.out.print(nameMap.get(getId(dialog.getPeer())) + \\\": \\\");\\n        TLAbsMessage topMessage = messageMap.get(dialog.getTopMessage());\\n        if (topMessage instanceof TLMessage) {\\n            // The message could also be a file, a photo, a gif, ...\\n            System.out.println(((TLMessage) topMessage).getMessage());\\n        } else if (topMessage instanceof TLMessageService) {\\n            TLAbsMessageAction action = ((TLMessageService) topMessage).getAction();\\n            // action defined the type of message (user joined group, ...)\\n            System.out.println(\\\"Service message\\\");\\n        }\\n    });\\n} catch (RpcErrorException | IOException e) {\\n    e.printStackTrace();\\n} finally {\\n    client.close(); // Important, do not forget this, or your process won't finish\\n}\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\nIt sadly can become very fastidious in Java (one of the reason I did most of the code in Kotlin):\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"/**\\n * :::at:::param tlAbsDialogs result from messagesGetDialogs\\n * @return a map where the key is the peerId and the value is the chat/channel title or the user's name\\n */\\npublic static HashMap<Integer, String> createNameMap(TLAbsDialogs tlAbsDialogs) {\\n    // Map peer id to name\\n    HashMap<Integer, String> nameMap = new HashMap<>();\\n\\n    tlAbsDialogs.getUsers().stream()\\n                .map(TLAbsUser::getAsUser)\\n                .forEach(user -> nameMap.put(user.getId(),\\n                                             user.getFirstName() + \\\" \\\" + user.getLastName()));\\n\\n    tlAbsDialogs.getChats().stream()\\n                .forEach(chat -> {\\n                    if (chat instanceof TLChannel) {\\n                        nameMap.put(chat.getId(), ((TLChannel) chat).getTitle());\\n                    } else if (chat instanceof TLChannelForbidden) {\\n                        nameMap.put(chat.getId(), ((TLChannelForbidden) chat).getTitle());\\n                    } else if (chat instanceof TLChat) {\\n                        nameMap.put(chat.getId(), ((TLChat) chat).getTitle());\\n                    } else if (chat instanceof TLChatEmpty) {\\n                        nameMap.put(chat.getId(), \\\"Empty chat\\\");\\n                    } else if (chat instanceof TLChatForbidden) {\\n                        nameMap.put(chat.getId(), ((TLChatForbidden) chat).getTitle());\\n                    }\\n                });\\n\\n    return nameMap;\\n}\\n\\npublic static int getId(TLAbsPeer peer) {\\n    if (peer instanceof TLPeerUser)\\n        return ((TLPeerUser) peer).getUserId();\\n    if (peer instanceof TLPeerChat)\\n        return ((TLPeerChat) peer).getChatId();\\n\\n    return ((TLPeerChannel) peer).getChannelId();\\n}\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]","excerpt":"[See on github](https://github.com/badoualy/kotlogram/blob/master/sample/src/main/java/com/github/badoualy/telegram/sample/SampleGetDialogs.java)","slug":"get-recent-conversation-list","type":"basic","title":"Get recent conversation list"}

Get recent conversation list

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

This snippet will get the 10 most recent conversations and display the title + the latest message [block:code] { "codes": [ { "code": "// This is a synchronous client, that will block until the response arrive (or until timeout)\nTelegramClient client = Kotlogram.getDefaultClient(application, new ApiStorage());\n\n// Number of recent conversation you want to get (Telegram has an internal max, your value will be capped)\nint count = 10;\n\n// You can start making requests\ntry {\n TLAbsDialogs tlAbsDialogs = client.messagesGetDialogs(0, 0, new TLInputPeerEmpty(), count);\n\n // Map peer id to displayable string\n HashMap<Integer, String> nameMap = createNameMap(tlAbsDialogs);\n\n // Map message id to message\n HashMap<Integer, TLAbsMessage> messageMap = new HashMap<>();\n tlAbsDialogs.getMessages().forEach(message -> messageMap.put(message.getId(), message));\n\n tlAbsDialogs.getDialogs().forEach(dialog -> {\n System.out.print(nameMap.get(getId(dialog.getPeer())) + \": \");\n TLAbsMessage topMessage = messageMap.get(dialog.getTopMessage());\n if (topMessage instanceof TLMessage) {\n // The message could also be a file, a photo, a gif, ...\n System.out.println(((TLMessage) topMessage).getMessage());\n } else if (topMessage instanceof TLMessageService) {\n TLAbsMessageAction action = ((TLMessageService) topMessage).getAction();\n // action defined the type of message (user joined group, ...)\n System.out.println(\"Service message\");\n }\n });\n} catch (RpcErrorException | IOException e) {\n e.printStackTrace();\n} finally {\n client.close(); // Important, do not forget this, or your process won't finish\n}", "language": "java" } ] } [/block] It sadly can become very fastidious in Java (one of the reason I did most of the code in Kotlin): [block:code] { "codes": [ { "code": "/**\n * @param tlAbsDialogs result from messagesGetDialogs\n * @return a map where the key is the peerId and the value is the chat/channel title or the user's name\n */\npublic static HashMap<Integer, String> createNameMap(TLAbsDialogs tlAbsDialogs) {\n // Map peer id to name\n HashMap<Integer, String> nameMap = new HashMap<>();\n\n tlAbsDialogs.getUsers().stream()\n .map(TLAbsUser::getAsUser)\n .forEach(user -> nameMap.put(user.getId(),\n user.getFirstName() + \" \" + user.getLastName()));\n\n tlAbsDialogs.getChats().stream()\n .forEach(chat -> {\n if (chat instanceof TLChannel) {\n nameMap.put(chat.getId(), ((TLChannel) chat).getTitle());\n } else if (chat instanceof TLChannelForbidden) {\n nameMap.put(chat.getId(), ((TLChannelForbidden) chat).getTitle());\n } else if (chat instanceof TLChat) {\n nameMap.put(chat.getId(), ((TLChat) chat).getTitle());\n } else if (chat instanceof TLChatEmpty) {\n nameMap.put(chat.getId(), \"Empty chat\");\n } else if (chat instanceof TLChatForbidden) {\n nameMap.put(chat.getId(), ((TLChatForbidden) chat).getTitle());\n }\n });\n\n return nameMap;\n}\n\npublic static int getId(TLAbsPeer peer) {\n if (peer instanceof TLPeerUser)\n return ((TLPeerUser) peer).getUserId();\n if (peer instanceof TLPeerChat)\n return ((TLPeerChat) peer).getChatId();\n\n return ((TLPeerChannel) peer).getChannelId();\n}", "language": "java" } ] } [/block]