Creating a Retail Chatbot using Watson Assistant, Discovery and Database Services
Read this in other languages: 한국어
Watson Conversation is now Watson Assistant. Although some images in this code pattern may show the service as Watson Conversation, the steps and processes will still work.
In this developer Code Pattern we will create a Watson Assistant based chatbot that allows a user to: 1) find items to purchase using Watson Discovery, and 2) add and remove items from their cart by updating a Cloudant NoSQL Database.
When the reader has completed this Code Pattern, they will understand how to:
- Create a chatbot dialog with Watson Assistant
- Dynamically store and update a Cloudant NoSQL database based on chatbot results
- Seed data into Watson Discovery and leverage its natural language capabilities
- Manage and customize a Slack group to add a chatbot
- The user sends a message to the slackbot for online store.
- Slack sends this message to the running application.
- The application orchestrates the interactions between the various Watson services.
- The application queries the Cloudant database for the user's information, including the contents of their shopping cart, and writes the contents back to the database as they change.
- The application interacts with Watson Assistant to determine which response to send to Slack, and information passed back and forth in the conversation context determines actions within the application.
- Watson Discovery is used to get information about the items in the online store.
- Watson Assistant: Create a chatbot with a program that conducts a conversation via auditory or textual methods.
- Watson Discovery: A cognitive search and content analytics engine for applications to identify patterns, trends, and actionable insights.
- Cloudant NoSQL DB: A fully managed data layer designed for modern web and mobile applications that leverages a flexible JSON schema.
- Slack: Slack is a cloud-based set of team collaboration tools and services with chat bot integration.
- Python: Python is a programming language that lets you work more quickly and integrate your systems more effectively.
Watch the Video
NOTE: Perform steps 1-7 OR click the
Deploy to IBM Cloud button and hit
Deploy and then jump to step 6.
Deploy to IBM Cloud
You can use the
View appbutton to use a simple web UI to chat. For the Slack integration, use your Slack UI to chat after completing the additional slack configuration. Use the IBM Cloud dashboard to find and manage the app.
- Clone the repo
- Create IBM Cloud services
- Get IBM Cloud credentials and add to .env
- Configure Watson Assistant
- Configure Watson Discovery
- Configure Slack
- Run the application
1. Clone the repo
watson-online-store locally. In a terminal, run:
$ git clone https://github.com/ibm/watson-online-store
2. Create IBM Cloud services
Create the following services:
3. Get IBM Cloud services Credentials and add to .env file
As you create the IBM Cloud services, you'll need to create service credentials and get the username and password:
watson-online-store/env.sample file to
watson-online-store/.env and populate the service
credentials and URLs as you create the credentials:
# Copy this file to .env and replace the credentials with # your own before running run.py. # Watson Assistant WORKSPACE_ID=<add_assistant_workspace> ASSISTANT_URL=<add_assistant_url> ## Un-comment and use either username+password or IAM apikey. # ASSISTANT_USERNAME=<add_assistant_username> # ASSISTANT_PASSWORD=<add_assistant_password> # ASSISTANT_IAM_APIKEY=<add_assistant_apikey> # Cloudant DB CLOUDANT_USERNAME=<add_cloudant_username> CLOUDANT_PASSWORD=<add_cloudant_password> CLOUDANT_DB_NAME=watson_online_store CLOUDANT_URL=<add_cloudant_url> # Watson Discovery DISCOVERY_URL=<add_discovery_url> DISCOVERY_ENVIRONMENT_ID=<add_discovery_environment> DISCOVERY_COLLECTION_ID=<add_discovery_collection> ## Un-comment and use either username+password or IAM apikey. # DISCOVERY_USERNAME=<add_discovery_username> # DISCOVERY_PASSWORD=<add_discovery_password> # DISCOVERY_IAM_APIKEY=<add_discovery_apikey> # Slack SLACK_BOT_TOKEN=<add_slack_bot_token> SLACK_BOT_USER=wos
4. Configure Watson Assistant
Launch the Watson Assistant tool. Use the import icon button on the right
Find the local version of
data/workspace.json and select
Import. Find the Workspace ID by clicking on the context menu of the new
workspace and select View details.
Put this Workspace ID into the
Optionally, to view the conversation dialog select the workspace and choose the Dialog tab, here's a snippet of the dialog:
5. Configure Watson Discovery
Launch the Watson Discovery tool. The first time you do this, you will see
"Before working with private data, we will need to set up your storage". Click
wait for the storage to be set up.
Create a new data collection and give the data
collection a unique name.
Seed the content by using either
Drag and drop your documents here or
browse from your computer. Choose the JSON files under
Collection Info section, click
Use this collection in API and copy the
Collection ID and the
Environment ID into your
.env file as
6. Configure Slack
Note: This Code Pattern includes Slack integration, but if you are only interested in the web UI, you can skip this step.
Create a slack group or use an existing one if you
have sufficient authorization. (Refer to Slack's how-to
on creating new groups.) To add a new bot, go to the Slack group’s application settings
by navigating to
https://<slack_group>.slack.com/apps/manage and selecting the
Custom Integrations menu on the left.
Bots and then click the green
Add Configuration button.
Give the bot a meaningful name. Note that the
@ symbol is pre-populated by Slack
and you do not include that in your
.env configuration file. Save this in
Once created save the API Token that is generated into the
SLACK_BOT_TOKEN if you are running locally, or save this if you are using
Deploy to IBM Cloud.
/invite <botame> in a channel to invite the bot, or message it directly.
7. Run the application
If you used the Deploy to IBM Cloud button...
If you used
Deploy to IBM Cloud, most of the setup is automatic, but not
the Slack configuration. For that, we have to update a few environment variables.
In the IBM Cloud dashboard find the App that was created. Click on
Runtime on the menu and navigate to the
Environment variables tab.
Update the three environment variables:
SLACK_BOT_TOKENto the token you saved in Step 6
SLACK_BOT_USERto the name of your bot from Step 6
Save the new values and restart the application, watch the logs for errors.
If you decided to run the app locally...
$ pip install -r requirements.txt $ python run.py
Start a conversation with your bot:
Add an item to your cart:
- Help! I'm seeing errors in my log using Deploy to IBM Cloud
This is expected during the first run. The app tries to start before the Discovery service is fully created. Allow a minute or two to pass, the following message should appear:
Watson Online Store bot is connected and running!
- Large amount of Red Logging info appears.
This is expected. The color for logging in IBM Cloud will be red, regardless of the
nature of the message. The log levels are set to
Debug to assist the developer
in seeing how the code is executing. This can be changed to
logging.ERROR in the python code.
- Artificial Intelligence Code Patterns: Enjoyed this Code Pattern? Check out our other AI Code Patterns.
- AI and Data Code Pattern Playlist: Bookmark our playlist with all of our Code Pattern videos
- With Watson: Want to take your Watson app to the next level? Looking to utilize Watson Brand assets? Join the With Watson program to leverage exclusive brand, marketing, and tech resources to amplify and accelerate your Watson embedded commercial solution.