Skip to content

Commit

Permalink
feat: notify slack channel on post
Browse files Browse the repository at this point in the history
  • Loading branch information
hyochan committed Dec 27, 2024
1 parent 9666cfd commit 142f06f
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
DATABASE_URL=
expoProjectId=
SLACK_WEBHOOK_URL=
EXPO_PUBLIC_SUPABASE_URL=
EXPO_PUBLIC_SUPABASE_ANON_KEY=
EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY=
GOOGLE_SERVICES_IOS=./GoogleService-Info.plist
GOOGLE_SERVICES_ANDROID=./google-services.json
1 change: 1 addition & 0 deletions .github/workflows/deploy-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jobs:
run: bun build:web
env:
EXPO_PUBLIC_SUPABASE_URL: ${{ secrets.EXPO_PUBLIC_SUPABASE_URL }}
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
EXPO_PUBLIC_SUPABASE_ANON_KEY: ${{ secrets.EXPO_PUBLIC_SUPABASE_ANON_KEY }}
EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY: ${{ secrets.EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY }}
expoProjectId: ${{ secrets.expoProjectId }}
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
run: bun build:web
env:
EXPO_PUBLIC_SUPABASE_URL: ${{ secrets.EXPO_PUBLIC_SUPABASE_URL }}
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
EXPO_PUBLIC_SUPABASE_ANON_KEY: ${{ secrets.EXPO_PUBLIC_SUPABASE_ANON_KEY }}
EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY: ${{ secrets.EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY }}
expoProjectId: ${{ secrets.expoProjectId }}
Expand Down
2 changes: 1 addition & 1 deletion app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export default ({config}: ConfigContext): ExpoConfig => ({
'expo-splash-screen',
{
image: './assets/icon.png',
backgroundColor: '#343434',
backgroundColor: '#333333',
imageWidth: 200,
},
],
Expand Down
6 changes: 6 additions & 0 deletions src/apis/postQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {SupabaseClient} from '../hooks/useSupabase';
import {ImageInsertArgs, PostInsertArgs, PostWithJoins} from '../types';
import {PAGE_SIZE} from '../utils/constants';
import {sendNotificationsToAllUsers} from './notifications';
import {sendSlackNotification} from './slackQueries';

const filterDeletedImageInPost = (post: PostWithJoins): PostWithJoins => {
return {
Expand Down Expand Up @@ -288,6 +289,11 @@ export const fetchCreatePost = async ({
images,
};

sendSlackNotification({
post: post as unknown as PostWithJoins,
url: `https://forums.crossplatformkorea.com/post/${data.id}`,
});

sendNotificationsToAllUsers({
title: post.title,
body: post.content,
Expand Down
40 changes: 40 additions & 0 deletions src/apis/slackQueries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {Post} from '../types';

const SLACK_WEBHOOK_URL = process.env.SLACK_WEBHOOK_URL;

console.log('SLACK_WEBHOOK_URL:', SLACK_WEBHOOK_URL);

const truncateText = (text: string, maxLength: number) => {
if (text.length > maxLength) {
return text.slice(0, maxLength) + '...';
}
return text;
};

export const sendSlackNotification = async ({
post,
url,
}: {
post: Post;
url: string;
}) => {
const payload = {
text: `*${truncateText(post.title, 50)}*\n\n${truncateText(post.content, 200)}\n\n<${url}|자세히 보기>`,
};

try {
const response = await fetch(SLACK_WEBHOOK_URL, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(payload),
});

if (!response.ok) {
console.error('Slack notification failed:', response.statusText);
} else {
console.log('Slack notification sent successfully!');
}
} catch (error) {
console.error('Error sending Slack notification:', error);
}
};

0 comments on commit 142f06f

Please sign in to comment.