From db12f8721616150b9d4ca0e615646bad47cb3f40 Mon Sep 17 00:00:00 2001 From: tangyanbiao Date: Fri, 15 Oct 2021 15:40:45 +0800 Subject: [PATCH] feat: test ok --- .gitignore | 3 + api/bot/feishu.ts | 70 +++++++++++++++++++++++ package.json | 13 +++++ vercel.json | 5 ++ yarn.lock | 142 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 233 insertions(+) create mode 100644 .gitignore create mode 100644 api/bot/feishu.ts create mode 100644 package.json create mode 100644 vercel.json create mode 100644 yarn.lock diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..88596c0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.vercel +node_modules +.env.* \ No newline at end of file diff --git a/api/bot/feishu.ts b/api/bot/feishu.ts new file mode 100644 index 0000000..32d51ad --- /dev/null +++ b/api/bot/feishu.ts @@ -0,0 +1,70 @@ +import {VercelRequest, VercelResponse} from '@vercel/node' +import fetch from 'node-fetch' + +async function send(content: string) { + const body = JSON.stringify({ + "msg_type": "text", + "content": { + "text": content + } + }) + const res = await fetch(process.env.WEBHOOK_URL, { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body, + }) + const data = await res.json() + if (data.StatusCode !== 0) { + console.error(data.StatusMessage) + } +} + +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') + } + try { + const product = req.query.product.toString() || 'Unknown' + const content = handleMessage(product, req.body) + await send(content) + return res.status(200).end('ok') + } catch (e) { + console.error(e) + return res.status(500).end(e.message || 'Server Error') + } +} + +export default main \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..f0a3b74 --- /dev/null +++ b/package.json @@ -0,0 +1,13 @@ +{ + "name": "txc-webhook", + "version": "1.0.0", + "main": "index.js", + "license": "MIT", + "dependencies": { + "@vercel/node": "^1.12.1", + "node-fetch": "^2.6.5" + }, + "devDependencies": { + "@types/node-fetch": "^2.5.12" + } +} diff --git a/vercel.json b/vercel.json new file mode 100644 index 0000000..638784d --- /dev/null +++ b/vercel.json @@ -0,0 +1,5 @@ +{ + "rewrites": [ + { "source": "/hooks/:product", "destination": "/api/bot/feishu" } + ] +} \ No newline at end of file diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000..6fa9f93 --- /dev/null +++ b/yarn.lock @@ -0,0 +1,142 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +"@types/node-fetch@^2.5.12": + version "2.5.12" + resolved "https://registry.nlark.com/@types/node-fetch/download/@types/node-fetch-2.5.12.tgz?cache=0&sync_timestamp=1630688964453&other_urls=https%3A%2F%2Fregistry.nlark.com%2F%40types%2Fnode-fetch%2Fdownload%2F%40types%2Fnode-fetch-2.5.12.tgz#8a6f779b1d4e60b7a57fb6fd48d84fb545b9cc66" + integrity sha1-im93mx1OYLelf7b9SNhPtUW5zGY= + dependencies: + "@types/node" "*" + form-data "^3.0.0" + +"@types/node@*": + version "16.11.0" + resolved "https://registry.npmmirror.com/@types/node/download/@types/node-16.11.0.tgz#4b95f2327bacd1ef8f08d8ceda193039c5d7f52e" + integrity sha1-S5XyMnus0e+PCNjO2hkwOcXX9S4= + +"@vercel/node@^1.12.1": + version "1.12.1" + resolved "https://registry.npmmirror.com/@vercel/node/download/@vercel/node-1.12.1.tgz#15f42f64690f904f8a52a387123ce0958657060f" + integrity sha1-FfQvZGkPkE+KUqOHEjzglYZXBg8= + dependencies: + "@types/node" "*" + ts-node "8.9.1" + typescript "4.3.4" + +arg@^4.1.0: + version "4.1.3" + resolved "https://registry.nlark.com/arg/download/arg-4.1.3.tgz?cache=0&sync_timestamp=1629166537485&other_urls=https%3A%2F%2Fregistry.nlark.com%2Farg%2Fdownload%2Farg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089" + integrity sha1-Jp/HrVuOQstjyJbVZmAXJhwUQIk= + +asynckit@^0.4.0: + version "0.4.0" + resolved "https://registry.npm.taobao.org/asynckit/download/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79" + integrity sha1-x57Zf380y48robyXkLzDZkdLS3k= + +buffer-from@^1.0.0: + version "1.1.2" + resolved "https://registry.nlark.com/buffer-from/download/buffer-from-1.1.2.tgz#2b146a6fd72e80b4f55d255f35ed59a3a9a41bd5" + integrity sha1-KxRqb9cugLT1XSVfNe1Zo6mkG9U= + +combined-stream@^1.0.8: + version "1.0.8" + resolved "https://registry.nlark.com/combined-stream/download/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f" + integrity sha1-w9RaizT9cwYxoRCoolIGgrMdWn8= + dependencies: + delayed-stream "~1.0.0" + +delayed-stream@~1.0.0: + version "1.0.0" + resolved "https://registry.npm.taobao.org/delayed-stream/download/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619" + integrity sha1-3zrhmayt+31ECqrgsp4icrJOxhk= + +diff@^4.0.1: + version "4.0.2" + resolved "https://registry.nlark.com/diff/download/diff-4.0.2.tgz?cache=0&sync_timestamp=1622685824253&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fdiff%2Fdownload%2Fdiff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d" + integrity sha1-YPOuy4nV+uUgwRqhnvwruYKq3n0= + +form-data@^3.0.0: + version "3.0.1" + resolved "https://registry.npm.taobao.org/form-data/download/form-data-3.0.1.tgz#ebd53791b78356a99af9a300d4282c4d5eb9755f" + integrity sha1-69U3kbeDVqma+aMA1CgsTV65dV8= + dependencies: + asynckit "^0.4.0" + combined-stream "^1.0.8" + mime-types "^2.1.12" + +make-error@^1.1.1: + version "1.3.6" + resolved "https://registry.npm.taobao.org/make-error/download/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2" + integrity sha1-LrLjfqm2fEiR9oShOUeZr0hM96I= + +mime-db@1.50.0: + version "1.50.0" + resolved "https://registry.nlark.com/mime-db/download/mime-db-1.50.0.tgz#abd4ac94e98d3c0e185016c67ab45d5fde40c11f" + integrity sha1-q9SslOmNPA4YUBbGerRdX95AwR8= + +mime-types@^2.1.12: + version "2.1.33" + resolved "https://registry.npmmirror.com/mime-types/download/mime-types-2.1.33.tgz#1fa12a904472fafd068e48d9e8401f74d3f70edb" + integrity sha1-H6EqkERy+v0GjkjZ6EAfdNP3Dts= + dependencies: + mime-db "1.50.0" + +node-fetch@^2.6.5: + version "2.6.5" + resolved "https://registry.npmmirror.com/node-fetch/download/node-fetch-2.6.5.tgz#42735537d7f080a7e5f78b6c549b7146be1742fd" + integrity sha1-QnNVN9fwgKfl94tsVJtxRr4XQv0= + dependencies: + whatwg-url "^5.0.0" + +source-map-support@^0.5.17: + version "0.5.20" + resolved "https://registry.nlark.com/source-map-support/download/source-map-support-0.5.20.tgz?cache=0&sync_timestamp=1631179636992&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fsource-map-support%2Fdownload%2Fsource-map-support-0.5.20.tgz#12166089f8f5e5e8c56926b377633392dd2cb6c9" + integrity sha1-EhZgifj15ejFaSazd2Mzkt0stsk= + dependencies: + buffer-from "^1.0.0" + source-map "^0.6.0" + +source-map@^0.6.0: + version "0.6.1" + resolved "https://registry.nlark.com/source-map/download/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263" + integrity sha1-dHIq8y6WFOnCh6jQu95IteLxomM= + +tr46@~0.0.3: + version "0.0.3" + resolved "https://registry.npmmirror.com/tr46/download/tr46-0.0.3.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.npmmirror.com%2Ftr46%2Fdownload%2Ftr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a" + integrity sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o= + +ts-node@8.9.1: + version "8.9.1" + resolved "https://registry.npmmirror.com/ts-node/download/ts-node-8.9.1.tgz#2f857f46c47e91dcd28a14e052482eb14cfd65a5" + integrity sha1-L4V/RsR+kdzSihTgUkgusUz9ZaU= + dependencies: + arg "^4.1.0" + diff "^4.0.1" + make-error "^1.1.1" + source-map-support "^0.5.17" + yn "3.1.1" + +typescript@4.3.4: + version "4.3.4" + resolved "https://registry.npmmirror.com/typescript/download/typescript-4.3.4.tgz?cache=0&sync_timestamp=1634196174151&other_urls=https%3A%2F%2Fregistry.npmmirror.com%2Ftypescript%2Fdownload%2Ftypescript-4.3.4.tgz#3f85b986945bcf31071decdd96cf8bfa65f9dcbc" + integrity sha1-P4W5hpRbzzEHHezdls+L+mX53Lw= + +webidl-conversions@^3.0.0: + version "3.0.1" + resolved "https://registry.nlark.com/webidl-conversions/download/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871" + integrity sha1-JFNCdeKnvGvnvIZhHMFq4KVlSHE= + +whatwg-url@^5.0.0: + version "5.0.0" + resolved "https://registry.npmmirror.com/whatwg-url/download/whatwg-url-5.0.0.tgz?cache=0&sync_timestamp=1633539062585&other_urls=https%3A%2F%2Fregistry.npmmirror.com%2Fwhatwg-url%2Fdownload%2Fwhatwg-url-5.0.0.tgz#966454e8765462e37644d3626f6742ce8b70965d" + integrity sha1-lmRU6HZUYuN2RNNib2dCzotwll0= + dependencies: + tr46 "~0.0.3" + webidl-conversions "^3.0.0" + +yn@3.1.1: + version "3.1.1" + resolved "https://registry.nlark.com/yn/download/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50" + integrity sha1-HodAGgnXZ8HV6rJqbkwYUYLS61A=