Search code examples
telegramtelegram-botnode.js-telegram

In Nodejs, Telegram API, messages.GetAllChats method no longer work?


It has worked before. And just throw the error a few days recently. I don't see any updates from the Official documentation https://core.telegram.org/method/messages.GetAllChats

Doc update: The method was removed in layer 158 https://core.telegram.org/api/layers#deleted-methods

Deleted Methods
Removed messages.getAllChats
Removed folders.deleteFolder

RPC error message 400 IMPUT_METHOD_INVALID

Here is my implementation

I'm using the latest Telegram npm package

"telegram": "^2.16.4",
import { Api, TelegramClient } from 'telegram';

export async function getClient(): Promise<TelegramClient> {
  const { stringSession, apiId, apiHash } = getConfig();

  if (!globalClient) {
    console.log('Connect to Telegram...');

    globalClient = new TelegramClient(stringSession, apiId, apiHash, {
      connectionRetries: 5,
      testServers: !IS_PRODUCTION,
    });
    await globalClient.connect();
  }

  return globalClient;
}

 const client = await getClient();

 const result = await client.invoke(
   new Api.messages.GetAllChats({
        exceptIds: [],
      }),
    );

const finalResult = result.chats.map((item: any) => ({
  id: Number(item.id.value),
  title: item.title,
  username: item.username,
  className: item.className,
}));

console.log(JSON.stringify(finalResult));

Could someone help?


Solution

  • That method was removed in Layer 158. You should probably use messages.getDialogs instead.