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
import { fetch } from "https://esm.town/v/std/fetch";
import process from "node:process";
export async function handleGroupmeMessage(
req: express.Request,
res: express.Response,
) {
const getRandomRhyme = async (word: string) => {
const rhymeRes = await fetch(
`https://api.datamuse.com/words?rel_rhy=${word}`,
);
const rhymes = await rhymeRes.json();
if (rhymes.length > 0) {
let randomIndex = Math.floor(Math.random() * rhymes.length);
return rhymes[randomIndex].word;
}
else {
return "";
}
};
const sendGroupmeMessage = async (bot_id: string, text: string) => {
const response = await fetch("https://api.groupme.com/v3/bots/post", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
redirect: "follow",
body: JSON.stringify({ bot_id, text }),
});
const result = await response;
return result;
};
if (req.body.text && req.body.text.toLowerCase().startsWith("x-up-y-up")) {
let textArray = req.body.text.split(" ");
if (textArray.length > 1) {
let rhymeCheck = textArray.at(-1) == "tuss" ? "bus" : textArray.at(-1);
let rhyme = await getRandomRhyme(rhymeCheck);
const joinedText = textArray.slice(1).join(" ");
const text = rhyme
? `${joinedText} up ${rhyme} up`
: `Couldn't find rhyme for ${joinedText}`;
await sendGroupmeMessage(process.env.groupmeBotId, text);
}
}
}