Skip to content

Commit

Permalink
[USERS] fetch forward origin on reply
Browse files Browse the repository at this point in the history
  • Loading branch information
ryukaizen committed Jun 20, 2024
1 parent 7040fdd commit 71140f0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 18 deletions.
20 changes: 13 additions & 7 deletions src/helpers/helper_func.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,12 +505,18 @@ export async function getUserFullInstance(user: any) {
}

export async function resolveUserhandle(userhandle: string) {
const user = await gramjs.invoke(
new gramJsApi.users.GetFullUser({
id: userhandle,
})
);
return user;
try {
const user = await gramjs.invoke(
new gramJsApi.users.GetFullUser({
id: userhandle,
})
);
return user;
}
catch (ValueError) {
return undefined;
}

}

export async function datacenterLocation(dcId: number | string) {
Expand Down Expand Up @@ -584,6 +590,6 @@ export function convertUnixTime(unixTime: number): string {

export const sleep = promisify(setTimeout);

export function format_json(json: any) {
export async function format_json(json: any) {
return JSON.stringify(json, null, 2);
}
60 changes: 49 additions & 11 deletions src/modules/users.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { bot } from "../bot";
import { gramjs, gramJsApi } from "../utility";
import { resolveUserhandle, getUserInstance, getUserFullInstance, datacenterLocation } from "../helpers/helper_func"
import { resolveUserhandle, getUserInstance, getUserFullInstance, datacenterLocation, generateRandomFilename } from "../helpers/helper_func"
import { Composer } from "grammy";

const composer = new Composer();


// TODO: SEND PROFILE PICTURE AS DOCUMENT IN GROUP CHATS (userInfoPublic)
async function userInfoPublic(ctx: any, user_id: string) {
let user = await resolveUserhandle(user_id);
Expand All @@ -26,7 +24,7 @@ async function userInfoPublic(ctx: any, user_id: string) {

// Username
if (userinstance.username) {
message_body += `\n<b>Username</b>: <code>@${userinstance.username}</code>`;
message_body += `\n<b>Username</b>: @${userinstance.username}`;
};

// Multiple usernames (for rich kids)
Expand Down Expand Up @@ -64,7 +62,6 @@ async function userInfoPublic(ctx: any, user_id: string) {
}
message_body += `\n<b>Premium</b>: ${premium_status}`;


// Profile picture count
let photos_string = JSON.stringify(photos);
let regex = /"count"\s*:\s*(\d+)/;
Expand All @@ -81,7 +78,7 @@ async function userInfoPublic(ctx: any, user_id: string) {
file:photos.photos[0], message: message_body, parseMode: "html"
});
}

async function userInfoPrivate(ctx: any, user_id: string) {
let user = await resolveUserhandle(user_id);
let userfull = await getUserFullInstance(user);
Expand Down Expand Up @@ -167,38 +164,79 @@ async function fetchId(ctx: any) {
if (ctx.message.reply_to_message != undefined) {
let replied_to_user_id = ctx.message.reply_to_message.from.id;
let replied_to_message_id = ctx.message.reply_to_message.message_id;

if (ctx.message.reply_to_message.forward_origin) {
if (ctx.message.reply_to_message?.forward_origin?.type == "user") {
replied_to_user_id = ctx.message.reply_to_message?.forward_origin?.sender_user?.id
response += `\n<b>Replied user's ID :</b> <code>${replied_to_user_id}</code>`;
}
else if (ctx.message.reply_to_message?.forward_origin?.type == "chat") {
replied_to_user_id = ctx.message.reply_to_message?.forward_origin?.sender_chat?.id
response += `\n<b>Replied chat's ID :</b> <code>${replied_to_user_id}</code>`;
}
else if (ctx.message.reply_to_message?.forward_origin?.type == "channel") {
replied_to_user_id = ctx.message.reply_to_message?.forward_origin?.chat?.id
response += `\n<b>Replied channel's ID :</b> <code>${replied_to_user_id}</code>`;
}
else {
response += `\n<b>Replied user's ID :</b> hidden <b>(forward privacy)</b>`;
}
}

response += `\n<b>Replied message ID :</b> <code>${replied_to_message_id}</code>`;
response += `\n<b>Replied user's ID :</b> <code>${replied_to_user_id}</code>`;

}
else {
response += `<b>\nMessage ID :</b> <code>${message_id}</code>`
}

if (ctx.match) {
let user = await resolveUserhandle(ctx.match);
if (user != undefined) {
let userInstance = await getUserInstance(user);
let user_id = Number(user?.fullUser?.id)
response = `\n<b>${userInstance.firstName}'s ID is:</b> <code>${user_id}</code>`;
}

}
await ctx.api.sendMessage(ctx.chat.id, response, {reply_parameters: {message_id: ctx.message.message_id}, parse_mode: "HTML"});
}

composer.chatType("supergroup" || "group").command("info", (async (ctx: any) => {
if (ctx.message.reply_to_message != undefined) {
if (ctx.message.reply_to_message) {
let user_id = ctx.message.reply_to_message.from.id;
await userInfoPublic(ctx, user_id);
if (ctx.message.reply_to_message?.forward_origin) {
if (ctx.message.reply_to_message?.forward_origin?.type == "user") {
await userInfoPublic(ctx, ctx.message.reply_to_message?.forward_origin?.sender_user?.id);
}
else if (ctx.message.reply_to_message?.forward_origin?.type == "chat") {
await userInfoPublic(ctx, ctx.message.reply_to_message?.forward_origin?.sender_chat?.id);
}
else if (ctx.message.reply_to_message?.forward_origin?.type == "channel") {
await userInfoPublic(ctx, ctx.message.reply_to_message?.forward_origin?.chat?.id)

}
else if (ctx.message.reply_to_message?.forward_origin?.type == "hidden_user") {
await ctx.reply(`The original sender (${ctx.message.reply_to_message?.forward_origin?.sender_user_name}) of that message has forward privacy enabled >_<`, {reply_parameters: {message_id: ctx.message.message_id}})
}
}
else {
await userInfoPublic(ctx, user_id)
}
}
else {
let args = ctx.match;
if (args) {
let split_args = args.split(" ");
let userhandle = split_args[0];
let user = await resolveUserhandle(userhandle).catch(() => {});
if (user != undefined) {
let user = await resolveUserhandle(userhandle).catch(() => {})
if (user) {
let userinstance = await getUserInstance(user);
await userInfoPublic(ctx, userinstance.id);
}
else {
await ctx.reply("Couldn't find entity for the given user-handle.\n\n<i>Tip: Consider providing their username instead for better results.</i>", {reply_parameters: {message_id: ctx.message.message_id}, parse_mode: "HTML"})
}
}
else {
let user_id = ctx.from.id;
Expand Down

0 comments on commit 71140f0

Please sign in to comment.