tdlib / td

Cross-platform library for building Telegram clients
https://core.telegram.org/tdlib
Boost Software License 1.0
7.11k stars 1.44k forks source link

Search Telegram channels by `chatId` #2556

Closed akulik512 closed 1 year ago

akulik512 commented 1 year ago

I use TdApi.SearchPublicChats to find channels by query: client.send(new TdApi.SearchPublicChats("Witcher"), defaultHandler);

It returns such response:

Chats {
  totalCount = 9
  chatIds = vector[9] {
    -1001209710168
    -1001849893787
    110983051
    -1001616648573
    -1001939679537
    -1001468011807
    -1001673005436
    -1001727802006
    -1001249607996
  }
}

Could you please help me understand how can I find a channel bychatId? There is a TdApi.GetChat allows find channel by chatId but this is offline request and channels found above were not loaded into the active session before so I can't use it to find channels. The TdApi.GetChat returns error in this case:

Error {
  code = 400
  message = "Chat not found"
}

Are there any other ways to find channels by chatId?

levlam commented 1 year ago

It is impossible to find chats by chatId, but this is never needed to process response to the request TdApi.SearchPublicChats. See https://core.telegram.org/tdlib/getting-started for more details.

akulik512 commented 1 year ago

Maybe I do something wrong if it's not possible? I try to emulate a Telegram global search to find channels by query. In an example above I found channels by the "Witcher" query but it returned chatIds but I'd like to find channel names (@channelName) by these identifiers. Could you please let me know am I on the right way or not? Thanks.

levlam commented 1 year ago

See https://core.telegram.org/tdlib/getting-started.

Kylmakalle commented 1 year ago

@akulik512 this may be useful https://core.telegram.org/tdlib/getting-started#handling-updates

updateNewChat — This update is received whenever a new chat is discovered. This update is guaranteed to come before the chat identifier is returned to the Application. So, whenever an Application receives a chat_id, it never needs to use a getChat request to receive the chat object. Instead it must maintain a cache of chats received through this update and take all the necessary data about chats from this cache.

akulik512 commented 1 year ago

Hello @levlam @Kylmakalle It turned out much more easily when I understood tdlib processes a bit better. When I sent a search request using SearchPublicChats it loaded chat into the current session and I've got them from a session just sending the GetChat request in a loop for each specific chatId :man_facepalming: If you don't have any comments, we can close this issue.

UPD: I also wanna try implement something like:

    private class UpdateHandler implements Client.ResultHandler {
        @Override
        public void onResult(TdApi.Object object) {
            switch (object.getConstructor()) {
                case TdApi.Chats.CONSTRUCTOR:

Probably, it will work as listener for such events.