hack-club-eligiblity/index.ts
2024-09-04 00:12:02 +01:00

74 lines
2.6 KiB
TypeScript

import type { User } from "@slack/web-api/dist/response/UsersInfoResponse";
const { App, ExpressReceiver } = (await import("@slack/bolt"));
import "dotenv/config";
const app = new App({
token: process.env.SLACK_BOT_TOKEN,
signingSecret: process.env.SLACK_SIGNING_SECRET,
});
app.command("/check-eligiblity", async ctx => {
await ctx.ack();
const text = ctx.command.text.slice();
let match;
let userId = ctx.context.userId;
let matchedBy = "no input"
if (match = text.match(/\<\@(.+)\|(.+)>/)) {
userId = match[1];
matchedBy = "user mention"
} else if (text)
matchedBy = "invalid input"
const res = await fetch("https://verify.hackclub.dev/api/status", {
method: "POST",
headers: { 'content-type': 'application/json' },
body: JSON.stringify({
"slack_id": userId
}),
redirect: "follow"
}).then(res => res.json())
if (res === `User ${userId} not found!`)
return await ctx.respond({
response_type: 'ephemeral',
text: `Either ${matchedBy !== "user mention" ? "you haven't" : `<@${userId}> hasn't`} verified, or ${matchedBy !== "user mention" ? "your" : "their"} verification hasn't been accepted.${matchedBy !== "user mention" ? "\nCheck out the <https://forms.hackclub.com/eligibility|eligiblity form> to verify." : ""}`,
unfurl_links: true
})
else {
return await ctx.respond({
response_type: 'ephemeral',
text: `${matchedBy !== "user mention" ? "You have verified your" : `<@${userId}> has verified their`} student status, and ${matchedBy !== "user mention" ? "are" : "is"} ${res.status}.`,
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
text: `${matchedBy !== "user mention" ? "You have verified your" : `<@${userId}> has verified their`} student status, and ${matchedBy !== "user mention" ? "are" : "is"} *${res.status}*.`
}
},
...(matchedBy == "user mention" ? [] : [
{
type: 'section',
// @ts-ignore silly typings
text: {
type: 'mrkdwn',
text: `*Raw JSON output from the Eligiblity API:*\n` + "```\n" + JSON.stringify(res, null, 2) + "\n```"
}
}
])
]
})
}
})
;(async () => {
await app.start(60275);
console.log('⚡️ Bolt app is running!');
})();