使用td api获得前200个聊天记录

时间:2016-12-27 18:27:23

标签: java android api telegram

我正在使用TdApi获得前200个聊天但我无法获得,我只是获得前100个聊天。这是我的代码,我试图获得200次聊天但不成功。

即使我在here

阅读文档
  

以正确的顺序返回聊天列表,聊天按降序排序(order,chatId)。例如,要从头开始获取聊天列表,offsetOrder应该等于2 ^ 63 - 1。

TdApi.GetChats getChats = new TdApi.GetChats(Long.MAX_VALUE, 0, 200);
TG.getClientInstance().send(getChats, new Client.ResultHandler() {
    @Override
    public void onResult(TdApi.TLObject object) {
    TdApi.Chats chat = (TdApi.Chats) object;
    final TdApi.Chat[] chats2 = chat.chats;
    Log.e("test", "2 " + chats2.length + "");
    Log.e("test", "2 " + chats2[0].title + "");
});

即使我尝试过这个:

TdApi.GetChats getchats1 = new TdApi.GetChats(Long.MAX_VALUE, 0, 100);
TG.getClientInstance().send(getchats1, new Client.ResultHandler() {
     @Override
     public void onResult(TdApi.TLObject object) {
     TdApi.Chats chat = (TdApi.Chats) object;
     final TdApi.Chat[] chats2 = chat.chats;
     Log.e("OkHttp hey", "2 " + chats2.length + "");
     Log.e("OkHttp hey", "2 " + chats2[0].title + "");

     TdApi.GetChats getChats2 = new TdApi.GetChats(0, chats2[99].id, 100);
     TG.getClientInstance().send(getChats2, new Client.ResultHandler() {
         @Override
         public void onResult(TdApi.TLObject object) {
         TdApi.Chats chat = (TdApi.Chats) object;
         final TdApi.Chat[] chats3 = chat.chats;
         Log.e("OkHttp hey", "3 " + chats3.length + "");
         Log.e("OkHttp hey", "2 " + chats3[0].title + "");
    });
});

或者这个:

TdApi.GetChats getchats1 = new TdApi.GetChats(Long.MAX_VALUE, 0, 100);
TG.getClientInstance().send(getchats1, new Client.ResultHandler() {
     @Override
     public void onResult(TdApi.TLObject object) {
     TdApi.Chats chat = (TdApi.Chats) object;
     final TdApi.Chat[] chats2 = chat.chats;
     Log.e("OkHttp hey", "2 " + chats2.length + "");
     Log.e("OkHttp hey", "2 " + chats2[0].title + "");

     TdApi.GetChats getChats2 = new TdApi.GetChats(Long.MAX_VALUE-100, 0, 100);
     TG.getClientInstance().send(getChats2, new Client.ResultHandler() {
         @Override
         public void onResult(TdApi.TLObject object) {
         TdApi.Chats chat = (TdApi.Chats) object;
         final TdApi.Chat[] chats3 = chat.chats;
         Log.e("OkHttp hey", "3 " + chats3.length + "");
         Log.e("OkHttp hey", "2 " + chats3[0].title + "");
    });
});

1 个答案:

答案 0 :(得分:0)

我使用了这个示例:https://github.com/tdlib/td/blob/master/example/cpp/td_example.cpp

为了获得所有聊天,我使用以下代码:

变量

//...
int64_t offset_order_ = std::numeric_limits<int64_t>::max();
int64_t offset_chat_id_ = 0;
//...

在update_chats功能中

//for get next 100
if (chats->chat_ids_.size() > 0) {
    auto last_chat_id = chats->chat_ids_[chats->chat_ids_.size() - 1];
    send_query(td_api::make_object<td_api::getChat>(last_chat_id), [this](Object o){
        if (o->get_id() == td_api::error::ID) {
            return;
        }

        auto chat = td::move_tl_object_as<td_api::chat>(o);
        offset_order_ = chat->order_;
        offset_chat_id_ = chat->id_;
        update_chats_ = true;

        // std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    });
}

循环内功能

if (update_chats_) {
    send_query(td_api::make_object<td_api::getChats>(offset_order_, offset_chat_id_, 100), 
            std::bind(&TgClient::update_chats, this, std::placeholders::_1));

    update_chats_ = false;
}