Telegram API for OSINT – Part 2 – Messages

Following on from Part 1 – Users, we will be looking into the messages posted into groups/channels/user conversations and how we can extract them.

Messages

A message object in Telegram contains the following information:

message.PNG

If a message contains media, it will consist of one of the following:

Getting Messages

To query Telegram for messages, the messages.getHistory API call is needed. This requires the following:

history

The InputPeer represents a channel/group/user and requires that you are a member of that channel/group or have started a conversation with the user. This is because the InputPeer requires the ID and the access_hash associated with the Peer, which can only be provided by Telegram. Since the web version of Telegram caches the IDs and access_hashes for the channels/conversations, the AppPeersManager.getInputPeerByID() function can be queried with the ID and will provide the corresponding InputPeer. The code below will enable you to query a channel/group/user for the history. Altering the offset and offset_id will enable you to collect the history all the way back to the channel’s creation.

var id = channel_id || user_id;
var offset = 0;
var offset_id = 0;
var injector = angular.element(document).injector();
var mtpApiManager = injector.get('MtpApiManager');
var appPeersManager = injector.get('AppPeersManager');
mtpApiManager.invokeApi('messages.getHistory', {
  peer: appPeersManager.getInputPeerByID(id),
  offset_id: offset_id,
  add_offset: offset,
  limit: 20
}).then(function(data) {
  console.log(data.messages);
  console.log(data.users);
  console.log(data.chats);
});

The returned data contains the last 20 messages along with the information associated with the users who posted the messages and any information about channels/groups the messages might have been forwarded from.

There is a lot of information available, too much to describe here. However if you are interested, there is now a script on the scripts page which will collect the history from a user/group/channel over the last 24 hours. Feel free to look at the code and play around with it. If you have any suggestions, don’t hesitate to contact me.

Leave a comment