feat(shell): satori: add webhook

This commit is contained in:
Il Harper 2024-03-05 00:57:45 +08:00
parent 7ace01cfc8
commit 273392ff8d
No known key found for this signature in database
GPG Key ID: 4B71FCA698E7E8EC

View File

@ -0,0 +1,47 @@
import fetch from 'node-fetch'
import type { ChronocatSatoriWebHookConfig } from '../services/config/configEntity'
import type { ChronocatContext, DispatchMessage } from '../types'
import { buildEventIdCounter } from '../utils/token'
export const initSatoriWebHook = async (
cctx: ChronocatContext,
config: ChronocatSatoriWebHookConfig,
) => {
// 预处理 self_url
if (!config.self_url || config.self_url === 'https://chronocat.vercel.app')
config.self_url = `http://127.0.0.1:5500`
if (config.self_url.endsWith('/'))
config.self_url = config.self_url.slice(0, config.self_url.length - 1)
const getId = buildEventIdCounter()
const dispatcher = (message: DispatchMessage) => {
void (async () => {
const uin = (await cctx.chronocat.getAuthData()).uin
await message.toSatori(uin, config).then((events) =>
events.forEach((data) => {
const body = {
...data,
id: getId(),
platform: cctx.chronocat.platform,
self_id: uin,
}
const headers: HeadersInit = {}
if (config.token) headers['Authorization'] = `Bearer ${config.token}`
void fetch(config.url, {
method: 'POST',
headers,
body: JSON.stringify(body),
})
}),
)
})()
}
return {
dispatcher,
}
}