How to get the latest/new messages from Telegram API
Asked Answered
P

2

6

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.

Phlegmy answered 3/4, 2017 at 7:21 Comment(4)
how have you setup your TCP connection? are you polling for data, or are you listening for incoming data from Telegram servers?Auten
I have no problems with my connection, and I can fully retrieve the information. This is not a chatbot (ie. there is no polling mode or webhook mode) but the way I do it is to poll the server for the first 20 messages, then move on and take the rest until there are no messages left. But the point is, I dont need the old messages - I just need the new messages, after I have signed inPhlegmy
I'm asking for a different reason david. I know it is not a chat bot. There is a simpler method for getting real-time updates from Telegram API. See the solution i propose below, it works for meAuten
can we do this using telegram bot api?Dropping
A
1

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.

Auten answered 4/4, 2017 at 6:6 Comment(5)
It could work, though I need more help from you. I'm using Telegram Link foor node.js so it seems we are aligned. Sorry for asking but I cannot find any up-to-date telegram refs on the api. So two questions; 1- how to setup non-polling mode 2- what method should I use to get the messages? If I set it up the way you said, will only new messages be pushed to messages.getHistory?Phlegmy
You actually don't even need to send any message. once your TCP socket or connection is setup in what is known as active mode, then available bytes are simply pushed to you.Auten
The latest version of Telegram with voice calls etc is Layer 65. You can get the scheme.tl here: raw.githubusercontent.com/telegramdesktop/tdesktop/dev/Telegram/…Auten
Thanks a lot, let me boil it down to some specific node.js code and get backPhlegmy
thanks for pointing me in the right direction. For those interested in the node.js code please see my answerPhlegmy
P
2

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);
};
Phlegmy answered 5/4, 2017 at 7:3 Comment(3)
Amazing thank you, the API lacks of documentation, and you are resolving a common pbMitosis
Do you know if we can receive channel msgs with node using telegram.link ? I can't get it working @DavidMitosis
@GuilhemFry I haven't been doing this for some time now, but at the time I did; yes we could, and it was nothing special about itPhlegmy
A
1

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.

Auten answered 4/4, 2017 at 6:6 Comment(5)
It could work, though I need more help from you. I'm using Telegram Link foor node.js so it seems we are aligned. Sorry for asking but I cannot find any up-to-date telegram refs on the api. So two questions; 1- how to setup non-polling mode 2- what method should I use to get the messages? If I set it up the way you said, will only new messages be pushed to messages.getHistory?Phlegmy
You actually don't even need to send any message. once your TCP socket or connection is setup in what is known as active mode, then available bytes are simply pushed to you.Auten
The latest version of Telegram with voice calls etc is Layer 65. You can get the scheme.tl here: raw.githubusercontent.com/telegramdesktop/tdesktop/dev/Telegram/…Auten
Thanks a lot, let me boil it down to some specific node.js code and get backPhlegmy
thanks for pointing me in the right direction. For those interested in the node.js code please see my answerPhlegmy

© 2022 - 2024 — McMap. All rights reserved.