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);
}
};
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.