Created
January 21, 2023 09:50
-
-
Save waptik/9de410055eac8a60668ce7ac1e5183ac to your computer and use it in GitHub Desktop.
[GramJS] - Getting participant object in group and channel chats
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The code below works for both gramjs in node and grm in deno | |
.... // other imports above | |
import ChannelParticipants = Api.channels.ChannelParticipants; | |
import ChatParticipant = Api.ChatParticipant; | |
import ChannelParticipant = Api.ChannelParticipant; | |
/** | |
* Get all channel participants | |
* @see {@link https://github.com/Dionid/teleadmin/blob/e96bf05ad7b39d27868077303a1c69638dcb6a84/main-back/src/libs/telegram-js/get-channel-partisipants.ts#L11} | |
* | |
* @param client TelegramClient | |
* @param channel Channel | |
* @param offset number | |
* @param limit number | |
* @param aggregated ChannelParticipants | |
* @returns Promise<Api.channels.TypeChannelParticipants> | |
*/ | |
export const getAllChannelParticipants = async ( | |
client: TelegramClient, | |
channel: Api.TypeEntityLike, | |
offset = 0, | |
limit = 999999, | |
aggregated?: ChannelParticipants, | |
): Promise<Api.channels.TypeChannelParticipants> => { | |
const resultOrErr = await client.invoke( | |
new Api.channels.GetParticipants({ | |
channel, | |
filter: new Api.ChannelParticipantsRecent(), | |
limit, | |
offset, | |
}), | |
); | |
if (resultOrErr instanceof Error) { | |
if (resultOrErr.message.includes("No user has")) { | |
throw new Error("Channel name is incorrect or yoy haven't added homunculus to private channel"); | |
} | |
throw resultOrErr; | |
} | |
if (resultOrErr instanceof ChannelParticipants) { | |
if (aggregated) { | |
resultOrErr.participants = resultOrErr.participants.concat(aggregated.participants); | |
resultOrErr.users = resultOrErr.users.concat(aggregated.users); | |
resultOrErr.chats = resultOrErr.chats.concat(aggregated.chats); | |
} | |
if (resultOrErr.participants.length < resultOrErr.count) { | |
return getAllChannelParticipants(client, channel, resultOrErr.participants.length, limit, resultOrErr); | |
} | |
return resultOrErr; | |
} | |
throw new Error("Result in not instanceof ChannelParticipants"); | |
}; | |
... | |
// previously defined client and other relevant stuffs above | |
const users: Array<UserInfo> = []; | |
const entity = await client.getEntity(chatId); | |
if (entity.className === "Channel" || entity.className === "Chat") { | |
console.log("Get users for this", entity.className); | |
const limit = 999999; | |
const offset = 0; | |
switch (entity.className) { | |
case "Channel": { | |
const members = await getAllChannelParticipants(client, entity); | |
if (members instanceof ChannelParticipants) { | |
const total = members.count; | |
members.participants.forEach((item) => { | |
if (item instanceof ChannelParticipant) { | |
const user = members.users.find((u) => u.id.toString() === item.userId.toString()); | |
if (user instanceof Api.User) { | |
users.push({ | |
id: user.id.toString(), | |
firstName: user.firstName || "", | |
lastName: user.lastName, | |
username: user.username, | |
}); | |
} | |
} | |
}); | |
console.log({ total, users, totalUsers: users.length }); | |
} | |
break; | |
} | |
case "Chat": { | |
const members = await client.getParticipants(entity, { | |
filter: new Api.ChannelParticipantsRecent(), | |
limit, | |
offset, | |
showTotal: true, | |
}); | |
members.forEach((member) => { | |
if ("participant" in member && member.participant instanceof ChatParticipant) { | |
users.push({ | |
id: "", // member.id.toString(), | |
firstName: member.firstName || "", | |
lastName: member.lastName, | |
username: "", // member.username, | |
}); | |
} | |
}); | |
console.log({ total: members.length, users, totalUsers: users.length }); | |
break; | |
} | |
default: | |
break; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Below is an example log of regular members in a group