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
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
export const slackReplyToMessage = async (req: Request) => {
const body = await req.json();
// Verify the request is genuine
if (body.token !== Deno.env.get("slackVerificationToken")) {
return new Response(undefined, { status: 401 });
}
// Respond to the initial challenge (when events are enabled)
if (body.challenge) {
return Response.json({ challenge: body.challenge });
}
// Reply to app_mention events
if (body.event.type === "app_mention") {
// Note: `body.event` has information about the event
// like the sender and the message text
const result = await fetchJSON(
"https://slack.com/api/chat.postMessage",
{
headers: {
"Authorization": `Bearer ${Deno.env.get("slackToken")}`,
},
method: "POST",
body: JSON.stringify({
channel: body.event.channel,
thread_ts: body.event.ts,
text: "Hello, ~World~ from Val Town!",
}),
},
);
// Slack replies with information about the created message
console.log(result);
}
};