Setting Up Slack App
- Create a Slack account if you don't have one already at the Slack website.
- Navigate to the Slack API page: api.slack.com and click on "Create an App".
- Choose "From scratch" and give your app a name, then select your development Slack workspace.
- Once the app is created, go to "OAuth & Permissions" in the sidebar.
- Under "Scopes", add the required permissions for your bot. Typically, you'll need `channels:join`, `chat:write`, `im:history`, and `im:read`.
- Click "Install App to Workspace" and authorize the permissions. Copy the Bot User OAuth Token, which starts with `xoxb-`.
Configuring Rasa
- Ensure you have a Rasa project set up. If not, create one using:
rasa init --no-prompt
- Install the Slack connector for Rasa by adding or ensuring the package in your `requirements.txt`:
rasa[slack]
- Alternatively, if managing manually, install via pip:
pip install rasa[slack]
- In your Rasa project, open the `credentials.yml` file. Add the Slack configuration:
slack:
slack_token: "your_bot_user_oauth_token_here"
slack_channel: "your_slack_channel"
- Replace `"your_bot_user_oauth_token_here"` with your actual Bot User OAuth Token from Slack.
- Set `"your_slack_channel"` to the channel name or ID where your bot will operate.
Routing Messages to Slack
- Use the Rasa's `ngrok` to expose your local server. First, download and install ngrok from ngrok.com.
- Run ngrok to forward requests from the internet to your local port, typically port 5005 for Rasa:
ngrok http 5005
- Copy the HTTPS URL provided by ngrok. This URL forwards requests to your local Rasa server.
Connecting Slack Events
- Back in the Slack API page for your app, navigate to "Event Subscriptions". Enable events by toggling the switch to "On".
- Enter your ngrok URL with `/webhooks/slack/webhook` at the end in the "Request URL" field, e.g., `https://your-ngrok-url.ngrok.io/webhooks/slack/webhook`.
- Subscribe to Bot Events. Add events like `message.channels`, `message.im`, and any others you might need for interactivity.
- Save changes to ensure Slack will route messages to your Rasa bot.
Testing Your Integration
- Restart your Rasa server with:
rasa run
- Test sending messages in your Slack channel where the bot has been invited.
- Ensure that messages are correctly processed and responded to as per your Rasa setup.
Troubleshooting and Optimization
- Make sure the ngrok URL is active and correct each time you start ngrok, as it changes every session unless you have a paid version with a fixed URL.
- Check Rasa logs for any error messages or exceptions that could offer clues on misconfigurations or Slack errors.
- Optimize your bot's interaction strategy by adjusting intents, entities, and conversation flows based on user feedback and test interactions.