Files
kt-financial-system/apps/backend/api/telegram/notifications.post.ts
你的用户名 a4e4168c00
Some checks failed
Deploy to Production / Build and Test (push) Has been cancelled
Deploy to Production / Deploy to Server (push) Has been cancelled
feat: 添加Telegram Bot通知功能
 新功能:
- 添加Telegram Bot通知支持
- 账目记录自动推送到Telegram
- 支持多个Bot配置管理
- 支持群组和个人通知

📊 数据库:
- 新增telegram_notification_configs表
- 存储Bot配置和通知类型

🔧 后端API:
- GET /api/telegram/notifications - 获取所有配置
- POST /api/telegram/notifications - 创建配置
- PUT /api/telegram/notifications/:id - 更新配置
- DELETE /api/telegram/notifications/:id - 删除配置
- POST /api/telegram/test - 测试Bot配置

💬 通知功能:
- 自动发送账目记录通知
- 包含交易类型、金额、分类、账户等信息
- 支持格式化显示(类型图标、状态标识)
- 配置创建时自动测试有效性

📝 文档:
- 添加完整的使用说明文档
- API接口说明和示例
- 常见问题解答
2025-11-04 23:15:19 +08:00

56 lines
1.5 KiB
TypeScript

import { readBody } from 'h3';
import db from '~/utils/sqlite';
import { useResponseError, useResponseSuccess } from '~/utils/response';
import { testTelegramConfig } from '~/utils/telegram-bot';
export default defineEventHandler(async (event) => {
const body = await readBody(event);
if (!body?.name || !body?.botToken || !body?.chatId) {
return useResponseError('缺少必填字段', -1);
}
const notificationTypes = Array.isArray(body.notificationTypes)
? body.notificationTypes
: ['transaction'];
// 测试配置是否有效
const testResult = await testTelegramConfig(body.botToken, body.chatId);
if (!testResult.success) {
return useResponseError(
`Telegram配置测试失败: ${testResult.error}`,
-1,
);
}
const now = new Date().toISOString();
const result = db
.prepare<unknown, [string, string, string, string, number, string, string]>(
`
INSERT INTO telegram_notification_configs (name, bot_token, chat_id, notification_types, is_enabled, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?)
`,
)
.run(
body.name,
body.botToken,
body.chatId,
JSON.stringify(notificationTypes),
body.isEnabled !== false ? 1 : 0,
now,
now,
);
return useResponseSuccess({
id: result.lastInsertRowid,
name: body.name,
botToken: body.botToken,
chatId: body.chatId,
notificationTypes,
isEnabled: body.isEnabled !== false,
createdAt: now,
updatedAt: now,
});
});