Get messages from a conversation
This snippet will get the most recent conversation and print the last 10 messages
// This is a synchronous client, that will block until the response arrive (or until timeout)
TelegramClient client = Kotlogram.getDefaultClient(application, new ApiStorage());
// How many messages we want to get (same than dialogs, there is a cap)
int count = 10;
// You can start making requests
try {
TLAbsDialogs tlAbsDialogs = client.messagesGetDialogs(0, 0, new TLInputPeerEmpty(), 1);
TLAbsInputPeer inputPeer = getInputPeer(tlAbsDialogs);
TLAbsMessages tlAbsMessages = client.messagesGetHistory(inputPeer, 0, 0, 0, count, 0, 0);
tlAbsMessages.getMessages().forEach(message -> {
if (message instanceof TLMessage)
System.out.println(((TLMessage) message).getMessage());
else
System.out.println("Service message");
});
} catch (RpcErrorException | IOException e) {
e.printStackTrace();
} finally {
client.close(); // Important, do not forget this, or your process won't finish
}
Always very long with all those constructors...
/**
* Get the first peer and return it as an InputPeer to use with methods
*/
public static TLAbsInputPeer getInputPeer(TLAbsDialogs tlAbsDialogs) {
TLAbsPeer tlAbsPeer = tlAbsDialogs.getDialogs().get(0).getPeer();
int peerId = getId(tlAbsPeer);
TLObject peer = tlAbsPeer instanceof TLPeerUser ?
tlAbsDialogs.getUsers().stream().filter(user -> user.getId() == peerId).findFirst().get()
: tlAbsDialogs.getChats().stream().filter(chat -> chat.getId() == peerId).findFirst().get();
if (peer instanceof TLChannel)
return new TLInputPeerChannel(((TLChannel) peer).getId(), ((TLChannel) peer).getAccessHash());
if (peer instanceof TLChat)
return new TLInputPeerChat(((TLChat) peer).getId());
if (peer instanceof TLUser)
return new TLInputPeerUser(((TLUser) peer).getId(), ((TLUser) peer).getAccessHash());
return new TLInputPeerEmpty();
}
Updated less than a minute ago
Did this page help you?