Skip to content

Commit 5046011

Browse files
committed
slack component
1 parent 8bcb299 commit 5046011

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

app/page.tsx

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,8 @@ import { postsPerPage } from "@/site";
44
import Feed from "@/components/Feed";
55
import HeroSection from "../components/HeroSection";
66
import axios, { AxiosResponse } from 'axios';
7+
import { sendSlackMessage } from "@/functions/sendSlackMessage";
78

8-
interface SlackMessage {
9-
text: string;
10-
}
11-
12-
const sendSlackMessage = async (message: SlackMessage): Promise<AxiosResponse> => {
13-
const slackWebhookUrl = process.env.SLACK_WEBHOOK;
14-
15-
if (!slackWebhookUrl) {
16-
throw new Error('Slack webhook URL is not defined.');
17-
}
18-
19-
return await axios.post(slackWebhookUrl, message);
20-
};
219

2210
const HomePage = async ({
2311
searchParams,
@@ -28,7 +16,7 @@ const HomePage = async ({
2816
const publishedPosts: Article[] = await getAllPosts();
2917

3018
// Send a message to Slack when the page is accessed
31-
const message: SlackMessage = {
19+
const message = {
3220
text: `Someone visited the homepage! Page: ${page}`,
3321
};
3422

functions/sendSlackMessage.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// slackUtils.ts
2+
3+
import axios, { AxiosResponse } from 'axios';
4+
5+
interface SlackMessage {
6+
text: string;
7+
}
8+
9+
export const sendSlackMessage = async (message: SlackMessage): Promise<AxiosResponse | void> => {
10+
const slackWebhookUrl = process.env.SLACK_WEBHOOK;
11+
12+
if (!slackWebhookUrl) {
13+
console.error('Slack webhook URL is not defined.');
14+
return;
15+
}
16+
17+
try {
18+
const response = await axios.post(slackWebhookUrl, message);
19+
return response;
20+
} catch (error: unknown) {
21+
if (error instanceof Error) {
22+
console.error('Error sending Slack message:', error.message);
23+
// Handle the error as needed
24+
} else {
25+
console.error('An unexpected error occurred:', error);
26+
// Handle other types of errors or log them
27+
}
28+
}
29+
};

0 commit comments

Comments
 (0)