How to get latest / new messages from Telegram API

I am trying to get the latest messages from Telegram API ( NOT bot API ). I am currently using messages.getHistory, but it returns all messages from the beginning. If I get new messages (since I log in) that will be fine too.

My best bet so far has been to read all the posts and then track the offset so I don't read the same posts again, but it's too slow and resource expensive.

+3


source to share


2 answers


There is an easier way to get real-time updates from the Telegram API.

If you set your TCP connection to non-polling, then as soon as there are updates for your telegram account, the messages will simply be pushed to you.

This removes the cost you mentioned and you have no duplicates at all.



For my Telegram clients, I have done this successfully by simply executing this on startup:

TL.invokewithlayer(layer, TL.initconnection(app_id, device_model, system_version, app_version, lang_code, TL.help_getconfig))

      

Then I just process the incoming data from the connected TCP socket as it comes in.

+1


source


Charles' answer pointed me in the right direction. For those interested in the node.js version, I managed to get it to work using the telegram module and setting the connectionType to TCP:

var telegramLink = require('telegram.link')();

// set the  environment
var app = {
    // NOTE: if you FORK the project you MUST use your APP ID.
    // Otherwise YOUR APPLICATION WILL BE BLOCKED BY TELEGRAM
    // You can obtain your own APP ID for your application here: https://my.telegram.org
    id: 12345,
    hash: 'somehashcode',
    version: require('../package.json').version,
    lang: 'en',
    deviceModel: os.type().replace('Darwin', 'OS_X'),
    systemVersion: os.platform() + '/' + os.release(),
    connectionType: 'TCP'
};

//var primaryDC = telegramLink.TEST_PRIMARY_DC;
var primaryDC = telegramLink.PROD_PRIMARY_DC;

...

telegramLink.createClient(app, dataCenter, function() {
...

      

The simple point is that changing it to TCP will give you the effect you want and you will get messages to be sent to you in registerOnUpdates:



clientProxy.getClient().account.updateStatus(false).then(function() {
    clientProxy.getClient().registerOnUpdates(function(update) {
        console.log('update', update.toPrintable());

        clientProxy.getClient().messages.receivedMessages(update.id, function(err) { console.log(err); });
    });
...

      

Pay attention to the messages you receive - if you don't, Telegram won't send you any new updates. If received messages are not defined in your telegram, add the following code to lib / api / messages.js:

// ***
// messages.**receivedMessages(max_id, [callback])**

// Return a Promise to Confirms receipt of messages by a client, cancels PUSH-notification sending.

// [Click here for more details](https://core.telegram.org/method/messages.receivedMessages)
Messages.prototype.receivedMessages = function(max_id, callback) {
    return utility.callService(api.service.messages.receivedMessages, this.client, this.client._channel, callback, arguments);
};

      

+2


source







All Articles