grmjs / grm

MTProto API Client for Deno 🦕
https://t.me/grm_news
MIT License
46 stars 3 forks source link

Could not find input entity with key -100xxxx #35

Closed waptik closed 1 year ago

waptik commented 1 year ago

Hi. So i'm trying to use this library with grammy.dev but i don't know how to go about it. So i'm trying to get all participants(users) in a group where i & the bot are both members and admins and i created a test group to try it out. I'm using a middleware to listen to all text messages and get the users

the middleware ```ts const composer = new Composer(); const groupEvents = composer.filter((ctx) => ctx.hasChatType(["group", "supergroup"])); // const groupId = telegramGroups.notifications.id; groupEvents .filter((ctx) => !ctx.hasChatType("private")) .on("message:text", async (ctx, next) => { const client = await getTelegramClient(); const entity = await client.getEntity(ctx.chat.id); console.log("entity", entity); console.log({ chatHash }); // const chatUsers = await client.invoke( // new Api.channels.GetParticipants({ // channel: entity.id, // new Api.InputPeerChat({ chatId: peer.chatId }), // filter: new Api.ChannelParticipantsRecent(), // offset: 0, // limit: 100, // // hash: entity.id, // }), // ); // console.log('chatUsers', chatUsers); return next(); }); ```
getTelegramClient() ```ts export function getTelegramClient(): Promise { return new Promise(async (resolve, reject) => { try { console.log("[TELEGRAM-USER] It will now connect to your Telegram account."); console.log("[TELEGRAM-USER] Loading and connecting to Telegram User API..."); const client = new TelegramClient(grmInfo.myStringSession, grmInfo.apiId, grmInfo.apiHash, { connectionRetries: 5, }); await client.start({ botAuthToken: tokens.tg, }); // await client.connect(); // tried this and it didn't work console.log("[TELEGRAM-USER] You should now be connected."); await client.sendMessage("me", code(client.session.save() as unknown as string).send); // it's being sent console.clear(); console.log("The session was sent to saved messages"); resolve(client); } catch (error) { reject(error); } }); } ```
The error in console ```sh [2023-01-17T05:08:18.046] [INFO] - [Starting reconnect...] The session was sent to saved messages Error: Could not find input entity with key -100xxxx at StringSession.getInputEntity (https://deno.land/x/grm@0.5.3/src/sessions/memory_session.ts:314:13) at async Module.getEntity (https://deno.land/x/grm@0.5.3/src/client/users.ts:159:19) at async file:///home/waptik/code/bots/testbot/src/telegram/handlers/middleware/events.group.ts:25:24 at async https://deno.land/x/grammy@v1.13.0/composer.ts:663:13 at async https://deno.land/x/grammy@v1.13.0/composer.ts:144:13 at async https://deno.land/x/grammy@v1.13.0/composer.ts:141:9 at async https://deno.land/x/grammy@v1.13.0/composer.ts:663:13 at async https://deno.land/x/grammy@v1.13.0/composer.ts:144:13 at async https://deno.land/x/grammy@v1.13.0/composer.ts:141:9 at async https://deno.land/x/grammy@v1.13.0/composer.ts:141:9 ```
waptik commented 1 year ago

When i try to use other methods, i get the error below

the alternative method/object ```ts const messages = await client.getMessages(entity); console.log("messages", messages); ```
the error ```sh Error: Cannot cast InputPeerChat to any kind of InputChannel at _raiseCastFail (https://deno.land/x/grm@0.5.3/src/utils.ts:22:9) at Module.getInputChannel (https://deno.land/x/grm@0.5.3/src/utils.ts:526:3) at getInputFromResolve (https://deno.land/x/grm@0.5.3/src/tl/api.js:594:20) at async VirtualClass.resolve (https://deno.land/x/grm@0.5.3/src/tl/api.js:426:27) at async Module.invoke (https://deno.land/x/grm@0.5.3/src/client/users.ts:44:3) at async file:///home/waptik/code/bots/testbot/src/telegram/handlers/middleware/events.group.ts:39:27 at async https://deno.land/x/grammy@v1.13.0/composer.ts:663:13 at async https://deno.land/x/grammy@v1.13.0/composer.ts:144:13 at async https://deno.land/x/grammy@v1.13.0/composer.ts:141:9 at async https://deno.land/x/grammy@v1.13.0/composer.ts:663:13 ```
rojvv commented 1 year ago

Duplicate of #31, @spechide?

waptik commented 1 year ago

Duplicate of #31, @SpEcHiDe?

According to your response in that issue, you mentioned it being a bot-only issue. But this problem persists again when i use a user client.

I decided to use StoreSession and the problem stops persisting after first success. When using StringSession, the problem will continue even after the first success when the server restarts.

rojvv commented 1 year ago

@waptik Your problem aside, do you know that the channels.GetParticipants method is not available for bots?

waptik commented 1 year ago

@waptik Your problem aside, do you know that the channels.GetParticipants method is not available for bots?

I don't. Because it didn't work with user clients that's why I had to try with bot client.

So I found a solution about it in gramjs telegram group which says I should use client.getParticipants and that worked.