Something went wrong. Try again.
providing password reset services for a long while: circa 2025
Something went wrong. Try again.
7.4 kB · 243 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244import { slackApp } from "../index";import { fetchUserData } from "./unfurl";
const command = async () => { slackApp.command("/hackatime", async ({ context, payload }) => { if (!context?.respond) return; const hackatimeUser: { apiKey: string } | null = await fetch( `https://waka.hackclub.com/api/special/apikey?user=${context.userId}`, { headers: { Authorization: `Bearer ${process.env.HACKATIME_API_KEY}`, }, }, ).then((res) => (res.status === 200 ? res.json() : null)); console.log(payload.text); if (hackatimeUser) { if (payload.text.includes("summary")) { const interval = payload.text.split(" ")[1] || "month"; const userData = await fetchUserData(context.userId, interval); if (!userData) { await context.respond({ response_type: "ephemeral", text: "uh oh! something went wrong :ohnoes:", blocks: [ { type: "section", text: { type: "mrkdwn", text: "uh oh! something went wrong :ohnoes:", }, }, { type: "context", elements: [ { type: "mrkdwn", text: "if this keeps happening dm <@U062UG485EE> and let them know", }, ], }, ], }); return; }
const projectTotal = userData.projects.reduce((total, project) => { return total + project.total; }, 0); userData.projects.sort((a, b) => b.total - a.total);
await context.respond({ response_type: "ephemeral", text: "here's your summary! :yay:", blocks: [ { type: "section", text: { type: "mrkdwn", text: `here's your summary <@${context.userId}>! :roo-yay:`, }, }, { type: "divider", }, { type: "context", elements: [ { type: "mrkdwn", text: `you have spent ${Math.floor(projectTotal / 3600)} hours, ${Math.floor((projectTotal % 3600) / 60)} minutes, and ${projectTotal % 60} seconds coding in the ${interval.replaceAll("_", " ")}${interval.includes("days") || interval.includes("month") ? "" : " interval"}`, }, ], }, { type: "divider", }, { type: "context", elements: [ { type: "mrkdwn", text: `your most active project was \`${userData.projects[0].key}\`, where you spent ${Math.floor(userData.projects[0].total / 3600)} hours, ${Math.floor((userData.projects[0].total % 3600) / 60)} minutes, and ${userData.projects[0].total % 60} seconds`, }, ], }, { type: "divider", }, { type: "context", elements: [ { type: "mrkdwn", text: `here's a list of the rest of your projects:\n\n${userData.projects .slice(1) .map( (project) => `\`${project.key}\`: ${Math.floor(project.total / 3600)} hours, ${Math.floor((project.total % 3600) / 60)} minutes, and ${project.total % 60} seconds`, ) .join("\n")}`, }, ], }, { type: "divider", }, { type: "actions", elements: [ { type: "button", text: { type: "plain_text", text: "share with channel", }, value: interval, action_id: "share-summary", }, ], }, ], }); return; }
await context.respond({ response_type: "ephemeral", text: "Hi there! I'm the Hackatime bot :hyper-dino-wave:", blocks: [ { type: "section", text: { type: "mrkdwn", text: "Hi there! I'm the Hackatime bot :hyper-dino-wave:", }, }, { type: "section", text: { type: "mrkdwn", text: `It looks like you already have an account! You can log in to the <https://waka.hackclub.com/login|Hackatime dashboard> with your username \`${context.userId}\` and password :3c:`, }, }, { type: "actions", elements: [ { type: "button", text: { type: "plain_text", text: "Reset Password", }, action_id: "reset-password", style: "danger", }, { type: "button", text: { type: "plain_text", text: "gutentag!", }, action_id: "bye", }, ], }, { type: "context", elements: [ { type: "mrkdwn", text: `your api key for the Hackatime API is \`${hackatimeUser.apiKey}\``, }, ], }, ], }); return; }
context.respond({ response_type: "ephemeral", text: "Hi there! I'm the Hackatime bot :hyper-dino-wave:", blocks: [ { type: "section", text: { type: "mrkdwn", text: "Hi there! I'm the Hackatime bot :hyper-dino-wave:", }, }, { type: "section", text: { type: "mrkdwn", text: "I can see that you don't have an account yet. Do you want me to make you one?", }, }, { type: "actions", elements: [ { type: "button", text: { type: "plain_text", text: "Yes, please!", }, value: "yes", style: "primary", action_id: "create-account", }, { type: "button", text: { type: "plain_text", text: "No, thanks", }, value: "no", action_id: "no-thanks", }, ], }, ], }); });
slackApp.action("no-thanks", async ({ context }) => { if (context?.respond) await context.respond({ response_type: "ephemeral", text: "No worries! If you change your mind just type `/hackatime` again ^-^", }); });
slackApp.action("bye", async ({ context }) => { if (context?.respond) await context.respond({ response_type: "ephemeral", text: "bye!!! :goodnight:", }); });};
export default command;