Using Chatbots with Slack
How to integrate your PySpur chatbots with Slack
Integrating PySpur Chatbots with Slack
This guide explains how to connect your PySpur chatbots to Slack, allowing users to interact with your chatbot directly through Slack channels and threads.
Understanding Slack Integration Options
PySpur offers two distinct ways to work with Slack:
- Workflow Output to Slack - Using the
SlackNotifyNode
to send one-time results from a workflow to a Slack channel - Interactive Chatbot in Slack - Creating a fully interactive chatbot that users can converse with through Slack
It’s important to understand the difference between these approaches:
Feature | SlackNotifyNode | Interactive Chatbot |
---|---|---|
Interaction | One-way (workflow → Slack) | Two-way conversation |
Session Management | None | Full conversation history |
Use Case | Sending alerts, summaries, notifications | Q&A, support, interactive assistance |
Implementation | Simple workflow node | Custom Slack app with API integration |
Setting Up an Interactive Chatbot in Slack
Follow these steps to integrate your PySpur chatbot with Slack for interactive conversations:
1. Create a Slack App
- Go to api.slack.com/apps and click “Create New App”
- Choose “From scratch” and provide a name and workspace
- In the app settings, enable the following:
- Socket Mode (under “Socket Mode”)
- Event Subscriptions (under “Event Subscriptions”)
- Bot Token Scopes (under “OAuth & Permissions”):
app_mentions:read
channels:history
chat:write
groups:history
im:history
mpim:history
2. Collect Required Tokens
You’ll need three tokens from your Slack app:
- Bot Token (
SLACK_BOT_TOKEN
): Found under “OAuth & Permissions” → “Bot User OAuth Token” - Signing Secret (
SLACK_SIGNING_SECRET
): Found under “Basic Information” → “App Credentials” - App Token (
SLACK_APP_TOKEN
): Generate under “Basic Information” → “App-Level Tokens” (withconnections:write
scope)
3. Create Your Integration Script
Create a Python script similar to the example below. This script:
- Initializes a Slack Bolt app
- Listens for mentions and thread replies
- Creates PySpur sessions for users
- Forwards messages to your PySpur chatbot workflow
- Returns responses back to Slack
4. Set Up Environment Variables
Create a .env
file with your tokens:
5. Run Your Integration
-
Install required packages:
-
Make sure your PySpur server is running
-
Start your Slack integration:
How It Works
This integration creates a bidirectional connection between Slack and your PySpur chatbot:
-
User Input: When a user mentions your bot or replies in a thread, the Slack app captures this input.
-
Session Management: The script creates or retrieves a PySpur session for the user, using the thread timestamp as the external ID to track conversations.
-
Message History: For thread replies, the script retrieves the entire conversation history and formats it in the proper structure for your chatbot.
-
PySpur API Call: The user message and conversation history are sent to your PySpur workflow through the API.
-
Response: The assistant’s response from your workflow is posted back to the Slack thread.
Key Differences from SlackNotifyNode
This approach differs significantly from the SlackNotifyNode
:
-
SlackNotifyNode is a one-way communication channel where your workflow sends a message to Slack when it completes. It’s ideal for notifications, alerts, or sharing results of automated processes.
-
Interactive Slack Integration creates a two-way communication channel where users can have ongoing conversations with your chatbot. The integration manages sessions, tracks conversation history, and maintains context across multiple interactions.
Example Use Cases
- Support Bot: Create a support chatbot that answers user questions about your product
- Data Query Assistant: Allow users to query company data through natural language in Slack
- Task Manager: Build a bot that helps users create and track tasks through conversation
- Knowledge Base: Develop a bot that retrieves and explains information from your documentation
Troubleshooting
- Bot not responding: Ensure your bot has been invited to the channel and has the necessary permissions
- Missing messages: Check your log output for any API errors
- Session errors: Verify that your PySpur server is running and accessible
- Token issues: Double-check that all environment variables are set correctly