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
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:
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:
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:
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:
Then I simply process incoming data from the connected TCP socket as it arrives.