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

5.4k views Asked by At

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.

3

There are 3 answers

0
Alex Maddyson On

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.

1
Alex Maddyson On

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;
                }
            });
        }
    });
0
Сальников Сергей On

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)