I get chatId in tdlib but i can not get message (tdAPI or telegram database library for java)
Asked Answered
O

3

6

I am trying to use telegram database library for java (tdlib or tdapi) but when i get chatId of a channel by SearchPublicChat and try to get messages or view messages i get an error.

Error{code=6 message= Chat not found}

I can not understand why the chatId i receive above why when i pass it to another method i get that error Please help me about that problem and that library. Thank you.

Overreach answered 16/12, 2016 at 17:50 Comment(0)
N
4

Before requesting chat by id the TdLib must know about this chat in current session. You need search this chat by @mention_link if it public, or getting whole your chat list. Also, the library will be know about chat if some action happens with this chat (like new message from chat, chat updated...)
And this applies also to messages, users and etc. You can request it by id only when TdLib know about this entity.

Nano answered 16/12, 2016 at 19:56 Comment(0)
N
3

example for getting last 15 messages from chat

String username = "any_chat_public_link";
TdApi.SearchPublicChat searchPublicChat=new TdApi.SearchPublicChat(username);
    TG.getClientInstance().send(searchPublicChat, new Client.ResultHandler() {
        @Override
        public void onResult(TdApi.TLObject object) {                
            TdApi.Chat chat = (TdApi.Chat) object;
            TdApi.Message topMessage = chat.topMessage;

            long chatId = chat.id;

            TdApi.GetChatHistory getChatHistory = new TdApi.GetChatHistory(chatId, topMessage.id, 0, 15);
            TG.getClientInstance().send(getChatHistory, new Client.ResultHandler() {
                @Override
                public void onResult(TdApi.TLObject object) {
                    TdApi.Messages messages = (TdApi.Messages) object;
                }
            });
        }
    });
Nano answered 17/12, 2016 at 15:41 Comment(1)
How can i get latest 200 chats ( i need chat ids) ?Overreach
T
-4

Before receiving the message history, you need to subscribe to the chat by sending TdApi.JoinChat. The procedure is as follows:

1) TdApi.SearchPublicChat
2) TdApi.JoinChat
3) TdApi.GetChatHistory

TdApi.GetChatHistory requires the id of the last chat message. It can be obtained using the TdApi.GetChat method. I used tdlib/example. Information about chats is updated automatically by the getMainChatList method, then it can be obtained from chats.get(chatId)

Throwback answered 16/5, 2022 at 11:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.