Reputation: 2184
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.
Upvotes: 6
Views: 6229
Reputation: 1
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)
Upvotes: -5
Reputation: 411
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;
}
});
}
});
Upvotes: 3
Reputation: 411
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.
Upvotes: 4