Skip to content

Slack / Teams Integration #246

Description

@Entropy-rgb

Problem Statement

No Slack, Teams, or Discord integration. Users must switch to the web app to ask questions. There is no way to get answers directly in messaging platforms where teams already collaborate.

Proposed Solution

Build Slack and Teams bots that allow users to ask questions inline, with rich message formatting and interactive elements.

Acceptance Criteria

  • Slack bot with /ask slash command
  • Teams bot with message extensions
  • Rich message formatting (tables, charts as images)
  • Thread support for follow-up questions
  • Authentication via OAuth
  • Rate limiting per user/workspace
  • Error handling with helpful messages
  • Installation guide for admins

Technical Approach

Backend Changes

1. Slack bot service (backend/app/integrations/slack.py):

from slack_bolt import App
from slack_bolt.adapter.fastapi import SlackRequestHandler

class SlackBot:
    def __init__(self, app: App, db, llm):
        self.app = app
        self.db = db
        self.llm = llm
        
        @app.command("/ask")
        async def handle_ask(ack, command, say):
            await ack()
            
            question = command["text"]
            user_id = command["user_id"]
            channel_id = command["channel_id"]
            
            # Get user's database connection
            db_conn = await self._get_user_connection(user_id)
            if not db_conn:
                await say("Please connect to a database first in BoloDB.")
                return
            
            # Execute query
            result = await self._execute_query(question, db_conn)
            
            # Format response
            response = self._format_response(result)
            await say(**response)
        
        @app.event("app_mention")
        async def handle_mention(event, say):
            # Handle @mentions
            pass

2. Teams bot service (backend/app/integrations/teams.py):

from botbuilder.core import ActivityHandler, MessageFactory
from botbuilder.schema import Attachment, CardAction, HeroCard

class TeamsBot(ActivityHandler):
    def __init__(self, db, llm):
        self.db = db
        self.llm = llm
    
    async def on_message_activity(self, turn_context):
        question = turn_context.activity.text
        
        # Execute query
        result = await self._execute_query(question, turn_context.activity.from_user.id)
        
        # Create adaptive card
        card = self._create_adaptive_card(result)
        
        reply = MessageFactory.attachment(Attachment(
            content_type="application/vnd.microsoft.card.adaptive",
            content=card
        ))
        await turn_context.send_activity(reply)

3. OAuth flow (backend/app/integrations/oauth.py):

class SlackOAuth:
    def __init__(self, client_id, client_secret):
        self.client_id = client_id
        self.client_secret = client_secret
    
    async def handle_oauth_callback(self, code: str, state: str):
        """Handle OAuth callback from Slack."""
        # Exchange code for token
        token = await self._exchange_code(code)
        
        # Store workspace token
        await self._store_workspace_token(state, token)
        
        return {"success": True}

4. Message formatting (backend/app/integrations/formatter.py):

class MessageFormatter:
    def format_query_result(self, result: dict) -> dict:
        """Format query result for Slack/Teams."""
        if result.get("columns") and result.get("rows"):
            # Create table
            table = self._create_table(result["columns"], result["rows"])
            
            # Create chart if applicable
            chart = None
            if self._is_chartable(result):
                chart = self._generate_chart_image(result)
            
            return {
                "text": result.get("restatement", ""),
                "blocks": [
                    {"type": "section", "text": {"type": "mrkdwn", "text": table}},
                    {"type": "context", "elements": [
                        {"type": "mrkdwn", "text": f"Confidence: {result.get('confidence', 'N/A')}"}
                    ]}
                ]
            }
        
        return {"text": result.get("restatement", "No results found")}

5. API routes (backend/app/routes/integrations.py):

  • GET /api/integrations/slack - Slack installation status
  • POST /api/integrations/slack/install - Install Slack app
  • GET /api/integrations/teams - Teams installation status
  • POST /api/integrations/teams/install - Install Teams bot

Frontend Changes

1. Integration settings (frontend/src/routes/settings/integrations/+page.svelte):

<script>
    let slackInstalled = false;
    let teamsInstalled = false;
    
    async function installSlack() {
        window.location.href = await apiCall('/api/integrations/slack/install');
    }
    
    async function installTeams() {
        window.location.href = await apiCall('/api/integrations/teams/install');
    }
</script>

<div class="integrations">
    <h1>Integrations</h1>
    
    <div class="integration-card">
        <h2>Slack</h2>
        <p>Ask BoloDB questions directly from Slack</p>
        {#if slackInstalled}
            <span class="status installed">Installed</span>
            <button on:click={uninstallSlack}>Uninstall</button>
        {:else}
            <button on:click={installSlack}>Install Slack App</button>
        {/if}
    </div>
    
    <div class="integration-card">
        <h2>Microsoft Teams</h2>
        <p>Ask BoloDB questions directly from Teams</p>
        {#if teamsInstalled}
            <span class="status installed">Installed</span>
            <button on:click={uninstallTeams}>Uninstall</button>
        {:else}
            <button on:click={installTeams}>Install Teams Bot</button>
        {/if}
    </div>
</div>

2. Installation guide - Step-by-step instructions for admins

3. Usage examples - Sample commands and responses

Key Files

  • backend/app/integrations/slack.py - Slack bot (new)
  • backend/app/integrations/teams.py - Teams bot (new)
  • backend/app/integrations/oauth.py - OAuth flow (new)
  • backend/app/integrations/formatter.py - Message formatting (new)
  • backend/app/routes/integrations.py - API routes (new)
  • frontend/src/routes/settings/integrations/+page.svelte - Settings

Related Issues

Metadata

Metadata

Assignees

Labels

P-5Future considerationfeature-requestNew feature request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions