Skip to content

Instantly share code, notes, and snippets.

@mayneyao
Last active April 6, 2021 04:13
Show Gist options
  • Save mayneyao/0688303382f999f9cfeed082a733fc0e to your computer and use it in GitHub Desktop.
Save mayneyao/0688303382f999f9cfeed082a733fc0e to your computer and use it in GitHub Desktop.
// 钉钉机器人 webhook 请求,在 figma 插件中存在跨域限制,这个 proxy 脚本可以解决这个问题。
// https://gist.github.com/mayneyao/0688303382f999f9cfeed082a733fc0e
// deno deploy 暂不支持私密仓库的部署,先使用公开的 gist。
async function handleRequest(request) {
// For making a POST request we need to specify the method property
// as POST and provide data to the body property in the same object.
// https://post.deno.dev echoes data we POST to it.
const url = new URL(request.url);
const text = url.searchParams.get("text")
const messageUrl = url.searchParams.get("messageUrl")
const picUrl = url.searchParams.get("picUrl")
const title = url.searchParams.get("title")
const data = {
"msgtype": "link",
"link": {
"text": text,
"title": title || "VIKA 设计信息同步",
"picUrl": picUrl || '',
"messageUrl": messageUrl,
}
}
// 钉钉机器人 webhook 的 token。
const token = Deno.env.get('token');
const dingtalk_bot_api_endpoint = `https://oapi.dingtalk.com/robot/send?access_token=${token}`
const response = await fetch(dingtalk_bot_api_endpoint, {
method: "POST",
headers: {
// This headers implies to the server that the content of
// body is JSON and is encoded using UTF-8.
"content-type": "application/json; charset=UTF-8",
},
body: JSON.stringify(data),
});
if (response.ok) {
// The echo server returns the data back in
const json = await response.json();
return new Response(JSON.stringify(json), {
headers: {
"content-type": "application/json; charset=UTF-8",
},
});
}
return new Response(
JSON.stringify({ message: "couldn't process your request" }),
{
status: 500,
headers: {
"content-type": "application/json; charset=UTF-8",
},
},
);
}
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment