Skip to content

Commit

Permalink
slack component
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanPreusse committed Jan 12, 2024
1 parent 8bcb299 commit 5046011
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
16 changes: 2 additions & 14 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,8 @@ import { postsPerPage } from "@/site";
import Feed from "@/components/Feed";
import HeroSection from "../components/HeroSection";
import axios, { AxiosResponse } from 'axios';
import { sendSlackMessage } from "@/functions/sendSlackMessage";

interface SlackMessage {
text: string;
}

const sendSlackMessage = async (message: SlackMessage): Promise<AxiosResponse> => {
const slackWebhookUrl = process.env.SLACK_WEBHOOK;

if (!slackWebhookUrl) {
throw new Error('Slack webhook URL is not defined.');
}

return await axios.post(slackWebhookUrl, message);
};

const HomePage = async ({
searchParams,
Expand All @@ -28,7 +16,7 @@ const HomePage = async ({
const publishedPosts: Article[] = await getAllPosts();

// Send a message to Slack when the page is accessed
const message: SlackMessage = {
const message = {
text: `Someone visited the homepage! Page: ${page}`,
};

Expand Down
29 changes: 29 additions & 0 deletions functions/sendSlackMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// slackUtils.ts

import axios, { AxiosResponse } from 'axios';

interface SlackMessage {
text: string;
}

export const sendSlackMessage = async (message: SlackMessage): Promise<AxiosResponse | void> => {
const slackWebhookUrl = process.env.SLACK_WEBHOOK;

if (!slackWebhookUrl) {
console.error('Slack webhook URL is not defined.');
return;
}

try {
const response = await axios.post(slackWebhookUrl, message);
return response;
} catch (error: unknown) {
if (error instanceof Error) {
console.error('Error sending Slack message:', error.message);
// Handle the error as needed
} else {
console.error('An unexpected error occurred:', error);
// Handle other types of errors or log them
}
}
};

0 comments on commit 5046011

Please sign in to comment.