Creating an SMS Chatbot using 2 way SMS

Overview

SMS is a great way to communicate with customers. This is simple to set up using the Syniverse Developer Community. This example shows how to set up a simple chat bot that responds to key words sent via SMS.

Pre-requisites

It is assumed that you have already registered with the Syniverse Developer Community and run the Quick Start SMS example. If not then do this following the instructions here 

It is also assumed that you know some Python, since this example is written in Python. Ideally you have also heard of the Flask framework, though that is not essential.

This example will also require a modest investment, as you will need to rent a local phone number. This can be as little as a few dollars, but does vary depending on the country.

Set up SDC

Set up a Payment Method

To set up the simple SMS Chatbot, you will need to rent a phone number.

Before this can be done you need to set up a post-paid payment method.

This is described in more detail in the support documentation here

Update your application

In order to support 2 way SMS the Event Manager will be used to deliver SMS sent from a mobile. To enable this you will need to enable the Event Subscription Services in your existing application. This can be done by selecting edit and then scrolling down to Account and APIs.

Buy and Set up a Phone Number

First buy a sender address and then configure it as a private channel .

Test sending SMS from the phone number to your mobile using the Send Message function underneath Messaging, or by adding the Channel ID to your existing quickstart demo.

Set up delivery of Mobile Originated SMS

Next we want to configure the system to send SMS sent from mobiles to your preferred end point.

First set up a delivery configuration. I recommend using https://requestb.in or similar service first to test the service. To do this create a requestbin and use the URL provided in the address field of the delivery configuration.

Next create a subscription which sends messaging events, including mobile originated SMS, to this delivery configuration.

Test this by sending an SMS to the number from your mobile, and then refreshing the requestbin you created. This will then show you the content of the notification.

Set up your Text Bot end point

Our example text bot is written in Python and uses the Flask framework. It is deliberately simple but provides an easy way to test out a more advanced SMS use case.

The text bot comprises

  • A REST service created using Flask which receives POST calls from the Event Manager and parses the notification to check it contains an SMS.
  • A simple method which checks whether the SMS contains a keyword contained in a dictionary. If so then it sends the matching response back to the sending mobile number.

To host our example we are using a cloud hosting service called PythonAnywhere. This provides a hosted Python environment and infrastructure using Amazon cloud. There is even a free tier that can be used to run this demo.

Sign up at PythonAnywhere

Follow the instructions at https://www.pythonanywhere.com/ and select the free tier.

Set up the Flask Hello World Example

Using the guided Web App creation tool to create a hello world Flask App. You can do this by going to the Web Tab and hit Add a new Web App, and choose Flask and Python 2 (the version of Python the example is written in).

Alternatively there are manual instructions here.

If you want to learn more about Flask there are a lot of resources on the internet. The main resource I used to build this example is here.

Install the Text Bot Flask App

The example Text Bot code is stored on GitHub in https://github.com/Syniverse/Example-2WaySMS-TextBot-Python

To install and set this up

  • Open a bash console on pythonanywhere and create a new directory
  • Use git clone to download the code to the directory
  • Use your preferred editor to
    • Add the access token for the SDC application
    • Add the private channel ID
    • Save the changes
  • Then copy py to app.py in the flask directory, overwriting the existing Hello World Flask application.
  • Reload the web application via the pythonanywhere Web Tab.

Your Text bot / REST service is now live at http://your-username.pythonaywhere.com/simplebot/v1/notification

Test the REST App

There are a few ways we can test this.

You can use Postman to test the Application from your desktop. A generic POST request should provide a HTTP 201 response saying ‘hello not an SMS’

You can also use POSTMAN to simulate the notification itself using the example notification provided in the last section of the documentation page here.

Update the Delivery Configuration

Once your REST app is set up then have one final change needed, to point the delivery configuration to the REST App.

You can do this by editing the delivery configuration and changing the address to the URL for your REST service.

Adding International Support

After you've launched in one country, it is simple to add more.

All you need to do is to repeat the buy and set up phone number set for the additional countries, and add them to the existing private channel you have already created.

Text Away!

After all this hard work you can now get busy trying out the chatbot.

 

Was this article helpful?
0 out of 0 found this helpful

0 Comments

Please sign in to leave a comment.