feat(api, web): добавить поддержку уведомлений и журнал уведомлений
Docker / build (push) Has been cancelled
Docker / build (push) Has been cancelled
Добавлены новые функции для отправки уведомлений через Telegram и webhook, включая настройки для интервалов уведомлений и проверки uptime. Реализован журнал уведомлений для отслеживания статуса отправленных сообщений. Обновлены схемы и интерфейсы для поддержки новых полей и функционала. Co-authored-by: Cursor <[email protected]>
This commit is contained in:
@@ -2,20 +2,27 @@
|
||||
* Telegram Bot API — отправка уведомлений
|
||||
*/
|
||||
|
||||
export interface TelegramSendResult {
|
||||
ok: boolean
|
||||
error?: string
|
||||
}
|
||||
|
||||
export async function sendTelegramMessage(
|
||||
token: string,
|
||||
chatIds: string | string[],
|
||||
text: string,
|
||||
messageThreadId?: string | number | null,
|
||||
): Promise<void> {
|
||||
if (!token?.trim() || !text?.trim()) return
|
||||
): Promise<TelegramSendResult> {
|
||||
if (!token?.trim() || !text?.trim()) {
|
||||
return { ok: false, error: 'Пустой токен или текст' }
|
||||
}
|
||||
const ids = Array.isArray(chatIds)
|
||||
? chatIds
|
||||
: String(chatIds || '')
|
||||
.split(',')
|
||||
.map((id) => id.trim())
|
||||
.filter(Boolean)
|
||||
if (ids.length === 0) return
|
||||
if (ids.length === 0) return { ok: false, error: 'Не указан chat ID' }
|
||||
|
||||
const threadId = messageThreadId != null && messageThreadId !== '' ? Number(messageThreadId) : null
|
||||
const payload: Record<string, unknown> = {
|
||||
@@ -26,6 +33,9 @@ export async function sendTelegramMessage(
|
||||
if (Number.isFinite(threadId)) payload.message_thread_id = threadId
|
||||
|
||||
const url = `https://api.telegram.org/bot${token.trim()}/sendMessage`
|
||||
const errors: string[] = []
|
||||
let anyOk = false
|
||||
|
||||
for (const chatId of ids) {
|
||||
try {
|
||||
const res = await fetch(url, {
|
||||
@@ -34,12 +44,20 @@ export async function sendTelegramMessage(
|
||||
body: JSON.stringify({ ...payload, chat_id: chatId }),
|
||||
})
|
||||
const data = (await res.json().catch(() => ({}))) as { ok?: boolean; description?: string }
|
||||
if (!data.ok) {
|
||||
console.warn(`Telegram sendMessage failed for chat ${chatId}:`, data.description || res.statusText)
|
||||
if (data.ok) {
|
||||
anyOk = true
|
||||
} else {
|
||||
const err = data.description || res.statusText || 'Unknown error'
|
||||
errors.push(`${chatId}: ${err}`)
|
||||
console.warn(`Telegram sendMessage failed for chat ${chatId}:`, err)
|
||||
}
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err)
|
||||
errors.push(`${chatId}: ${message}`)
|
||||
console.warn(`Telegram sendMessage error for chat ${chatId}:`, message)
|
||||
}
|
||||
}
|
||||
|
||||
if (anyOk) return { ok: true }
|
||||
return { ok: false, error: errors.join('; ') || 'Не удалось отправить' }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user