|
| 1 | +import { getChikuResponse } from "../utils/Response"; |
| 2 | +import { TgApi } from "./api"; |
| 3 | +import { TOKEN } from "../../config"; |
| 4 | +import { getRandomErrorText } from "../../strings/Magical-Spells"; |
| 5 | + |
| 6 | +class ChikuAi { |
| 7 | + private static commands: Map<string, Function> = new Map(); |
| 8 | + private static callbacks: Map<string, Function> = new Map(); |
| 9 | + |
| 10 | + static on_command(command: string, handler: Function): void { |
| 11 | + ChikuAi.commands.set(command, handler); |
| 12 | + } |
| 13 | + |
| 14 | + static callback(name: string, handler: Function): void { |
| 15 | + ChikuAi.callbacks.set(name, handler); |
| 16 | + } |
| 17 | + |
| 18 | + static async handleMessage(message: { chat: { id: number }; text?: string }): Promise<void> { |
| 19 | + const chatId = message.chat.id; |
| 20 | + const text = message.text?.trim() ?? ""; |
| 21 | + const command = text.split(" ")[0]; |
| 22 | + |
| 23 | + if (ChikuAi.commands.has(command)) { |
| 24 | + const handler = ChikuAi.commands.get(command); |
| 25 | + await handler?.(message); |
| 26 | + } else { |
| 27 | + await ChikuAi.TypingAction(chatId); |
| 28 | + try { |
| 29 | + const aiResponse = await getChikuResponse(text, chatId); |
| 30 | + await ChikuAi.sendWithRetries(chatId, aiResponse); |
| 31 | + } catch (error) { |
| 32 | + console.error("Error handling message:", error); |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + static async sendWithRetries(chatId: number, message: string, retries = 3): Promise<void> { |
| 38 | + for (let attempt = 1; attempt <= retries; attempt++) { |
| 39 | + try { |
| 40 | + await ChikuAi.send_message(chatId, message); |
| 41 | + return; |
| 42 | + } catch (error) { |
| 43 | + console.error(`Attempt ${attempt} to send message failed:`, error); |
| 44 | + if (attempt === retries) { |
| 45 | + console.error("All attempts to send the message failed."); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + static async handleCallback(callbackQuery: { data: string }): Promise<void> { |
| 52 | + const callbackData = callbackQuery.data; |
| 53 | + |
| 54 | + if (ChikuAi.callbacks.has(callbackData)) { |
| 55 | + const handler = ChikuAi.callbacks.get(callbackData); |
| 56 | + await handler?.(callbackQuery); |
| 57 | + } else { |
| 58 | + console.error("No handler registered for callback:", callbackData); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + static async send_message(chatId: number, text: string): Promise<any> { |
| 63 | + const effectIds = [ |
| 64 | + "5044134455711629726", |
| 65 | + "5046509860389126442", |
| 66 | + "5107584321108051014", |
| 67 | + "5104841245755180586", |
| 68 | + ]; |
| 69 | + const randomEffectId = effectIds[Math.floor(Math.random() * effectIds.length)]; |
| 70 | + const response = await fetch( |
| 71 | + TgApi("sendMessage", { |
| 72 | + chat_id: chatId, |
| 73 | + text, |
| 74 | + parse_mode: "Markdown", |
| 75 | + message_effect_id: randomEffectId, |
| 76 | + }) |
| 77 | + ); |
| 78 | + |
| 79 | + return response.json(); |
| 80 | + } |
| 81 | + |
| 82 | + static async send_messageV2( |
| 83 | + chatId: number, |
| 84 | + text: string, |
| 85 | + options: { parse_mode?: string; reply_markup?: any } = {} |
| 86 | + ): Promise<any> { |
| 87 | + const { parse_mode = "Markdown", reply_markup } = options; |
| 88 | + const body: Record<string, any> = { chat_id: chatId, text, parse_mode }; |
| 89 | + |
| 90 | + if (reply_markup) { |
| 91 | + try { |
| 92 | + body.reply_markup = typeof reply_markup === "object" ? JSON.stringify(reply_markup) : reply_markup; |
| 93 | + } catch (error) { |
| 94 | + console.error("Error serializing reply_markup:", error); |
| 95 | + throw new Error("Invalid reply_markup provided."); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + const response = await fetch(TgApi("sendMessage"), { |
| 100 | + method: "POST", |
| 101 | + headers: { "Content-Type": "application/json" }, |
| 102 | + body: JSON.stringify(body), |
| 103 | + }); |
| 104 | + |
| 105 | + return response.json(); |
| 106 | + } |
| 107 | + |
| 108 | + static async send_error(chatId: number): Promise<void> { |
| 109 | + const randomError = getRandomErrorText(); |
| 110 | + await ChikuAi.send_message(chatId, randomError); |
| 111 | + } |
| 112 | + |
| 113 | + static async delete_message(chatId: number, messageId: number): Promise<void> { |
| 114 | + try { |
| 115 | + await fetch(TgApi("deleteMessage"), { |
| 116 | + method: "POST", |
| 117 | + headers: { "Content-Type": "application/json" }, |
| 118 | + body: JSON.stringify({ chat_id: chatId, message_id: messageId }), |
| 119 | + }); |
| 120 | + } catch (error) { |
| 121 | + console.error(`Failed to delete message (ID: ${messageId}):`, error); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + static async sendMediaGroup(chatId: number, mediaGroup: Array<{ type: string; media: string }>): Promise<void> { |
| 126 | + await fetch( |
| 127 | + TgApi("sendMediaGroup", { |
| 128 | + chat_id: chatId, |
| 129 | + media: JSON.stringify(mediaGroup), |
| 130 | + }) |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + static async sendPhotoAction(chatId: number): Promise<void> { |
| 135 | + try { |
| 136 | + await fetch(TgApi("sendChatAction"), { |
| 137 | + method: "POST", |
| 138 | + headers: { "Content-Type": "application/json" }, |
| 139 | + body: JSON.stringify({ chat_id: chatId, action: "upload_photo" }), |
| 140 | + }); |
| 141 | + } catch (error) { |
| 142 | + console.error("Error sending photo action:", error); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + static async sendVoiceAction(chatId: number): Promise<void> { |
| 147 | + try { |
| 148 | + await fetch(TgApi("sendChatAction"), { |
| 149 | + method: "POST", |
| 150 | + headers: { "Content-Type": "application/json" }, |
| 151 | + body: JSON.stringify({ chat_id: chatId, action: "record_voice" }), |
| 152 | + }); |
| 153 | + } catch (error) { |
| 154 | + console.error("Error sending voice action:", error); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + static async send_audio(chatId: number, audioUrl: string): Promise<void> { |
| 159 | + await fetch(TgApi("sendAudio"), { |
| 160 | + method: "POST", |
| 161 | + headers: { "Content-Type": "application/json" }, |
| 162 | + body: JSON.stringify({ chat_id: chatId, audio: audioUrl }), |
| 163 | + }); |
| 164 | + } |
| 165 | + |
| 166 | + static async TypingAction(chatId: number): Promise<void> { |
| 167 | + await fetch(TgApi("sendChatAction", { chat_id: chatId, action: "typing" })); |
| 168 | + } |
| 169 | + |
| 170 | + static async send_photo( |
| 171 | + chatId: number, |
| 172 | + photoUrl: string, |
| 173 | + options: { caption?: string; parse_mode?: string; reply_markup?: any; spoiler?: boolean } = {} |
| 174 | + ): Promise<void> { |
| 175 | + const { caption = "", parse_mode = "MarkdownV2", reply_markup = undefined, spoiler = false } = options; |
| 176 | + const photo = spoiler ? `||${photoUrl}||` : photoUrl; |
| 177 | + |
| 178 | + await fetch(TgApi("sendPhoto"), { |
| 179 | + method: "POST", |
| 180 | + headers: { "Content-Type": "application/json" }, |
| 181 | + body: JSON.stringify({ |
| 182 | + chat_id: chatId, |
| 183 | + photo, |
| 184 | + caption: caption || undefined, |
| 185 | + parse_mode: caption ? parse_mode : undefined, |
| 186 | + reply_markup: reply_markup || undefined, |
| 187 | + }), |
| 188 | + }); |
| 189 | + } |
| 190 | + |
| 191 | + static async getFile(fileId: string): Promise<any> { |
| 192 | + try { |
| 193 | + const apiUrl = `https://api.telegram.org/bot${TOKEN}/getFile?file_id=${fileId}`; |
| 194 | + const response = await fetch(apiUrl); |
| 195 | + if (!response.ok) { |
| 196 | + throw new Error(`Failed to fetch file info: ${response.statusText}`); |
| 197 | + } |
| 198 | + const responseData = await response.json(); |
| 199 | + if (!responseData.ok) { |
| 200 | + throw new Error(`Error in API response: ${responseData.description}`); |
| 201 | + } |
| 202 | + return responseData.result; |
| 203 | + } catch (error) { |
| 204 | + console.error("Error fetching file info:", error); |
| 205 | + throw error; |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + static async send_video(chatId: number, videoUrl: string, caption: string): Promise<void> { |
| 210 | + await fetch(TgApi("sendVideo"), { |
| 211 | + method: "POST", |
| 212 | + headers: { "Content-Type": "application/json" }, |
| 213 | + body: JSON.stringify({ |
| 214 | + chat_id: chatId, |
| 215 | + video: videoUrl, |
| 216 | + caption, |
| 217 | + parse_mode: "HTML", |
| 218 | + }), |
| 219 | + }); |
| 220 | + } |
| 221 | + |
| 222 | + static async VideoAction(chatId: number): Promise<void> { |
| 223 | + await fetch(TgApi("sendChatAction", { chat_id: chatId, action: "upload_video" })); |
| 224 | + } |
| 225 | + |
| 226 | + static async send_animation( |
| 227 | + chatId: number, |
| 228 | + animationUrl: string, |
| 229 | + options: { caption?: string; parse_mode?: string } = {} |
| 230 | + ): Promise<void> { |
| 231 | + const { caption = "", parse_mode = "Markdown" } = options; |
| 232 | + |
| 233 | + await fetch(TgApi("sendAnimation"), { |
| 234 | + method: "POST", |
| 235 | + headers: { "Content-Type": "application/json" }, |
| 236 | + body: JSON.stringify({ |
| 237 | + chat_id: chatId, |
| 238 | + animation: animationUrl, |
| 239 | + caption: caption || undefined, |
| 240 | + parse_mode: caption ? parse_mode : undefined, |
| 241 | + }), |
| 242 | + }); |
| 243 | + } |
| 244 | + |
| 245 | + static async edit_message_text( |
| 246 | + chatId: number, |
| 247 | + messageId: number, |
| 248 | + text: string, |
| 249 | + options: { parse_mode?: string; reply_markup?: any } = {} |
| 250 | + ): Promise<void> { |
| 251 | + await fetch(TgApi("editMessageText"), { |
| 252 | + method: "POST", |
| 253 | + headers: { "Content-Type": "application/json" }, |
| 254 | + body: JSON.stringify({ |
| 255 | + chat_id: chatId, |
| 256 | + message_id: messageId, |
| 257 | + text, |
| 258 | + ...options, |
| 259 | + }), |
| 260 | + }); |
| 261 | + } |
| 262 | +} |
| 263 | + |
| 264 | +export { ChikuAi }; |
| 265 | + |
| 266 | + |
| 267 | + |
0 commit comments