{"_id":"5703e53399543c2900e9db91","githubsync":"","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"},"__v":0,"parentDoc":null,"project":"5703d527bb69fc1700553ce0","user":"5631f962c3b04b0d00ba9bf1","updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-04-05T16:17:55.750Z","link_external":false,"link_url":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":3,"body":"This snippet will get the most recent conversation and print the last 10 messages\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// How many messages we want to get (same than dialogs, there is a cap)\\nint count = 10;\\n\\n// You can start making requests\\ntry {\\n    TLAbsDialogs tlAbsDialogs = client.messagesGetDialogs(0, 0, new TLInputPeerEmpty(), 1);\\n    TLAbsInputPeer inputPeer = getInputPeer(tlAbsDialogs);\\n\\n    TLAbsMessages tlAbsMessages = client.messagesGetHistory(inputPeer, 0, 0, 0, count, 0, 0);\\n    tlAbsMessages.getMessages().forEach(message -> {\\n        if (message instanceof TLMessage)\\n            System.out.println(((TLMessage) message).getMessage());\\n        else\\n            System.out.println(\\\"Service message\\\");\\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]\nAlways very long with all those constructors...\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"/**\\n * Get the first peer and return it as an InputPeer to use with methods\\n */\\npublic static TLAbsInputPeer getInputPeer(TLAbsDialogs tlAbsDialogs) {\\n    TLAbsPeer tlAbsPeer = tlAbsDialogs.getDialogs().get(0).getPeer();\\n    int peerId = getId(tlAbsPeer);\\n    TLObject peer = tlAbsPeer instanceof TLPeerUser ?\\n            tlAbsDialogs.getUsers().stream().filter(user -> user.getId() == peerId).findFirst().get()\\n            : tlAbsDialogs.getChats().stream().filter(chat -> chat.getId() == peerId).findFirst().get();\\n\\n    if (peer instanceof TLChannel)\\n        return new TLInputPeerChannel(((TLChannel) peer).getId(), ((TLChannel) peer).getAccessHash());\\n    if (peer instanceof TLChat)\\n        return new TLInputPeerChat(((TLChat) peer).getId());\\n    if (peer instanceof TLUser)\\n        return new TLInputPeerUser(((TLUser) peer).getId(), ((TLUser) peer).getAccessHash());\\n\\n    return new TLInputPeerEmpty();\\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/SampleGetHistory.java)","slug":"get-messages-from-a-conversation","type":"basic","title":"Get messages from a conversation"}

Get messages from a conversation

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

This snippet will get the most recent conversation and print the last 10 messages [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// How many messages we want to get (same than dialogs, there is a cap)\nint count = 10;\n\n// You can start making requests\ntry {\n TLAbsDialogs tlAbsDialogs = client.messagesGetDialogs(0, 0, new TLInputPeerEmpty(), 1);\n TLAbsInputPeer inputPeer = getInputPeer(tlAbsDialogs);\n\n TLAbsMessages tlAbsMessages = client.messagesGetHistory(inputPeer, 0, 0, 0, count, 0, 0);\n tlAbsMessages.getMessages().forEach(message -> {\n if (message instanceof TLMessage)\n System.out.println(((TLMessage) message).getMessage());\n else\n System.out.println(\"Service message\");\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] Always very long with all those constructors... [block:code] { "codes": [ { "code": "/**\n * Get the first peer and return it as an InputPeer to use with methods\n */\npublic static TLAbsInputPeer getInputPeer(TLAbsDialogs tlAbsDialogs) {\n TLAbsPeer tlAbsPeer = tlAbsDialogs.getDialogs().get(0).getPeer();\n int peerId = getId(tlAbsPeer);\n TLObject peer = tlAbsPeer instanceof TLPeerUser ?\n tlAbsDialogs.getUsers().stream().filter(user -> user.getId() == peerId).findFirst().get()\n : tlAbsDialogs.getChats().stream().filter(chat -> chat.getId() == peerId).findFirst().get();\n\n if (peer instanceof TLChannel)\n return new TLInputPeerChannel(((TLChannel) peer).getId(), ((TLChannel) peer).getAccessHash());\n if (peer instanceof TLChat)\n return new TLInputPeerChat(((TLChat) peer).getId());\n if (peer instanceof TLUser)\n return new TLInputPeerUser(((TLUser) peer).getId(), ((TLUser) peer).getAccessHash());\n\n return new TLInputPeerEmpty();\n}", "language": "java" } ] } [/block]