Skip to content

Commit ae5c1a2

Browse files
committed
Initial commit
0 parents  commit ae5c1a2

35 files changed

+6469
-0
lines changed

Chiku/Front-End/index.ts

+2,628
Large diffs are not rendered by default.

Chiku/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export { ChikuAi, TgApi, onUpdate, ChikuOf, ChikuOn, handleWebhook } from "./main";
2+
export { Neko } from "./utils";
3+
export { sendStartMessage, NotifyLogGroup, getLamaResponse, getClaudResponse, getMixtralResponse, UploadToImageUr, sendDice, DownloadInstaVdo, GetAiVoice, SendUid, GetCharacterImages, generateImage, getPokemon, fetchBingImages, DICE_COMMANDS } from "./plugins";

Chiku/interfaces/Index.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export interface AIResponse {
2+
text?: string;
3+
}
4+
5+
export interface TelegramUpdate {
6+
message?: {
7+
chat: { id: number };
8+
text?: string;
9+
};
10+
}
11+
12+
export interface NekoResponse {
13+
results: { url: string }[];
14+
}
15+
16+
export interface CallbackQuery {
17+
id: string;
18+
data: string;
19+
message?: {
20+
chat: { id: number };
21+
message_id: number;
22+
};
23+
}
24+

Chiku/main/Chiku.ts

+267
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
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+

Chiku/main/api.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { TOKEN } from "../../config";
2+
3+
export function TgApi(methodName: string, params: Record<string, any> | null = null): string {
4+
const query = params ? "?" + new URLSearchParams(params).toString() : "";
5+
return `https://api.telegram.org/bot${TOKEN}/${methodName}${query}`;
6+
}
7+

Chiku/main/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export { onUpdate, ChikuOf, ChikuOn, handleWebhook } from "./webhooks";
2+
export { ChikuAi } from "./Chiku";
3+
export { TgApi } from "./api";

Chiku/main/webhooks.ts

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { TelegramUpdate as X } from "../Chiku/interfaces";
2+
import { SECRET as Y } from "../../config";
3+
import { ChikuAi as Z } from "../index";
4+
import { TgApi as W } from "./api";
5+
6+
async function A(a: FetchEvent): Promise<Response> {
7+
if (a.request.headers.get("X-Telegram-Bot-Api-Secret-Token") !== Y) {
8+
return new Response("Unauthorized", { status: 403 });
9+
}
10+
try {
11+
const b: X = await a.request.json();
12+
await B(b);
13+
return new Response("Bot is active");
14+
} catch (c) {
15+
console.error("Error:", c);
16+
return new Response("Error processing request", { status: 500 });
17+
}
18+
}
19+
20+
async function B(d: X): Promise<void> {
21+
if (d.message) {
22+
await Z.handleMessage(d.message);
23+
} else if (d.callback_query) {
24+
await Z.handleCallback(d.callback_query);
25+
}
26+
}
27+
28+
async function C(
29+
e: FetchEvent,
30+
f: URL,
31+
g: string,
32+
h: string
33+
): Promise<Response> {
34+
const i = `${f.protocol}//${f.host}${g}`;
35+
const j = await fetch(W("setWebhook", { url: i, secret_token: h })).then((k) =>
36+
k.json()
37+
);
38+
if (!("ok" in j) || !j.ok) {
39+
throw new Error(`Failed to set webhook: ${JSON.stringify(j)}`);
40+
}
41+
return new Response("Ok");
42+
}
43+
44+
async function D(l: FetchEvent): Promise<Response> {
45+
const m = await fetch(W("setWebhook", { url: "" })).then((n) => n.json());
46+
if (!("ok" in m) || !m.ok) {
47+
throw new Error(`Failed to remove webhook: ${JSON.stringify(m)}`);
48+
}
49+
return new Response("Ok");
50+
}
51+
52+
export { A as handleWebhook, C as ChikuOn, D as ChikuOf, B as onUpdate };
53+

0 commit comments

Comments
 (0)