A Microsoft Teams bot that integrates with Databricks Genie to enable natural language querying of data within Teams conversations. Users can ask questions in natural language, and the bot leverages Databricks Genie's AI capabilities to generate SQL queries and return data insights directly in Teams.
This project bridges Microsoft Teams and Databricks Genie, allowing teams to collaborate and analyze data without leaving their Teams environment. The bot handles:
- Natural language question processing
- Databricks Genie API integration for intelligent query generation
- File upload/download capabilities for data sharing
- Conversation history management
- Adaptive Card UI for rich interactions
- Natural Language Processing: Ask questions in plain English and get data insights
- Multi-turn Conversations: Maintain conversation context across multiple exchanges
- Genie Spaces Management: List and select from available Databricks Genie spaces
- Query Result Formatting: Display query results, schema information, and descriptions
- File Handling: Upload and download data files through Teams
- Adaptive Cards: Interactive UI elements for better user engagement
- Conversation Tracking: Persistent conversation state across user sessions
- Error Handling: Graceful error messages and logging
.
├── main.py # Application entry point
├── config.py # Configuration management
├── requirements.txt # Python dependencies
├── example.env # Environment variable template
├── Bots/
│ ├── teams_genie_bot.py # Main Teams activity handler
│ └── Handlers/
│ ├── message_handler.py # Message routing logic
│ ├── card_action_handler.py # Adaptive card interactions
│ ├── file_card_handler.py # File upload/download handling
│ ├── handle_questions.py # Question processing
│ ├── genie_list_handler.py # Space listing
│ └── space_selection_handler.py # Space selection logic
├── Modules/
│ ├── Genie.py # Databricks Genie API wrapper
│ ├── database.py # Database management
│ └── AdaptiveCardTemplate.py # Card template definitions
├── Models/
│ └── user_state.py # User conversation state model
└── Utils/
├── bot_utils.py # Utility functions
├── llm_summarizer.py # LLM-based text summarization
├── response_formatter.py # Response formatting
└── sync_lru_cache_async.py # Async caching utilities
- Python 3.8+
- Microsoft Azure
- Databricks workspace with Genie enabled
- Access to Microsoft Teams
-
Clone the repository
git clone https://github.com/henry-richard7/Databricks-Genie-With-Microsoft-Teams.git cd Databricks-Genie-With-Microsoft-Teams -
Create a virtual environment
python -m venv venv venv\Scripts\activate
-
Install dependencies
pip install -r requirements.txt
-
Configure environment variables
- Copy
example.envto.env - Fill in the required values:
- Copy
Create a .env file with the following variables:
# Microsoft Bot Framework
MicrosoftAppId=<your-app-id>
MicrosoftAppPassword=<your-app-password>
MicrosoftAppTenantId=<your-tenant-id>
MicrosoftAppType=MultiTenant
# Databricks Configuration
DATABRICKS_HOST=<Your-Databricks-Host-URL>
DATABRICKS_TOKEN=dapi<your-personal-access-token>
DATABRICKS_LLM_ENDPOINT=databricks-gpt-oss-20b
# Database (Optional - defaults to SQLite)
database_url=sqlite:///genie_bot.db- Deploy the code in any platform of your choice Azure Web App Services or locally on your machine using ngrok.
- Go to Teams Developer Portal | Bot Framework and create a new bot.
- Configure the messaging endpoint to your bot's URL:
https://your-domain/api/messages - Got to Teams Developer Portal | Apps and create a New App.
- Once created Configure -> App features click on Bot.
- Identify your bot -> Select the bot you have created in step 3. In What can your bot do? tick Upload and download files. In Select the scopes where people can use your bot select all 3 options.
- Create a Databricks workspace and enable Genie
- Generate a Personal Access Token (PAT)
- Create or select a Genie space for queries
- Update the environment variables with your credentials
python main.pyThe bot will start on http://localhost:3978
Using Gunicorn:
gunicorn --worker-class aiohttp.GunicornWebWorker --workers 4 main:APP┌─────────────────────────────────────────────────────────────────┐
│ MICROSOFT TEAMS │
│ User types message in chat │
└──────────────────────────┬──────────────────────────────────────┘
│
↓
┌─────────────────────────────────────────────────────────────────┐
│ MAIN BOT (teams_genie_bot.py) │
│ • Receives message from Teams │
│ • Calls process_message() to handle it │
└──────────────────────────┬──────────────────────────────────────┘
│
↓
┌─────────────────────────────────────────────────────────────────┐
│ MESSAGE HANDLER (message_handler.py) │
│ │
│ Is it "list genie spaces"? ──→ Go to GenieListHandler │
│ ↓ No │
│ Is it a button click? ──→ Go to CardActionHandler │
│ ↓ No │
│ → Must be a question ──→ Go to HandleQuestions │
└──────────────────────────┬──────────────────────────────────────┘
│
┌──────────────────┼──────────────────┐
↓ ↓ ↓
┌────────────┐ ┌────────────┐ ┌──────────────┐
│ LIST SPACES│ │SHOW SPACES │ │ ASK GENIE │
│HANDLER │ │HANDLER │ │(HandleQuestio
└────────────┘ └────────────┘ └──────────────┘
│ │ │
├────────────────┼──────────────────┤
│ │ │
└────────────────┼──────────────────┘
↓
┌────────────────────────────────────┐
│ DATABASE (database.py) │
│ • Save user's selected space │
│ • Save conversation ID │
│ • Remember chat history │
└────────────────────────────────────┘
│
┌────────────────┴──────────────────┐
│ │
↓ ↓
┌──────────────────────┐ ┌──────────────────────┐
│ GENIE API (Genie.py)│ │ ADAPTIVE CARDS │
│ │ │ (response_formatter) │
│ • Ask question to │ │ │
│ Databricks Genie │ │ • Format results │
│ • Get spaces │ │ • Make pretty cards │
│ • Run queries │ │ • Create buttons │
└──────────────────────┘ └──────────────────────┘
↓
┌──────────────────────┐
│ DATABRICKS WORKSPACE │
│ │
│ • Runs the query │
│ • Returns results │
└──────────────────────┘
│
└─────→ [Results go back through handlers] → [Display in Teams]
| Component | File | What It Does |
|---|---|---|
| Main Bot | teams_genie_bot.py |
Catches all Teams messages and passes them to handlers |
| Message Router | message_handler.py |
Decides: Is it a question? Space command? Button click? Routes accordingly |
| Question Handler | handle_questions.py |
Takes your question, gets it answered, formats the response |
| Space Lister | genie_list_handler.py |
Shows available Genie spaces with nice clickable buttons |
| Card Handler | card_action_handler.py |
Handles button clicks and form submissions |
| File Handler | file_card_handler.py |
Manages file uploads and downloads |
| Genie API | Modules/Genie.py |
Talks to Databricks, sends questions, gets answers |
| Database | Modules/database.py |
Remembers who you are, what space you picked, previous conversations |
| Response Formatter | Utils/response_formatter.py |
Makes raw data look pretty in Teams |
You in Teams: "How many users?"
↓
Main Bot catches it
↓
Message Handler says "This is a question"
↓
HandleQuestions does this:
1. Look up your space ID from database
2. Look up your conversation ID (if you're continuing chat)
3. Send question to Genie API: "How many users?"
4. Genie writes SQL and runs it on Databricks
5. Get back: {data: [...], columns: [...]}
↓
ResponseFormatter makes it pretty:
- Turns data into a table
- Adds nice formatting
- Creates an Adaptive Card
↓
Send back to Teams as formatted message with table
↓
You see: "📊 Results: Users: 15,234"
Key Python packages:
botbuilder-core: Microsoft Bot Framework SDKbotbuilder-integration-aiohttp: Async HTTP integrationdatabricks-sdk: Databricks API clientdatabricks-langchain: LangChain integration for LLMssqlalchemy: ORM for database managementpydantic-settings: Configuration managementlangchain: LLM chain orchestrationaiohttp: Async HTTP clientpandas: Data manipulationopenpyxl: Excel file handling
See requirements.txt for the complete list with versions.
By default, the bot uses SQLite (genie_bot.db). To use a different database:
database_url=postgresql://user:password@localhost/genie_botSupported databases:
- SQLite (default):
sqlite:///genie_bot.db - PostgreSQL:
postgresql://user:pass@localhost/dbname - MySQL:
mysql+pymysql://user:pass@localhost/dbname
- Create a Web App Bot in Azure Portal
- Configure deployment settings
- Deploy using Git or ZIP deployment
- Update messaging endpoint in bot settings
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
For issues, questions, or feature requests, please open an issue on the GitHub repository.