Public
HTTP (deprecated)
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

A Slack bot which rephrases messages in a channel in the voice of a Pirate.

Originally built for National Talk Like a Pirate Day, September 19, 2024.

https://dctalbot.nyc/blog/pirate-talk/

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
58
59
60
import { OpenAI } from "https://esm.town/v/std/openai";
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
const openai = new OpenAI();
const ok = new Response(undefined, { status: 200 });
const unauthorized = new Response(undefined, { status: 401 });
async function ahoy(prompt: string): Promise<string> {
const completion = await openai.chat.completions.create({
messages: [
{ role: "system", content: "You are a pirate. Rephrase everything in the voice of a pirate." },
{ role: "user", content: prompt },
],
model: "gpt-4",
});
return completion.choices[0].message.content;
}
export const slackReplyToMessage = async (req: Request) => {
const { event, token, challenge } = await req.json();
if (token !== Deno.env.get("slackVerificationToken")) {
return unauthorized;
}
if (challenge) {
return Response.json({ challenge });
}
const skip: boolean[] = [
event?.type !== "message",
event?.subtype,
event?.bot_id,
event?.bot_profile,
];
if (skip.some(x => x)) {
return ok;
}
const pirateSpeak = await ahoy(event.text);
const result = await fetchJSON(
"https://slack.com/api/chat.postMessage",
{
headers: {
"Authorization": `Bearer ${Deno.env.get("slackToken")}`,
},
method: "POST",
body: JSON.stringify({
channel: event.channel,
thread_ts: event.ts,
text: pirateSpeak,
}),
},
);
return ok;
};
dctalbot-piratetalk.web.val.run
September 16, 2024