|
| 1 | +import { Context } from 'grammy'; |
| 2 | +import { Catch } from '../../../decorators/ErrorHandlingDecorator'; |
| 3 | +import { BotReply } from '../../../utils/chat/BotReply'; |
| 4 | +import { help, start, commands } from '../../../utils/jsons/botMessages.json'; |
| 5 | +import { ChatInfo } from '../../../utils/chat/ChatInfo'; |
| 6 | +import { DateCommand } from './date'; |
| 7 | +import { info, user_support } from '../../../../docs/BotInfo.json'; |
| 8 | +/** |
| 9 | + * Reason for lowercase command names: |
| 10 | + * |
| 11 | + * Since the names of all commands are written in lowercase for uniformity, |
| 12 | + * and the commands are automatically generated, I needed to ensure that commands |
| 13 | + * can be triggered in any case (e.g., "getBotInfo", "Getbotinfo", or "getbotinfo"). |
| 14 | + * To achieve this, I converted all command names to lowercase. |
| 15 | + * This way, the system can recognize commands regardless of the case |
| 16 | + * the user enters, providing a consistent user experience. |
| 17 | + * |
| 18 | + * Code for handling case-insensitivity bot/bot.ts |
| 19 | + */ |
| 20 | +export class GeneralCommands { |
| 21 | + static getMessage(ctx: Context): { commands: string; help: string; start: string } { |
| 22 | + const chatInfo = new ChatInfo(ctx); |
| 23 | + const chattype = chatInfo.getChatType(); |
| 24 | + if (chattype === 'private') { |
| 25 | + return { |
| 26 | + help: help.general, |
| 27 | + commands: commands.private, |
| 28 | + start: start.Private, |
| 29 | + }; |
| 30 | + } |
| 31 | + return { |
| 32 | + commands: commands.public, |
| 33 | + help: help.general, |
| 34 | + start: start.gorup, |
| 35 | + }; |
| 36 | + } |
| 37 | + // === General Command Handlers === |
| 38 | + @Catch({ |
| 39 | + message: 'Error displaying help message. Please try again later.', |
| 40 | + category: 'Bot', |
| 41 | + statusCode: 500, |
| 42 | + }) |
| 43 | + public static async help(ctx: Context) { |
| 44 | + const reply = new BotReply(ctx); |
| 45 | + const { help } = GeneralCommands.getMessage(ctx); |
| 46 | + const sanitizedHelp = help.replace(/([_*[\]()~`>#+\-=|{}.!])/g, '\\$1'); |
| 47 | + await reply.markdownReply(sanitizedHelp); |
| 48 | + } |
| 49 | + |
| 50 | + @Catch({ |
| 51 | + message: 'Error starting the bot. Please try again later.', |
| 52 | + category: 'Bot', |
| 53 | + statusCode: 500, |
| 54 | + }) |
| 55 | + public static async start(ctx: Context) { |
| 56 | + const reply = new BotReply(ctx); |
| 57 | + const { start } = GeneralCommands.getMessage(ctx); |
| 58 | + await reply.textReply(start); |
| 59 | + } |
| 60 | + |
| 61 | + @Catch({ |
| 62 | + message: 'Error retrieving the date. Please try again later.', |
| 63 | + category: 'Bot', |
| 64 | + statusCode: 500, |
| 65 | + }) |
| 66 | + public static async date(ctx: Context) { |
| 67 | + const reply = new BotReply(ctx); |
| 68 | + const { gregorianDate, persianDate } = await DateCommand.date(); |
| 69 | + const message = ` |
| 70 | +🌍 **Gregorian Date**: |
| 71 | + ${gregorianDate} |
| 72 | +
|
| 73 | +🌍 **Persian Date**: |
| 74 | + ${persianDate} |
| 75 | +`; |
| 76 | + reply.markdownReply(message); |
| 77 | + } |
| 78 | + |
| 79 | + static async commands(ctx: Context) { |
| 80 | + const reply = new BotReply(ctx); |
| 81 | + const { commands } = GeneralCommands.getMessage(ctx); |
| 82 | + await reply.textReply(commands); |
| 83 | + } |
| 84 | + @Catch({ |
| 85 | + message: 'Error retrieving support contact information. Please try again later.', |
| 86 | + category: 'Bot', |
| 87 | + statusCode: 500, |
| 88 | + }) |
| 89 | + /** viewSupportContact */ |
| 90 | + public static async viewsupportcontact(ctx: Context) { |
| 91 | + const reply = new BotReply(ctx); |
| 92 | + |
| 93 | + // Contact information from BotInfo.json |
| 94 | + const contactMessage = ` |
| 95 | +**Help Contact**: ${user_support.help_contact}\n\r |
| 96 | +**Support Email**: ${user_support.support_email} |
| 97 | +**Support Groups**: |
| 98 | + ${user_support.support_groups.map((group) => `- ${group.name}: ${group.link}`).join('\n')} |
| 99 | +
|
| 100 | +Please reach out to us for assistance. |
| 101 | + `; |
| 102 | + await reply.markdownReply(contactMessage); |
| 103 | + } |
| 104 | + @Catch({ |
| 105 | + message: 'Error retrieving bot information. Please try again later.', |
| 106 | + category: 'Bot', |
| 107 | + statusCode: 500, |
| 108 | + }) |
| 109 | + /** getBotInfo */ |
| 110 | + public static async getbotinfo(ctx: Context) { |
| 111 | + const reply = new BotReply(ctx); |
| 112 | + |
| 113 | + // Extract detailed information from BotInfo.json |
| 114 | + const botInfoMessage = ` |
| 115 | + **Bot Information**: |
| 116 | + - **Name**: ${info.bot_name} |
| 117 | + - **Version**: ${info.bot_version} |
| 118 | + - **Status**: ${info.bot_status} |
| 119 | + - **Creation Date**: ${new Date(info.created_at).toLocaleString()} |
| 120 | + - **Last Updated**: ${new Date(info.last_update).toLocaleString()} |
| 121 | + - **Supported Languages**: ${info.supported_languages.join(', ')} |
| 122 | +
|
| 123 | +**Description**: ${info.description} |
| 124 | + |
| 125 | +This bot is designed to deliver fast and secure responses to users. |
| 126 | + `; |
| 127 | + await reply.markdownReply(botInfoMessage); |
| 128 | + } |
| 129 | +} |
0 commit comments