Two weeks ago we built a “Serverless” SMS FAQ Bot”, which answers questions from an FAQ knowledge base via SMS using StdLib, MessageBird, and QnAMaker.ai in 6 minutes! We turned a boring FAQ laundry list into a fun and interactive way for your users, customers, or website’s visitors to learn more about your product/service via simple SMS text to your Bot.
But what if a user asks a good question that isn’t in the FAQ? This week, we will set up an easy way to add new content to your FAQ service via Slack slash commands!
If you don’t have your FAQ bot up and running yet, take a look at our previous tutorial — this article is a meant as a follow-up.
- 1x Slack Workspace
- 1x Command Line Terminal
- 8x Minutes
- 1 Previously Claimed MessageBird Number
In Slack, you can get a task done by simply typing a slash and a command in the message field.
For example, when typing /msg (or /dm) @someone [your message] Slack will send a direct message to another member. This is one of Slack’s many built-in commands. Check the rest out here.
But what I find to be the most empowering, is that Slack allows its users to create their own commands!
In just a few minutes you will have a Slack workspace set up with a new slash command to train your SMS FAQ Bot’s Knowledge Base!
Click Create New App to create your app. You’ll be presented with a modal to enter in your App Name. I suggest KBTrainer since this application will be used to “Train” your bot’s knowledge base.
And now, click Create App.
You’ll be sent to your Slack app dashboard. Click on the ‘Bot Users’ tab on the side, and click Add a Bot User to create your bot. You’ll be given an option to enter in the bot username. Click Add Bot User to complete the process, and that’s it! Your bot user is now added and ready to be used.
Open up your Terminal or Command Line and navigate into the
stdlib directory that you created with the previous tutorial. Create a new StdLib service to configure your slack app. You can use the Slack source code to get a slack bot up and running with very minimal effort.
$ cd stdlib
$ lib create -s @slack/app
You’ll be asked to enter a
Service Name, I recommend
KBTrainer here as well.
To have your slack bot interact with the QnAMaker.ai knowledge base that you have created, you will need to make an HTTP POST request. To do this, we will use the npm package — axios.
Make sure you are inside your service directory where your source code is e.g
cd ./<username>/KBTrainer and then run:
$ npm install --save axios
*This will modify your
package.json file and install the npm package axios into your service directory.
env.json (environment variables) under
<username>/KBTrainer in a text editor of your choice.
We’ll be making modifications to the “local” and “dev” environment variables — make sure you’re modifying the right set!
First, grab your StdLib Token from the StdLib Dashboard. You’ll be asked to log in again — once you’ve done so, click on Library Tokens on the left sidebar. Click Show token to see its value and copy and paste it to your
env.json file under
Next, fill out
SLACKAPPNAME. You should differentiate between your dev / release apps — make sure this is unique for every app you build, as it sets a unique identifier for data storage / retrieval. For the
dev environment you can set this to
Finally, return to your Slack App, and scroll down on the Basic Information panel:
Copy each of these values to
dev section of
As a last step, modify
<username> is your StdLib username.
Now that we’ve set added our commands and set the appropriate environment variables in our StdLib Slack service, we’re ready to push our app live! Simply deploy with:
$ lib up dev
To add or modify Slash commands, you’ll want to look in your StdLib Slack Source Code directory under
<username>/KBTrainer/functions/commands and create files with the name
NAME is your intended command. I suggest you name your file
You’ll then need to copy and paste the code below into your StdLib Slack Source Code directory inside the qna.js file:
In line 18 and 20 you will need to fill in the
key from the Sample HTTP request that was provided to you by the QnAMaker.ai service you built last week:
/qnamaker/knowledgebases/123456abcd65efgh54and key is the string that comes after
When you’re ready, run the following command from your service directory:
$ lib up dev
You have completed the set up of your slack-app service (KBTrainer) which will allow you to add information to your Bot’s knowledge base from Slack. Next, you will need to return to your SMS QnA service from the last tutorial to configure it so that it can interact with your slack workspace. To do this, we will use the npm package slack js.
From your terminal, change directory into the SMS QA service that you created when setting up your SMS FAQ Bot (minute 3).
In your service directory (where your QA service source code is) e.g
$ npm i slack --save
Now open the directory by running
$ code . or whatever command you use to open the directory in your favorite text editor.
Navigate to the
env.json file; here you will need to add your “SLACKBOTTOKEN” under
Next, we will be modifying the source code under the handler:
Make sure you define Slack on line 4.
Delete lines 29–36 :
And then copy and paste the following code into line 29–36:
Save your changes and run:
$ lib up dev
Next, we will need to create our new
Find and Click Slash Commands on the side bar menu.
After clicking Create New Command, you’ll be asked to enter some command details, for example:
- Short Description:
Sends modified Question and Answer to Microsoft's QnA maker's DB
- Usage Hint:
- Hit “Save” once complete
Return to your Slack App, we’ll need to enable OAuth. on the sidebar menu, click OAuth & Permissions. Once there, you’ll want to enter in a Redirect URL as follows:
https://<username>.lib.id/KBTrainer@dev/auth/ where <username> is your StdLib username and click “Add” and “Save URLS.”
The final step is to authorize the app by going to:
https://<username>.lib.id/KBTrainer@dev/ in your web browser — it should be available to be copy-and-pasted from your command line output. You’ll see a very simple “Add to Slack” button.
You’ll be returned to your specified auth callback, which should give a success message. Your bot is now “production-ready!”
That’s it! Your FAQ SMS Bot is ready to be trained from Slack!
You’re all done! Test it out!
You should be able to send commands in the following format:
/qna [question] > [answer] This should send new content to your QnA service!
Thanks for reading! We hope we’ve been helpful showing you how easy it is to create and train an FAQ SMS Bot using Standard Library, Slack, MessageBird, and QnA Maker.
StdLib is your new API development solution, built atop serverless architecture for fast deployments and infinite scalability. To stay informed of the latest platform updates, please follow us on Twitter, @StdLibHQ, or if you’re interested in joining the team — we’re hiring! Shoot us an e-mail or resume to firstname.lastname@example.org.