Contact Sales

All fields are required

Using SignalWire AI Agent to Create a SurveyBot | SignalWire
Developers

Using SignalWire AI Agent to Create a SurveyBot

Create a survey bot powered by SignalWire's AI voice agent

Senior Escalations Engineer

Shane Harrell

SignalWire AI Agent is a useful low-code tool that allows you to program an IVR or voice assistant in plain text language. However, if you’re a developer, you can use a little bit of extra code to build something more complex.

We’ve already created a SurveyBot script for you. In this post, you’ll find a working example of how to use an AI voice agent to connect to a database, retrieve a series of questions to ask the user, and then store the user’s responses to create your own SurveyBot.

The survey questions are retrieved from the database one at a time. This helps to limit the amount of data being given to the initial AI prompt, which means that it can be more direct in its instructions, and not get overwhelmed by large amounts of information.

Additionally, each subsequent prompt will be much smaller, and therefore less resource-intensive (meaning each call will cost less money)!

SurveyBot Use Cases

Use cases for the AI Agent are limitless. SurveyBot specifically can be used for simple questions, asking users what their favorite color, snack, and season are, which is what we will do in this example. However, the AI bot can ask whatever questions you need.

The bot can be used to ask callers financial questions to pre-approve a mortgage, credit card, or other personal loan. Or, it could be used to save time before a medical appointment by performing a basic health screening - asking the caller if they are having any symptoms, have any allergies to medications, and the reason for their call.

Election season is also just around the corner in the United States. Instead of relying solely on traditional phone polling, interviewers can deploy AI SurveyBots to reach a broader audience. These bots can conduct surveys via phone calls, collecting valuable data on voter preferences, concerns, and demographics.

How to set up the example survey

To get started, you’ll need a SignalWire account and at least one phone number. This is a basic example that uses a python webserver combined with sqlite, and ngrok to serve SWML to a webhook created from calling a SignalWire number.

You can change the prompt in any way to fit your needs, use your own questions, use your own database schema, etc. Otherwise, everything you need to get an example up and running is provided for you.

To get started, clone the repository that contains the survey bot example to your local machine.


This is a good time to start the ngrok tunnel so that the tunnel URL is ready. In a separate terminal, start the tunnel and set the http port to 5000 (the default port used by the flask module in python. This can be changed to anything else if desired) by typing:


The screen should now look something like the following. Make note of the URL listed at Forwarding. That will be the base webhook URL going forward.


Once the ngrok tunnel has been established, navigate into the directory where the example was downloaded. The first thing that needs to be done is to update the swml_base_web_hook_url.

Open main.py in a preferred text editor and change the following line to reflect the URL from NGROK above. This should be somewhere around line 15.

Skipping this step will cause the survey to misbehave. Make sure the URL is accurate and active!

Change

to


To ensure that the script can run without much additional setup, there is a requirements.txt file, which contains all its dependencies. Creating a python virtual environment and then installing the dependencies should be a quick way to ensure that the script will run without any issues.


Now we can add some questions for SurveyBot to ask the caller. In this example, we will be using INSERT statements on the database using the sqlite3 command line.


Here, you can change, add, or remove questions to suit your needs. Each question will be fed to SurveyBot individually once it is done asking the previous question.

Lastly, you will want to configure your SignalWire number to point to the ngrok URL that we created.

In your SignalWire Space, navigate to Phone Numbers and edit the number you’d like to use for this example. Set the call to be handled as ‘a SWML Script’ and paste in the Forwarding tunnel url from NGROK that was established earlier.

Then start the script.


You are now able to make a call to that number, and the AI voice assistant will ask the caller a series of questions from the database and record the answers.

Retrieving the survey results

Once a caller has completed the survey, the results can be retrieved from the database.

Access the database using the following command:


A record for the caller is created in the user table:


The user’s answers are stored in the survey_answers table:


A JOIN statement can be used on both of these tables, to make them more readable as to which question is being asked and answered by the caller:


We want to see what you can build using SignalWire AI agent! Share what you’re working on or bring your questions to our Slack channel or Discord to connect with the team.

Related Articles