diff --git a/back/data/notify.ts b/back/data/notify.ts index 10ee8558..4def4888 100644 --- a/back/data/notify.ts +++ b/back/data/notify.ts @@ -18,6 +18,7 @@ export enum NotificationMode { 'pushMe' = 'pushMe', 'feishu' = 'feishu', 'webhook' = 'webhook', + 'chronocat' = 'Chronocat', } abstract class NotificationBaseInfo { @@ -108,6 +109,12 @@ export class PushMeNotification extends NotificationBaseInfo { public pushMeKey: string = ''; } +export class ChronocatNotification extends NotificationBaseInfo { + public chronocatURL: string = ''; + public chronocatQQ: string = ''; + public chronocatToekn: string = ''; +} + export class WebhookNotification extends NotificationBaseInfo { public webhookHeaders: string = ''; public webhookBody: string = ''; @@ -140,4 +147,6 @@ export interface NotificationInfo EmailNotification, PushMeNotification, WebhookNotification, + ChronocatNotification, LarkNotification {} + diff --git a/back/services/notify.ts b/back/services/notify.ts index c4ea67f0..55827b41 100644 --- a/back/services/notify.ts +++ b/back/services/notify.ts @@ -1,12 +1,12 @@ -import { NotificationInfo } from '../data/notify'; -import { Service, Inject } from 'typedi'; -import winston from 'winston'; -import UserService from './user'; -import got from 'got'; -import nodemailer from 'nodemailer'; import crypto from 'crypto'; +import got from 'got'; import { HttpProxyAgent, HttpsProxyAgent } from 'hpagent'; +import nodemailer from 'nodemailer'; +import { Inject, Service } from 'typedi'; +import winston from 'winston'; import { parseBody, parseHeaders } from '../config/util'; +import { NotificationInfo } from '../data/notify'; +import UserService from './user'; @Service() export default class NotificationService { @@ -31,6 +31,7 @@ export default class NotificationService { ['pushMe', this.pushMe], ['webhook', this.webhook], ['lark', this.lark], + ['chronocat', this.chronocat], ]); private title = ''; @@ -195,7 +196,8 @@ export default class NotificationService { } private async bark() { - let { barkPush, barkIcon, barkSound, barkGroup, barkLevel, barkUrl } = this.params; + let { barkPush, barkIcon, barkSound, barkGroup, barkLevel, barkUrl } = + this.params; if (!barkPush.startsWith('http')) { barkPush = `https://api.day.app/${barkPush}`; } @@ -588,6 +590,63 @@ export default class NotificationService { } } + private async chronocat() { + const { chronocatURL, chronocatQQ, chronocatToekn } = this.params; + try { + const user_ids = chronocatQQ + .match(/user_id=(\d+)/g) + ?.map((match: any) => match.split('=')[1]); + const group_ids = chronocatQQ + .match(/group_id=(\d+)/g) + ?.map((match: any) => match.split('=')[1]); + + const url = `${chronocatURL}/api/message/send`; + const headers = { + 'Content-Type': 'application/json', + Authorization: `Bearer ${chronocatToekn}`, + }; + + for (const [chat_type, ids] of [ + [1, user_ids], + [2, group_ids], + ]) { + if (!ids) { + continue; + } + let _ids: any = ids; + for (const chat_id of _ids) { + const data = { + peer: { + chatType: chat_type, + peerUin: chat_id, + }, + elements: [ + { + elementType: 1, + textElement: { + content: `${this.title}\n\n${this.content}`, + }, + }, + ], + }; + const res: any = await got.post(url, { + ...this.gotOption, + json: data, + headers, + }); + if (res.body === 'success') { + return true; + } else { + throw new Error(res.body); + } + } + } + return false; + } catch (error: any) { + throw new Error(error.response ? error.response.body : error); + } + } + private async webhook() { const { webhookUrl,