feature(dashboard): add integrations and notifications

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-10-02 22:12:05 +02:00
parent d920f6951c
commit f65a633403
94 changed files with 3692 additions and 127 deletions

View File

@@ -0,0 +1,2 @@
// Empty, import directly from src/
export {};

View File

@@ -0,0 +1,18 @@
{
"name": "@openpanel/integrations",
"version": "0.0.1",
"main": "index.ts",
"scripts": {
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@slack/bolt": "^3.18.0",
"@slack/oauth": "^3.0.0"
},
"devDependencies": {
"@openpanel/tsconfig": "workspace:*",
"@openpanel/validation": "workspace:*",
"@types/node": "^18.16.0",
"typescript": "^5.2.2"
}
}

View File

@@ -0,0 +1,34 @@
// Cred to (@OpenStatusHQ) https://github.com/openstatusHQ/openstatus/blob/main/packages/notifications/discord/src/index.ts
export function sendDiscordNotification({
webhookUrl,
message,
}: {
webhookUrl: string;
message: string;
}) {
return fetch(webhookUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
content: message,
avatar_url: 'https://openpanel.dev/logo.jpg',
username: 'OpenPanel Notifications',
}),
}).catch((err) => {
return {
ok: false,
json: () => Promise.resolve({}),
};
});
}
export function sendTestDiscordNotification(webhookUrl: string) {
return sendDiscordNotification({
webhookUrl,
message:
'**🧪 Test [OpenPanel.dev](<https://openpanel.dev/>)**\nIf you can read this, your Slack webhook is functioning correctly!\n',
});
}

View File

@@ -0,0 +1,50 @@
// Cred to (@c_alares) https://github.com/christianalares/seventy-seven/blob/main/packages/integrations/src/slack/index.ts
import { LogLevel, App as SlackApp } from '@slack/bolt';
import { InstallProvider } from '@slack/oauth';
const SLACK_CLIENT_ID = process.env.SLACK_CLIENT_ID;
const SLACK_CLIENT_SECRET = process.env.SLACK_CLIENT_SECRET;
const SLACK_OAUTH_REDIRECT_URL = process.env.SLACK_OAUTH_REDIRECT_URL;
const SLACK_STATE_SECRET = process.env.SLACK_STATE_SECRET;
export const slackInstaller = new InstallProvider({
clientId: SLACK_CLIENT_ID!,
clientSecret: SLACK_CLIENT_SECRET!,
stateSecret: SLACK_STATE_SECRET,
logLevel: process.env.NODE_ENV === 'development' ? LogLevel.DEBUG : undefined,
});
export const getSlackInstallUrl = ({
integrationId,
organizationId,
}: { integrationId: string; organizationId: string }) => {
return slackInstaller.generateInstallUrl({
scopes: [
'incoming-webhook',
'chat:write',
'chat:write.public',
'team:read',
],
redirectUri: SLACK_OAUTH_REDIRECT_URL,
metadata: JSON.stringify({ integrationId, organizationId }),
});
};
export function sendSlackNotification({
webhookUrl,
message,
}: {
webhookUrl: string;
message: string;
}) {
return fetch(webhookUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
text: message,
}),
});
}

View File

@@ -0,0 +1,12 @@
{
"extends": "@openpanel/tsconfig/base.json",
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"],
},
"tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json"
},
"include": ["."],
"exclude": ["node_modules"]
}