-
Notifications
You must be signed in to change notification settings - Fork 235
Integrate slackbot with baml client and sage backend #2308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
seawatts
wants to merge
1
commit into
canary
Choose a base branch
from
cursor/integrate-slackbot-with-baml-client-and-sage-backend-4f38
base: canary
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
typescript/apps/sage-backend/app/api/slack/events/route.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
import type { NextRequest } from 'next/server'; | ||
import { NextResponse, after } from 'next/server'; | ||
import crypto from 'crypto'; | ||
import { WebClient } from '@slack/web-api'; | ||
import { submitQuery } from '@/app/actions/query'; | ||
import type { Message, QueryRequest } from '@baml/sage-interface'; | ||
|
||
export const runtime = 'nodejs'; | ||
|
||
function timingSafeEqual(a: string, b: string): boolean { | ||
const aBuf = Buffer.from(a, 'utf8'); | ||
const bBuf = Buffer.from(b, 'utf8'); | ||
if (aBuf.length !== bBuf.length) return false; | ||
return crypto.timingSafeEqual(aBuf, bBuf); | ||
} | ||
|
||
function verifySlackSignature({ | ||
rawBody, | ||
timestamp, | ||
signature, | ||
secret, | ||
}: { | ||
rawBody: string; | ||
timestamp: string | null; | ||
signature: string | null; | ||
secret: string; | ||
}): boolean { | ||
if (!timestamp || !signature) return false; | ||
// Reject old timestamps (> 5 minutes) | ||
const fiveMinutes = 60 * 5; | ||
const tsNum = Number(timestamp); | ||
if (!Number.isFinite(tsNum)) return false; | ||
const now = Math.floor(Date.now() / 1000); | ||
if (Math.abs(now - tsNum) > fiveMinutes) return false; | ||
|
||
const sigBase = `v0:${timestamp}:${rawBody}`; | ||
const hmac = crypto.createHmac('sha256', secret).update(sigBase).digest('hex'); | ||
const expected = `v0=${hmac}`; | ||
return timingSafeEqual(expected, signature); | ||
} | ||
|
||
function stripMentionsAndFormatting(text: string): string { | ||
return text | ||
.replace(/<@[^>]+>/g, '') // remove mentions | ||
.replace(/<([^|>]+)\|[^>]+>/g, '$1') // <url|text> -> url | ||
.replace(/\s+/g, ' ') | ||
.trim(); | ||
} | ||
|
||
export async function POST(req: NextRequest) { | ||
const signingSecret = process.env.SLACK_SIGNING_SECRET; | ||
const botToken = process.env.SLACK_BOUNDARY_BOT_TOKEN; | ||
if (!signingSecret || !botToken) { | ||
return NextResponse.json({ error: 'Slack environment not configured' }, { status: 500 }); | ||
} | ||
|
||
const rawBody = await req.text(); | ||
const timestamp = req.headers.get('x-slack-request-timestamp'); | ||
const signature = req.headers.get('x-slack-signature'); | ||
|
||
if (!verifySlackSignature({ rawBody, timestamp, signature, secret: signingSecret })) { | ||
return NextResponse.json({ error: 'Invalid signature' }, { status: 401 }); | ||
} | ||
|
||
let payload: any; | ||
try { | ||
payload = JSON.parse(rawBody); | ||
} catch (e) { | ||
return NextResponse.json({ error: 'Invalid JSON' }, { status: 400 }); | ||
} | ||
|
||
// URL verification challenge | ||
if (payload.type === 'url_verification' && payload.challenge) { | ||
return new NextResponse(payload.challenge, { | ||
headers: { 'content-type': 'text/plain' }, | ||
}); | ||
} | ||
|
||
// Acknowledge immediately; process asynchronously | ||
after(async () => { | ||
try { | ||
const event = payload.event; | ||
if (!event) return; | ||
|
||
// Only handle app mentions and direct messages for now | ||
if (event.type !== 'app_mention' && !(event.type === 'message' && event.channel_type === 'im')) { | ||
return; | ||
} | ||
|
||
const slack = new WebClient(botToken); | ||
const channel: string = event.channel; | ||
const thread_ts: string = event.thread_ts || event.ts; | ||
const text: string = typeof event.text === 'string' ? event.text : ''; | ||
|
||
const cleaned = stripMentionsAndFormatting(text); | ||
if (!cleaned) return; | ||
|
||
// Build prev_messages from thread history (last 10 messages) | ||
let prev_messages: Message[] = []; | ||
try { | ||
const auth = await slack.auth.test(); | ||
const botUserId = auth.user_id; | ||
const replies = await slack.conversations.replies({ channel, ts: thread_ts, inclusive: true, limit: 10 }); | ||
const msgs = (replies.messages || []) as Array<{ user?: string; bot_id?: string; text?: string; subtype?: string; ts: string }>; | ||
prev_messages = msgs | ||
.filter((m) => !m.subtype || m.subtype === 'bot_message') | ||
.map((m) => { | ||
const mText = stripMentionsAndFormatting(m.text || ''); | ||
const isAssistant = (m.user && m.user === botUserId) || Boolean(m.bot_id); | ||
if (isAssistant) { | ||
return { | ||
role: 'assistant', | ||
message_id: `slack-${m.ts}`, | ||
text: mText, | ||
ranked_docs: [], | ||
} as Message; | ||
} | ||
return { | ||
role: 'user', | ||
text: mText, | ||
} as Message; | ||
}) | ||
.slice(0, -1); // exclude current event message | ||
} catch {} | ||
|
||
const sessionId = `${channel}:${thread_ts}`; | ||
const request: QueryRequest = { | ||
session_id: sessionId, | ||
prev_messages, | ||
message: { | ||
role: 'user', | ||
text: cleaned, | ||
language_preference: 'en', | ||
}, | ||
}; | ||
|
||
// Optional: send typing indicator (ephemeral) | ||
try { | ||
await slack.chat.postEphemeral({ channel, user: event.user, text: 'Thinking…' }); | ||
} catch {} | ||
|
||
const result = await submitQuery(request); | ||
|
||
const answer = result.message.text || 'I could not find an answer.'; | ||
const links = (result.message.ranked_docs || []) | ||
.slice(0, 3) | ||
.map((d) => `<${d.url}|${d.title}>`) | ||
.join(' • '); | ||
|
||
const finalText = links ? `${answer}\n\nSources: ${links}` : answer; | ||
|
||
await slack.chat.postMessage({ | ||
channel, | ||
text: finalText, | ||
thread_ts, | ||
}); | ||
} catch (err) { | ||
console.error('Slack event handling failed:', err); | ||
} | ||
}); | ||
|
||
return NextResponse.json({ ok: true }); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / CodeQL
Polynomial regular expression used on uncontrolled data High