skip to Main Content

I am trying to get the latest messages from the Telegram API (NOT bot API). I’m currently using messages.getHistory but that returns all messages from the beginning. If I get new messages (since I sign in) that would also be fine.

My best bet so far has been to read all messages, then track the offset so I don’t read the same messages again, but this is too slow and resource costly.

2

Answers


  1. Chosen as BEST ANSWER

    Charles' answer pointed me in the right direction. For those interested in the node.js version, I managed to get it working by using telegram-link module and setting 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, changing it to TCP will give you the desired effect, and you will get the messages pushed 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 receivedMessages - if you don't call this, then Telegram will not send you any new updates. If receivedMessages is not defined in your telegram-link, 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. There is a simpler method for getting real-time updates from Telegram API.

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

    This eliminates the cost that you mentioned and you don’t get any duplicates at all.

    For my Telegram clients I have done this successfully by simply running this on start up:

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

    Then I simply process incoming data from the connected TCP socket as it arrives.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search