-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
57 lines (54 loc) · 1.68 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
addEventListener("fetch", (event: FetchEvent): void => {
/* Handle available API routes */
const { pathname } = new URL(event.request.url);
switch (pathname) {
case "/api/slack":
return event.respondWith(handleSlackRequest(event.request));
default:
return event.respondWith(
new Response(JSON.stringify({ error: "Not Found" }, null, 2), {
status: 404,
}),
);
}
});
async function handleSlackRequest(request: Request): Promise<Response> {
try {
const data = await request?.json();
switch (data?.type) {
/* Handle Slack URL Verification */
case "url_verification":
return new Response(JSON.stringify(data?.challenge));
/* Handle Slack Event Callbacks */
case "event_callback":
if (
data?.event.type === "emoji_changed" && data?.event.subtype === "add"
) {
fetch("https://slack.com/api/chat.postMessage", {
method: "POST",
headers: {
"Authorization": `Bearer ${Deno.env.get("SLACK_OAUTH_TOKEN")}`,
"Content-Type": "application/json; charset=utf-8",
},
body: JSON.stringify({
channel: Deno.env.get("SLACK_CHANNEL"),
text: `Emoji :${data?.event.name}: appeared!`,
}),
}).then(async (response: Response) => console.log(await response.json()))
.catch(console.error);
}
/* falls through */
default:
return new Response(null, {
status: 204,
});
}
} catch {
return new Response(
JSON.stringify({ error: "Unknown Error Occured" }, null, 2),
{
status: 500,
},
);
}
}