How to Integrate Rasa with Discord
In this article
Yes, you can connect your Rasa chatbot to Discord using the official Rasa Discord connector. This lets users chat with your bot directly in a Discord server. You'll need a Rasa server running (either locally or on a cloud platform), a Discord bot token, and basic knowledge of the command line.
What You'll Need
- A Rasa project (with a trained model) that you can run with
rasa run. - A Discord application and bot token (create one at the Discord Developer Portal).
- Python 3.7 or later installed on your machine.
- Rasa installed (via
pip install rasa). - The
discord.pylibrary installed (Rasa's Discord connector uses it).
Step-by-Step
- Create a Discord bot. Go to the Discord Developer Portal, click "New Application", give it a name, then go to the "Bot" tab and click "Add Bot". Copy the bot token (you'll need it later).
- Invite the bot to your server. In the Developer Portal, go to "OAuth2" > "URL Generator". Select the "bot" scope and the "Send Messages" and "Read Message History" permissions. Use the generated URL to invite the bot to your Discord server.
- Install dependencies. In your terminal, run
pip install rasa[discord]orpip install discord.pyto ensure the Discord library is available. - Configure your Rasa credentials. Open your
credentials.ymlfile (in your Rasa project root) and add the Discord channel configuration. It looks like this:
discord:
token: "YOUR_BOT_TOKEN"
Replace YOUR_BOT_TOKEN with the token you copied earlier. Save the file.
- Run your Rasa server. In your terminal, navigate to your Rasa project folder and run
rasa run. This starts the Rasa server with the Discord connector enabled. - Test the bot. Go to your Discord server and send a message to the bot (or mention it). If everything is set up correctly, the bot should respond with your Rasa model's replies.
Common Mistakes
- Forgetting to enable the bot's message intents. In the Discord Developer Portal, under the "Bot" tab, make sure "MESSAGE CONTENT INTENT" is turned on. Without this, the bot cannot read message content.
- Using the wrong token. Make sure you copy the bot token (not the client secret). The token looks like a long string with dots and underscores.
- Not restarting Rasa after editing credentials. If you change
credentials.yml, you must stop and restartrasa runfor the changes to take effect. - Running Rasa on a different port. The Discord connector uses the default Rasa port (5005). If you changed the port, the connector won't work unless you also set the
webhook_urlin the credentials.
Tips
- Use environment variables for the token instead of hardcoding it in
credentials.yml. For example, settoken: "${DISCORD_TOKEN}"and export the variable before running. - If you want the bot to respond to every message (not just mentions), you can set
bot_channelin the credentials to a specific channel ID, or leave it empty to listen in all channels. - Test your Rasa model locally with
rasa shellfirst to make sure the bot's responses are working before connecting to Discord. - Keep your Rasa server running in a separate terminal or use a process manager like
systemdorpm2for production.
Troubleshooting
If the bot doesn't respond, try these in order:
- Check the Rasa logs. Look at the terminal where
rasa runis running. You should see a message like "Discord bot started". If not, check for errors. - Verify the bot token. Make sure the token is correct and the bot is online (you can see it in the Discord server's member list).
- Check intents. Go back to the Developer Portal and confirm that "MESSAGE CONTENT INTENT" is enabled. Save changes and restart your bot.
- Test with a direct message. Sometimes bots don't respond in channels if they lack permissions. Try sending a direct message to the bot.
- Restart everything. Stop the Rasa server, restart it, and try again. Also, if you changed the bot's permissions in Discord, you may need to re-invite the bot.
If the bot still doesn't respond, check if your Rasa model is trained (run rasa train if needed) and that the server is running on the default port (5005).
Frequently Asked Questions
Can I use this with a Rasa server hosted on a cloud platform?
Yes, but you'll need to set the webhook_url in the credentials to the public URL of your Rasa server. Discord will send events to that URL.
Does the bot need to be in a specific channel?
By default, the bot responds in any channel it has access to. You can restrict it by setting bot_channel in the credentials to a specific channel ID.
Why does the bot only respond when mentioned?
This happens if the bot's permissions are limited or if you've configured it that way. To respond to all messages, leave bot_channel empty and ensure the bot has "Send Messages" permission.
Can I use the same bot token for multiple Rasa instances?
No, each Discord bot token can only be used by one process at a time. If you run multiple Rasa instances, create a separate Discord bot for each.
How do I update the bot's responses?
You need to retrain your Rasa model with new training data (rasa train) and restart the Rasa server.
Is there a way to handle attachments or images?
Rasa's Discord connector only handles text messages. Attachments are ignored. You'd need to build a custom action to process them.