diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index ac6b6324..76c34905 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -59,6 +59,7 @@ import { getChatMembers } from './methods/chats/get-chat-members.js' import { getChatPreview } from './methods/chats/get-chat-preview.js' import { getFullChat } from './methods/chats/get-full-chat.js' import { getNearbyChats } from './methods/chats/get-nearby-chats.js' +import { getSimilarChannels } from './methods/chats/get-similar-channels.js' import { iterChatEventLog } from './methods/chats/iter-chat-event-log.js' import { iterChatMembers } from './methods/chats/iter-chat-members.js' import { joinChat } from './methods/chats/join-chat.js' @@ -1538,6 +1539,19 @@ export interface TelegramClient extends BaseTelegramClient { * @param longitude Longitude of the location */ getNearbyChats(latitude: number, longitude: number): Promise + + /** + * Get channels that are similar to a given channel + * + * > **Note**: This method only returns the channels that the current user + * > is not subscribed to. For non-premium users, this method will only return + * > a few channels (with the total number of similar channels being specified in `.total`) + * > + * > Returns empty array in case there are no similar channels available. + * **Available**: 👤 users only + * + */ + getSimilarChannels(channel: InputPeerLike): Promise> /** * Iterate over chat event log. * @@ -5184,6 +5198,7 @@ export class TelegramClient extends BaseTelegramClient { getChat = getChat.bind(null, this) getFullChat = getFullChat.bind(null, this) getNearbyChats = getNearbyChats.bind(null, this) + getSimilarChannels = getSimilarChannels.bind(null, this) iterChatEventLog = iterChatEventLog.bind(null, this) iterChatMembers = iterChatMembers.bind(null, this) joinChat = joinChat.bind(null, this) diff --git a/packages/client/src/methods/chats/get-similar-channels.ts b/packages/client/src/methods/chats/get-similar-channels.ts new file mode 100644 index 00000000..d187346b --- /dev/null +++ b/packages/client/src/methods/chats/get-similar-channels.ts @@ -0,0 +1,35 @@ +import { BaseTelegramClient } from '@mtcute/core' + +import { ArrayWithTotal, Chat, InputPeerLike } from '../../types/index.js' +import { makeArrayWithTotal } from '../../utils/misc-utils.js' +import { normalizeToInputChannel } from '../../utils/peer-utils.js' +import { resolvePeer } from '../users/resolve-peer.js' + +// @available=user +/** + * Get channels that are similar to a given channel + * + * > **Note**: This method only returns the channels that the current user + * > is not subscribed to. For non-premium users, this method will only return + * > a few channels (with the total number of similar channels being specified in `.total`) + * > + * > Returns empty array in case there are no similar channels available. + */ +export async function getSimilarChannels( + client: BaseTelegramClient, + channel: InputPeerLike, +): Promise> { + const res = await client.call({ + _: 'channels.getChannelRecommendations', + channel: normalizeToInputChannel(await resolvePeer(client, channel), channel), + }) + + const parsed = res.chats.map((chat) => new Chat(chat)) + + switch (res._) { + case 'messages.chatsSlice': + return makeArrayWithTotal(parsed, res.count) + case 'messages.chats': + return makeArrayWithTotal(parsed, parsed.length) + } +}