Public
Script
October 23, 2023
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
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
import process from "node:process";
export const slackReplyToMessage = async (
req: express.Request,
res: express.Response,
) => {
// Verify the request is genuine
if (req.body.token !== process.env.slackVerificationToken) {
return res.status(401);
}
// Respond to the initial challenge (when events are enabled)
if (req.body.challenge) {
return res.send({ challenge: req.body.challenge });
}
// Reply to app_mention events
if (req.body.event.type === "app_mention") {
// Note: `req.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 ${process.env.slackToken}`,
},
method: "POST",
body: JSON.stringify({
channel: req.body.event.channel,
thread_ts: req.body.event.ts,
text: "Hello, ~World~ from Val Town!",
}),
},
);
// Slack replies with information about the created message
console.log(result);
}
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
October 23, 2023