From a1417a385855fa1cbc38e0f0f9543ef1ec4adb41 Mon Sep 17 00:00:00 2001 From: Zerocool <35503951+TonyAntoun@users.noreply.github.com> Date: Thu, 6 Feb 2025 00:40:43 +0200 Subject: [PATCH] Update telegram.provider.ts Added the code to delete the user /connect message - IF THE BOT IS ADMIN Added code to check the member status of the bot --- .../integrations/social/telegram.provider.ts | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/libraries/nestjs-libraries/src/integrations/social/telegram.provider.ts b/libraries/nestjs-libraries/src/integrations/social/telegram.provider.ts index 665ffcfb..79c31076 100644 --- a/libraries/nestjs-libraries/src/integrations/social/telegram.provider.ts +++ b/libraries/nestjs-libraries/src/integrations/social/telegram.provider.ts @@ -89,6 +89,39 @@ export class TelegramProvider extends SocialAbstract implements SocialProvider { ); // get correct chatId based on the channel type const chatId = match?.message?.chat?.id || match?.channel_post?.chat?.id; + + // prevents the code from running while chatId is still undefined to avoid the error 'ETELEGRAM: 400 Bad Request: chat_id is empty'. the code would still work eventually but console spam is not pretty + if (chatId) { + //get the numberic ID of the bot + const botId = (await telegramBot.getMe()).id; + // check if the bot is an admin in the chat + const isAdmin = await this.botIsAdmin(chatId, botId); + // get the messageId of the message that triggered the connection + const connectMessageId = + match?.message?.message_id || match?.channel_post?.message_id; + + if (!isAdmin) { + // alternatively you can replace this with a console.log if you do not want to inform the user of the bot's admin status + telegramBot.sendMessage( + chatId, + "Connection Successful. I don't have admin privileges to delete these messages, please go ahead and remove them yourself." + ); + } else { + // Delete the message that triggered the connection + await telegramBot.deleteMessage(chatId, connectMessageId); + // Send success message to the chat + const successMessage = await telegramBot.sendMessage( + chatId, + 'Connection Successful. Message will be deleted in 10 seconds.' + ); + // Delete the success message after 10 seconds + setTimeout(async () => { + await telegramBot.deleteMessage(chatId, successMessage.message_id); + console.log('Success message deleted.'); + }, 10000); + } + } + // modified lastChatId to work with any type of channel (private/public groups/channels) return chatId ? { chatId } @@ -214,4 +247,23 @@ export class TelegramProvider extends SocialAbstract implements SocialProvider { } return result; } + + async botIsAdmin(chatId: number, botId: number): Promise { + try { + const chatMember = await telegramBot.getChatMember(chatId, botId); + + if ( + chatMember.status === 'administrator' || + chatMember.status === 'creator' + ) { + const permissions = chatMember.can_delete_messages; + return !!permissions; // Return true if bot can delete messages + } + + return false; + } catch (error) { + console.error('Error checking bot privileges:', error); + return false; + } + } }