Files
iistwin/server/medschedule/bot-sender.ts

91 lines
2.7 KiB
TypeScript

export type BotPlatform = "telegram" | "max";
const TELEGRAM_API_BASE = "https://api.telegram.org/bot";
const MAX_API_BASE = process.env.MAX_BOT_API_BASE || "https://api.max.ru/bot";
export async function sendBotMessage(
platform: BotPlatform,
chatId: string,
text: string,
options?: {
parseMode?: "HTML" | "Markdown";
replyMarkup?: any;
token?: string;
}
): Promise<{ ok: boolean; error?: string }> {
const token = options?.token || (platform === "telegram" ? process.env.TELEGRAM_BOT_TOKEN : process.env.MAX_BOT_TOKEN);
if (!token) {
return { ok: false, error: `Token not configured for ${platform}` };
}
const base = platform === "telegram" ? `${TELEGRAM_API_BASE}${token}` : `${MAX_API_BASE}${token}`;
const url = `${base}/sendMessage`;
try {
const body: Record<string, any> = {
chat_id: chatId,
text,
};
if (options?.parseMode) body.parse_mode = options.parseMode;
if (options?.replyMarkup) body.reply_markup = options.replyMarkup;
const response = await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
if (!response.ok) {
const errText = await response.text();
console.error(`[${platform}] sendMessage error ${response.status}:`, errText);
return { ok: false, error: errText };
}
return { ok: true };
} catch (err: any) {
console.error(`[${platform}] sendMessage failed:`, err);
return { ok: false, error: err.message || "Request failed" };
}
}
export async function sendBotPhoto(
platform: BotPlatform,
chatId: string,
photoUrl: string,
caption?: string,
options?: { token?: string }
): Promise<{ ok: boolean; error?: string }> {
const token = options?.token || (platform === "telegram" ? process.env.TELEGRAM_BOT_TOKEN : process.env.MAX_BOT_TOKEN);
if (!token) {
return { ok: false, error: `Token not configured for ${platform}` };
}
const base = platform === "telegram" ? `${TELEGRAM_API_BASE}${token}` : `${MAX_API_BASE}${token}`;
const url = `${base}/sendPhoto`;
try {
const body: Record<string, any> = {
chat_id: chatId,
photo: photoUrl,
};
if (caption) body.caption = caption;
const response = await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
if (!response.ok) {
const errText = await response.text();
console.error(`[${platform}] sendPhoto error ${response.status}:`, errText);
return { ok: false, error: errText };
}
return { ok: true };
} catch (err: any) {
console.error(`[${platform}] sendPhoto failed:`, err);
return { ok: false, error: err.message || "Request failed" };
}
}