txc-webhooks/api/txc.ts
2021-12-21 14:31:29 +08:00

55 lines
1.7 KiB
TypeScript

import {VercelRequest, VercelResponse} from '@vercel/node'
function handleMessage(name: string, data: any): string {
let action = ''
let content = ''
switch (data.type) {
// 主贴发表
case 'post.created':
action = '主贴发表'
content = data?.payload?.post?.content
break
// 主贴更新
case 'post.updated':
action = '主贴更新'
content = data?.payload?.post?.content
break
// 回复发表
case 'reply.created':
action = '回复发表'
content = data?.payload?.reply?.content
break
// 回复更新
case 'reply.updated':
action = '回复更新'
content = data?.payload?.reply?.content
break
default:
throw new Error('Bad Request')
}
return `${name}${action}: ${content}`
}
async function main(req: VercelRequest, res: VercelResponse) {
if (req.method !== 'POST') {
res.setHeader('Allow', 'POST')
return res.status(405).end('Method Not Allowed')
}
const bots = (process.env.BOTS || '').split(',').map(v => v.toUpperCase())
try {
const product = req.query.product.toString() || 'Unknown'
const content = handleMessage(product, req.body)
if (bots.indexOf('TELEGRAM') >= 0) {
await sendToTelegram(content)
}
if (bots.indexOf('FEISHU') >= 0) {
await sendToFeishu(content)
}
return res.status(200).end('ok')
} catch (e) {
console.error(e)
return res.status(500).end(e.message || 'Server Error')
}
}
export default main