MCPcopy Create free account
hub / github.com/Openpanel-dev/openpanel / slackWebhook

Function slackWebhook

apps/api/src/controllers/webhook.controller.ts:30–111  ·  view source on GitHub ↗
(
  request: FastifyRequest<{
    Querystring: unknown;
  }>,
  reply: FastifyReply
)

Source from the content-addressed store, hash-verified

28});
29
30export async function slackWebhook(
31 request: FastifyRequest<{
32 Querystring: unknown;
33 }>,
34 reply: FastifyReply
35) {
36 const parsedParams = paramsSchema.safeParse(request.query);
37
38 if (!parsedParams.success) {
39 request.log.error(parsedParams.error, 'Invalid params');
40 return reply.status(400).send({ error: 'Invalid params' });
41 }
42
43 const veryfiedState = await slackInstaller.stateStore?.verifyStateParam(
44 new Date(),
45 parsedParams.data.state
46 );
47 const parsedMetadata = metadataSchema.safeParse(
48 JSON.parse(veryfiedState?.metadata ?? '{}')
49 );
50
51 if (!parsedMetadata.success) {
52 request.log.error(parsedMetadata.error, 'Invalid metadata');
53 return reply.status(400).send({ error: 'Invalid metadata' });
54 }
55
56 const slackOauthAccessUrl = [
57 'https://slack.com/api/oauth.v2.access',
58 `?client_id=${process.env.SLACK_CLIENT_ID}`,
59 `&client_secret=${process.env.SLACK_CLIENT_SECRET}`,
60 `&code=${parsedParams.data.code}`,
61 `&redirect_uri=${process.env.SLACK_OAUTH_REDIRECT_URL}`,
62 ].join('');
63
64 try {
65 const response = await fetch(slackOauthAccessUrl);
66 const json = await response.json();
67 const parsedJson = zSlackAuthResponse.safeParse(json);
68
69 if (!parsedJson.success) {
70 request.log.error(
71 {
72 zod: parsedJson,
73 json,
74 },
75 'Failed to parse slack auth response'
76 );
77 const html = fs.readFileSync(path.join(__dirname, 'error.html'), 'utf8');
78 return reply.status(500).header('Content-Type', 'text/html').send(html);
79 }
80
81 // Send a notification first to confirm the connection
82 await sendSlackNotification({
83 webhookUrl: parsedJson.data.incoming_webhook.url,
84 message:
85 '👋 Hello. You have successfully connected OpenPanel.dev to your Slack workspace.',
86 });
87

Callers

nothing calls this directly

Calls 6

sendSlackNotificationFunction · 0.90
parseMethod · 0.80
joinMethod · 0.80
fetchFunction · 0.50
errorMethod · 0.45
sendMethod · 0.45

Tested by

no test coverage detected