Build a “Serverless” SMS FAQ Bot with Node.js, MessageBird and StdLib in 6 Minutes
Have you ever written an FAQ or document on a website but still get people asking you the same questions? When it comes to users or customers, there’s no such thing as a bad question, but answering the same questions can become tedious. Have you ever wanted to create a bot that answers all these questions so you don’t personally have to?
If you’re not familiar with StdLib yet, you’re in for a treat! We’re a serverless API development and publishing platform that can help you build and ship custom logic in record time.
What You’ll Need Beforehand
- 1x Command Line Terminal With Node.js Installed
- 6x Minutes (or 360x Seconds)
- A US Cell Phone Number
Minute 1: Create a StdLib Account and Initialize MessageBird
After that you will need to install the StdLib CLI by opening up a terminal and running:
$ npm install lib.cli -g
Next, we’re going to create a workspace for your StdLib functions. Create a new folder and initialize it with the following commands:
$ mkdir stdlib-workspace
$ cd stdlib-workspace
$ lib init
Use the credentials you created earlier.
We will use the MessageBird API to send and receive text messages. StdLib makes it really easy for you to initialize a number, send and receive messages.
First, navigate to the messagebird.numbers service page on StdLib and accept the terms of service.
After you’ve done that, return to your terminal and run:
$ lib messagebird.numbers.available --country <CA or US>
You should see a list of available, unclaimed telephone numbers. Choose one from the list, then run:
$ lib messagebird.numbers.initialize --number <chosen phone number>
You can then test your number by sending a text message with the
Run the following command from your terminal:
$ lib messagebird.sms.create \
--recipient <your personal phone number> \
--body Hello from StdLib!
And you should receive a text message from the number you’ve just claimed!
Minute 2: Train Your FAQ Bot Service Using QnA Maker
QnA Maker is a free Microsoft service that allows you to build, train and publish a simple question and answer bot based on FAQ URLs, structured documents or editorial content in minutes.
Once you have logged in to your Azure portal, go to the “Create QnA Maker” page to create your service. If you don’t have a Microsoft Azure account set up, take a minute to set your account-it’s free!
Here you will be asked to fill in the name (e.g GHCBot2018) of your service. Fill this out and hit “Create”.
Next return to Create a QnA Service page and select your service from the drop-downs and name your KB.
After you have added your FAQ page (e.g. https://ghc.anitab.org/2018-faq/) or a product manual page for it to crawl, click “Create your KB.”
You will be given the opportunity to review the knowledge base you have created.
- You must select Save and train on the top right to save any edits you make and to train the QnA Maker model.
Once you are done hit “Publish” to publish your bot.
You will see a sample HTTP request page, keep it open as you will need this information later on.
Minute 3: Fork the SMS Handler Sourcecode
Now that you have trained your QnA service you will need a handler that will respond to incoming questions sent to your MessageBird number. Thankfully we have a template that you can start with.
From your main StdLib workspace in your terminal, fork the starter sourcecode by running:
$ lib create -s @messagebird/messagehub
You’ll be prompted to give your service a name (e.g.
QAService) and then hit enter to create your service directory.
In order to interact with the QnAMaker.ai knowledge base we created, we will need to make a HTTP POST request. To do that we will use a common npm package — axios. We will need to install this package in our sourcecode.
In your service directory where your source code is e.g
$ npm install --save axios
This will modify your
package.json file and install the npm package axios into your service directory.
Minute 4: Modify Your Handler
Assuming you are in the service directory run
$ code . or something similar to open the directory in your favorite text editor.
Your handler service contains three endpoints within the
__main__.js, the main endpoint of your handler. This function will receive events triggered by incoming SMS messages to your MessageBird number and will call other handlers appropriately
messaging/more.js, a handler for incoming messages that contain the word “more” as their sole contents
messaging/__notfound__.js, a “not found” handler. If the incoming message can not be mapped to a named function (like
more), this handler will be triggered
For this application we will be modifying
messaging/__notfound__.js as most if not all questions will trigger this handler.
Copy the code below and replace the code in
In line 16 and 17 you will need to fill in the
key from the Sample HTTP request that was provided to you by the QnAMaker.ai service.
The uri e.g.
/qnamaker/knowledgebases/123456abcd65efgh54 and key is the string that comes after
Alternatively you can also add
key as environment variables in our
Due to anti-spam regulations, MessageBird’s API prevents you from sending messages with URLs in them. Please contact us about having this restriction removed at firstname.lastname@example.org.
Minute 5: Setting Your StdLib Token
The last bit of code we will need to modify is adding our
STDLIB_TOKEN into our
First, fetch 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 and you’ll see this page:
This is your StdLib auth token, click Show token to see its value and copy and paste it to your
env.json file under
Minute 6: Deploying and Set Your Handler
When you’re ready, run the following command from your service directory:
$ lib up dev
The final step is setting the service you just deployed as a handler. Run the following command from your command line:
$ lib messagebird.handlers.sms.set \
--number <your previously claimed phone number> \
You can see a list of your claimed numbers by running
$ lib messagebird.numbers.list if you need to see your claimed number again.
Test your FAQ Message Bot
To test your amazing new FAQ Bot simply text your claimed MessageBird Number.
For example, I created a Concierge Bot for the upcoming 2018 Grace Hopper Celebration.
That’s it! Your FAQ SMS Bot is live! Thanks for reading, and we hope we’ve been helpful showing you how easy it is to create an FAQ SMS Bot using Standard Library, 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 email@example.com.