Skip to content

Commit

Permalink
Add daily fact
Browse files Browse the repository at this point in the history
  • Loading branch information
AJaccP committed Jul 28, 2024
1 parent ebe9ef9 commit 16893c0
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/discord/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface DiscordItemMessage {
includeLinks: boolean;
}[];
message: string;
fact: string;
}

// TODO: any type
Expand All @@ -31,10 +32,11 @@ export const sendDiscordItemMessage = async (
.join("\n");
return `${sectionHeader} ${sectionItems}`;
});
const messageFact = formatFact(message.fact);

try {
const response = await axios.post(webhookUrl, {
content: `${messageHeader} ${messageSections.join()}`,
content: `${messageHeader} ${messageSections.join()} ${messageFact}`,
});
return Ok(response.data);
} catch (error) {
Expand Down Expand Up @@ -69,6 +71,10 @@ const formatMessageSectionTitle = (title: string) => {
return `\n### ${title}: \n`;
};

const formatFact = (fact: string) => {
return `\n### 📚 Fun Fact: \n ${fact}!`;
};

const formatDiscordAssignees = (assignees: string[]) => {
// TODO: we should filter out the github url before getting to this stuff
return assignees
Expand Down
6 changes: 4 additions & 2 deletions src/github/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import axios from "axios";
import { Ok, Err, Result } from "ts-results";
import dotenv from "dotenv";
import { PROJECT_V2_ITEMS } from "./graphql";

dotenv.config();

// TODO: improve this
export interface ProjectV2Item {
id: string;
Expand Down Expand Up @@ -45,8 +48,7 @@ export const fetchProjectV2Items = async (): Promise<
};

// TODO: move to .env (it's fine for now because it only has read access)
const TOKEN =
"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";
const TOKEN = process.env.GITHUB_ACCESS_TOKEN ?? "";

const fetchData = async (): Promise<Result<any, Error>> => {
try {
Expand Down
2 changes: 1 addition & 1 deletion src/jobs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { promotionReminder } from "./promotionReminder";
const program = new Command();

program
.argument("<jobname>")
.arguments("<jobname>")
.description("Run the desired job")
.action((jobname) => {
switch (jobname) {
Expand Down
5 changes: 5 additions & 0 deletions src/reminders/dueTodayReminder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
filterForUrgentItems,
filterOutStatus,
} from "../items";
import { fetchFacts } from ".";

export const dueTodayReminder = async () => {
const githubItemsResult = await fetchProjectV2Items();
Expand All @@ -18,6 +19,9 @@ export const dueTodayReminder = async () => {
const unassignedItems = filterForUnassigned(nonBacklogItems);
const urgentItems = filterForUrgentItems(nonBacklogItems);

const factResult = await fetchFacts();
const fact = `${factResult}`;

const message = {
title: "Daily Task Reminder 🎉",
message:
Expand All @@ -44,6 +48,7 @@ export const dueTodayReminder = async () => {
]
: []),
],
fact: fact,
};

const discordMessageResult = await sendDiscordItemMessage(message);
Expand Down
5 changes: 5 additions & 0 deletions src/reminders/fullItemReportReminder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
filterOutStatus,
filterUpcomingItems,
} from "../items";
import { fetchFacts } from ".";

export const fullItemReportReminder = async () => {
const githubItemsResult = await fetchProjectV2Items();
Expand All @@ -20,6 +21,9 @@ export const fullItemReportReminder = async () => {
const upcomingItems = filterUpcomingItems(nonBacklogItems);
const urgentItems = filterForUrgentItems(nonBacklogItems);

const factResult = await fetchFacts();
const fact = `${factResult}`;

const message = {
title: "Biweekly Tasks Reminder ☀️🌱",
message:
Expand Down Expand Up @@ -57,6 +61,7 @@ export const fullItemReportReminder = async () => {
]
: []),
],
fact: fact,
};

const discordMessageResult = await sendDiscordItemMessage(message);
Expand Down
17 changes: 17 additions & 0 deletions src/reminders/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
import axios from "axios";
import { Ok, Err, Result } from "ts-results";

export { dueTodayReminder } from "./dueTodayReminder";
export { fullItemReportReminder } from "./fullItemReportReminder";
export { urgentPromotionReminder } from "./urgentPromotionReminder";

export const fetchFacts = async (): Promise<Result<string, Error>> => {
try {
const response = await axios.get("https://api.api-ninjas.com/v1/facts", {
headers: {
"X-Api-Key": "OW3TaucPc4fGSrBpJe9KuQ==c0GNDiePYzdFuHYm",
},
});
const data = response.data;
return data[0].fact;
} catch (error) {
return Err(new Error("Failed to fetch fact"));
}
};
5 changes: 5 additions & 0 deletions src/reminders/urgentPromotionReminder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
filterForTwentyFourHours,
filterOutStatus,
} from "../items";
import { fetchFacts } from ".";

export const urgentPromotionReminder = async () => {
const githubItemsResult = await fetchProjectV2Items();
Expand All @@ -27,6 +28,9 @@ export const urgentPromotionReminder = async () => {
return null;
}

const factResult = await fetchFacts();
const fact = `${factResult}`;

const message = {
title: "Urgent Promotional Items Reminder 📬‼️",
message:
Expand All @@ -42,6 +46,7 @@ export const urgentPromotionReminder = async () => {
]
: []),
],
fact: fact,
};

console.log("Sending promotion reminder");
Expand Down

0 comments on commit 16893c0

Please sign in to comment.