Chat with your data using natural language. Powered by Snowflake Cortex AI Agents.
Transform how your team interacts with data. Ask questions in plain English, get SQL-powered answers in seconds — with the generated SQL and full results attached as files.
- Users mention
@CortexBotin a channel or DM the bot directly - Slack delivers the message over WebSocket (Socket Mode — no public URL needed)
- The bot extracts the question, retrieves thread history for context, and sends a POST request to the Cortex Agent REST API at
/api/v2/databases/{db}/schemas/{schema}/agents/{agent}:run - The Cortex Agent uses Cortex Analyst (and optionally Cortex Search or custom tools) to generate SQL, execute it against your Snowflake warehouse, and return the results
- The bot parses the response and sends back: a text summary in the thread, a
.sqlfile with the generated query, and a.csvfile with the full results
- Natural Language Queries — Ask questions like "What were our top products last month?"
- Automatic SQL Generation — Cortex AI generates optimized SQL for complex joins, CTEs, and window functions
- File Attachments — Generated SQL attached as
.sql, query results as.csv— no more truncated tables - Conversation Memory — Follow-up questions within a Slack thread maintain full context (up to 20 messages)
- Dual Interface — Works via @mentions in channels and direct messages
- App Home — Welcome tab with usage instructions and example queries
- Python 3.10+
- Poetry for dependency management
- Snowflake account with Cortex AI Agents enabled
- Snowflake Programmatic Access Token (PAT)
- Slack workspace with admin access
git clone https://github.com/yourusername/cortex-slack-bot.git
cd cortex-slack-bot
python3 -m venv .venv
source .venv/bin/activate
poetry install- Go to Slack API Apps
- Click Create New App → From scratch
- Configure the following:
OAuth & Permissions — Add these Bot Token Scopes:
app_mentions:readchat:writefiles:writeim:historyim:readim:write
Socket Mode:
- Enable Socket Mode
- Generate an App-Level Token with
connections:writescope
Event Subscriptions — Subscribe to:
app_mentionmessage.imapp_home_opened
App Home:
- Enable the Messages Tab (so users can DM the bot)
- Install the app to your workspace
- In Snowsight, navigate to AI & ML → Agents
- Click Create Agent
- Configure your semantic model and data sources
- Note the agent name, database, and schema
See Snowflake Cortex Agents Documentation for detailed setup.
- In Snowsight, click your user menu (bottom-left)
- Go to Programmatic Access Tokens
- Generate a new token and copy it
Note: Snowflake REST APIs do not support username/password auth directly. PAT is the simplest auth method — no key pairs or OAuth setup required.
cp .env.example .envEdit .env with your credentials:
# Slack
SLACK_BOT_TOKEN=xoxb-your-bot-token
SLACK_APP_TOKEN=xapp-your-app-token
SLACK_SIGNING_SECRET=your-signing-secret
# Snowflake
SNOWFLAKE_ACCOUNT=ORG-ACCOUNT
SNOWFLAKE_USER=your-username
SNOWFLAKE_PASSWORD=your-password
SNOWFLAKE_PAT=your-programmatic-access-token
SNOWFLAKE_WAREHOUSE=COMPUTE_WH
SNOWFLAKE_DATABASE=your-database
SNOWFLAKE_SCHEMA=your-schema
# Cortex Agent
CORTEX_AGENT_NAME=your-agent-nameIf your Snowflake account has network policies enabled, allowlist your IP:
CREATE OR REPLACE NETWORK RULE allow_my_ip
MODE = INGRESS TYPE = IPV4 VALUE_LIST = ('YOUR_IP/32');
CREATE OR REPLACE NETWORK POLICY my_policy
ALLOWED_NETWORK_RULE_LIST = ('allow_my_ip');
ALTER USER your_user SET NETWORK_POLICY = 'my_policy';poetry run cortex-botOr with Python directly:
poetry run python -m cortex_slack_bot.appIn any channel where the bot is invited:
@CortexBot What were our total sales last quarter?
Send a DM to the bot:
Show me the top 10 customers by revenue
Ask follow-ups in the same thread and the bot keeps context:
@CortexBot Show me total sales by category
→ (bot responds with breakdown)
@CortexBot Break that down by month
→ (bot understands "that" = sales by category)
| Natural Language | What It Does |
|---|---|
| "What were our sales last month?" | Time-series aggregation |
| "Top 10 products by revenue" | Ranking with aggregation |
| "Compare Q1 vs Q2 performance" | Period comparison |
| "Show me users who signed up this week" | Filtered date queries |
| "Average order value by region" | Grouped aggregations |
- User sends a question in Slack (mention or DM)
- Bot receives the message via WebSocket (Socket Mode)
- Thread history is retrieved from in-memory store for conversation context
- Question + history sent to Cortex Agent via REST API with PAT auth
- Cortex AI generates and executes SQL against your Snowflake warehouse
- Bot parses the response — extracts the text answer, SQL query, and result data
- Text answer posted to Slack thread
- SQL and results attached as
.sqland.csvfiles in the thread - Exchange stored in thread history for future follow-ups
src/cortex_slack_bot/
├── app.py # Entry point — creates app, starts Socket Mode
├── config.py # Pydantic settings — loads and validates .env
├── snowflake_client.py # Cortex Agent HTTP client and response parser
└── handlers.py # Slack event handlers, formatting, file uploads, thread memory
| Component | Technology |
|---|---|
| Slack SDK | slack-bolt with Socket Mode |
| HTTP Client | httpx (async) |
| Config | pydantic-settings with .env |
| Auth | Snowflake PAT (Bearer token) |
| Language | Python 3.10+ |
| Build | Poetry |
