6.2
This commit is contained in:
1 parent
528104fff0
commit
beb498d38b
60 files changed
+3184
-2129
No files matched your search
@@ -0,0 +1,52 @@
|
||||
import { API, ALLOWED_IDS } from "./config";
|
||||
|
||||
interface TgResponse<T = unknown> {
|
||||
ok: boolean;
|
||||
result?: T;
|
||||
description?: string;
|
||||
}
|
||||
|
||||
export async function tg<T = unknown>(
|
||||
method: string,
|
||||
body: Record<string, unknown>
|
||||
): Promise<TgResponse<T>> {
|
||||
const res = await fetch(`${API}/${method}`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
return res.json() as Promise<TgResponse<T>>;
|
||||
}
|
||||
|
||||
export function allowed(chatId: number): boolean {
|
||||
if (ALLOWED_IDS.length === 0) return true;
|
||||
return ALLOWED_IDS.includes(chatId);
|
||||
}
|
||||
|
||||
export async function sendMessage(
|
||||
chatId: number,
|
||||
text: string,
|
||||
opts?: Record<string, unknown>
|
||||
): Promise<void> {
|
||||
const safeText = String(text || "");
|
||||
try {
|
||||
await tg("sendMessage", {
|
||||
chat_id: chatId,
|
||||
text: safeText,
|
||||
parse_mode: "HTML",
|
||||
disable_web_page_preview: true,
|
||||
...opts,
|
||||
});
|
||||
} catch (e: unknown) {
|
||||
const msg = e instanceof Error ? e.message : String(e);
|
||||
if (msg.includes("parse") || msg.includes("HTML")) {
|
||||
await tg("sendMessage", {
|
||||
chat_id: chatId,
|
||||
text: safeText,
|
||||
...opts,
|
||||
});
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user