node.jsapinode-telegram-bot-apitdlib

How to request all chats/groups of the user through Telegram Database Library(TDLib) for Node.js


The official example from telegram explains that in order to use getChats() command, one needs to set two parameters 'offset_order' and 'offset_chat_id'. I'm using this node.js wrapper for the TDLib. So when I use getChats() with the following params:

'offset_order': '9223372036854775807',
'offset_chat_id': 0,
'limit': 100

just like it is explained in the official docs:

For example, to get a list of chats from the beginning, the offset_order should be equal to 2^63 - 1

as a result I get 100 chats from the top of the user's list.

What I can't understand is how do I iterate through that list? How do I use the API pagination?

When I try to enter a legitimate chat_id from the middle of the first 100, I still get the same first 100, so it seems like it makes no difference.

If I change that offset_order to ANY other number, I get an empty list of chats in return...

Completely lost here, as every single example I found says the same thing as the official docs, ie how to get the first 100.


Solution

  • Had the same problem, have tried different approaches and re-read documentation for a long time, and here is a decision:

    1. do getChats as you do with '9223372036854775807' offset_order parameter
    2. do getChat request with id of the last chat you have got. It's an offline request, just to tdlib.
    3. here you get a chat object with positions property - get a position from here, looks like this:
      positions: [
        {
          _: 'chatPosition',
          list: [Object],
          order: '6910658003385450706',
          is_pinned: false
        }
      ],
    
    1. next request - getChats - use the positions[0].order from (3) as offset_order
    2. goto (2) if there are more chats

    It wasn't easy to come to this, so would be glad if it helps anybody who came from google like me :)