Creating a slack bot
Use the sample Slack UI that is provided with Watson Assistant Solutions to chat with your assistant.
Setting up your environment
To create an API token for your slack bot and assign a user name, complete these steps:
- Go to https://YOUR_SLACK.slack.com/
- In the Search App Directory field, enter Bots.
- Click Bots.
- Click Add Configuration.
- Assign your bot a unique user name.
- Save the API token and the user name for later use.
You will use the user name and API token when you set up your environment.
To create an IAM key, complete these steps:
- Log in to IBM Cloud.
- In the top navigation bar, click Manage > Security > Platform API keys.
- Click Create +.
- Assign a name and description. For example,
watson-slackbot-token
. - When your IAM key is created, click Show.
- Copy the API key and paste it into your
.env
file.
To set up a python environment, install python version 3.6.2 or later from the python.org website.
To locate your tenant ID, complete these steps:
- Log in to the Watson Assistant Solutions console.
- In the top navigation bar, click Admin > Usage.
- Locate the tenant that you want to connect your slack bot to.
- Copy the tenant ID and paste it into your
.env
file.
To set up a python environment, install python version 3.6.2 or later from the python.org website.
Building a slack bot
Complete these steps to build a slack bot locally:
- Clone the Watson Assistant Solutions slack bot.
- Copy the sample
.env
file. On Linux or Mac OS, enter:cp .env.sample .env
. On Windows systems, enter:xcopy .env.sample .env
. The file must be in the root folder. Tip: Keep any credential configuration files private. -
Edit the
.env
file. For example:# Log Level (DEBUG, INFO, WARNING, ERROR) LOG_LEVEL="WARNING" # Slack bot Credentials SLACK_API_TOKEN="Place_your_slack_bot_API_token_here" BOT_ID=""Place_your_bot_user_name_here"" # Waton Assistant Soltuions Credentials - Use `https://watson-personal-assistant-toolkit.eu-de.mybluemix.net/` for the German data center. WA_URL="https://watson-personal-assistant-toolkit.mybluemix.net" WA_SKILLSET="place_your_assistant_skillset_name_here" WA_LANGUAGE="en-US" WA_DEVICE_TYPE="slackbot" # Authentication type. Either "IAM" or "API_KEY" AUTH_TYPE="IAM" #If using IAM authentication WA_TENANT_ID="place_your_tenant_ID_here" IAM_API_KEY="place_your_IAM_key_here" # If using Watson Assistant Solutions API KEY authentication WA_API_KEY="place_your_Watson_Assistant-Solutions_API_key_here" # Bot Configuration - Number of characters before card data is made into a JSON snippit MAX_CARD_CHARACTERS=1500 # (OPTIONAL) Configurations for Dashbot analytic services ANALYTICS_ENABLED="FALSE" ANALYTICS_API_KEY="" ANALYTICS_INPUT_URL="https://tracker.dashbot.io/track?platform=slack&v=9.8.0-rest&type=incoming&apiKey=" ANALYTICS_RESPONSE_URL="https://tracker.dashbot.io/track?platform=slack&v=9.8.0-rest&type=outgoing&apiKey=" # Max Message Pointer Cache Size MAX_MESSAGE_CACHE=1000
- Install the required python libraries. For example:
pip3 install -r requirements.txt
- Test that your slack bot is working. To run all unit tests, enter:
To run a specific unit test, enter:python3 -m unittest discover
Where values for test name includespython3 -m unittest test.<test name>
test_WA_configuration
,test_slack_configuration
,test_env_file
,test_context_file
, andsettings
. - Start your slack bot. Enter:
python3 bot.py
- Chat with your slack bot. Either:
- Send a direct message to your bot on slack.
- Invite your slack bot to a slack channel and enter
@botname _question_
to chat.
Hosting your slack bot on IBM Cloud
After you test that your slack bot is running locally, push the bot to IBM Cloud for others to use.
- Install the IBM Cloud CLI.
- Log in to IBM Cloud.
- Go to your slack bot directory and push your slack bot application to IBM Cloud. Assign a name to the application. Enter
Or, depending on your CLI tools, enter:cf push $YOUR_APP_NAME_HERE --no-route true --health-check-type process -m 256M
bx cf push $YOUR_APP_NAME_HERE --no-route true --health-check-type process -m 256M
- Verify that you can chat with your bot. Send it a direct message to your bot on slack.
Adding context
You can simulate context variables in your slack bot conversation. Provide the context information in the /context.json
file. The file must be in valid JSON format. The context information is sent in the request to the Watson Assistant
Solutions core routing component.
Use this mechanism if you are testing a skill that requires location information.
Viewing logs
All chat UI logs are in the /slackbot.log
file. The file mostly consists of requests, responses, and slack logging.
Viewing analytics
Your slack bot has built in hooks for enabling analytics. For example, you can enable analytics for dashbot.io by setting ANALYTICS_ENABLED=TRUE
and adding in your dashbot.io API key to the .env
file.