Build a “Serverless” SMS FAQ Bot with Node.js, MessageBird and StdLib in 6 Minutes

April 24, 2018 0 Comments

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?

In just 6 minutes I will show you how you can build a bot that will automatically answer questions from an FAQ via SMS using StdLib, MessageBird, and QnAMaker.ai!

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.

  • 1x Command Line Terminal With Node.js Installed
  • 6x Minutes (or 360x Seconds)
  • A US Cell Phone Number

Getting started with StdLib is easy — head over to our website, click “Sign Up” and choose a namespace, email, and password.

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.

Click the link under the description to view 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 messagebird.sms.create function.

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!

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.

QnAMaker.ai

Go to the “Creating a QnA service” page to create your service.

Here you will be asked to fill in the name (e.g GHC Bot) of your service and point it to an FAQ page (e.g. https://ghc.anitab.org/2018-faq/ )or a product manual page for it to crawl.

Once you are done hit “Create” to create your bot.

You will be given the opportunity to review the knowledge base you have created.

QnA Maker Knowledge Base

You can even delete QnA pairs or add an alternative phrasing of the same question. This will be useful for questions that do not match the existing knowledge base.

You can also test and train your bot.

QnA Maker tester

Once you are done hit “Publish” to publish your bot.

You will then be provided with a sample HTTP request. Keep this page open as you will need this information later on.

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 ./<username>/QAService run:

$ npm install --save axios

This will modify your package.json file and install the npm package axios into your service directory.

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 functions directory:

  1. 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
  2. messaging/more.js, a handler for incoming messages that contain the word “more” as their sole contents
  3. 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 notfound.js as most if not all questions will trigger this handler.

Copy the code below and replace the existing code in notfound.js

In line 17 and 18 you will need to fill in the uri and key from the Sample HTTP request that was provided to you by the QnAMaker.ai service.

Sample HTTP request provided to you by the QnAMaker Service

The uri is the string that comes directly after POST and the key is the string that comes after Ocp-Apim-subscription-key

Alternatively you can also add uri and key as environment variables in our env.json file.

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 contact@stdlib.com.

The last bit of code we will need to modify is adding our STDLIBTOKEN into our env.json

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:

Click “Show Token” to See Your Library Token

This is your StdLib auth token, click Show token to see its value and copy and paste it to your env.json file under STDLIBTOKEN for "local" and "dev"environments.

Default contents of the env.json file

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> \
--identifier <username>.QAService[@dev]

You can see a list of your claimed numbers by running $ lib messagebird.numbers.list if you need to see your claimed number again.

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.

Grace Hopper Conference Conceirge / FAQ Bot

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 careers@stdlib.com.


Tag cloud