brendmanager/src/telegram.js
2026-11-15 14:53:17 +05:00

37 lines
1.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Telegram Bot API: sendMessage. Токен и chat id — только из env.
export function createTelegram({ token = process.env.TELEGRAM_BOT_TOKEN, chatId = process.env.TELEGRAM_CHAT_ID, fetchImpl = fetch } = {}) {
return {
channel: "telegram",
secrets: () => [token].filter(Boolean),
connected: () => Boolean(token && chatId),
async send(text) {
if (!token || !chatId) throw new Error("нет доступов к Telegram");
const r = await fetchImpl(`https://api.telegram.org/bot${token}/sendMessage`, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ chat_id: chatId, text }),
});
const j = await r.json();
if (!j.ok) throw new Error(j.description ?? "ошибка Telegram");
return j.result;
},
};
}
// Мок с контрактом Bot API; заменить createTelegram() при появлении доступов Татьяны.
export function mockTelegram({ fail } = {}) {
const sent = [];
return {
channel: "telegram",
sent,
secrets: () => ["MOCK_TOKEN"],
connected: () => true,
async send(text) {
if (fail) throw new Error(fail);
const m = { message_id: sent.length + 1, text };
sent.push(m);
return m;
},
};
}