mirror of
https://github.com/chrononeko/chronocat.git
synced 2024-11-16 12:51:39 +00:00
feat(shell): satori: add webhook
This commit is contained in:
parent
7ace01cfc8
commit
273392ff8d
47
packages/shell/src/satori/webhook.ts
Normal file
47
packages/shell/src/satori/webhook.ts
Normal 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,
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user