AIML Tutorial: Get Your Bot to Ask Survey Questions
Bots can be developed to receive important information from customers and drive conversions.
May 29, 2019
One of the questions I am most commonly asked is, “How can I get my bot to ask the customer for information, such as their name, email address, location, etc., and then email the responses back to me?” You could create a few hard-coded categories in a topic to do a one-off survey, but it would be far more useful to have some code we can reuse if we need to run another survey or questionnaire. Also, if your survey has many questions, the AIML file is going to be pretty unmanageable after two or three questions. Let’s see how to do this in AIML using just four categories. The methods described here can easily be amended for your chatbot language of choice.
Step 1. Create the survey questions
First of all, we need some details about the survey. Where should the results be emailed? What is the name of the survey? What questions do we want to ask the user? We need to create a category to set these details up and then pass this information to the main survey category (SurveyLoop).
We say StartSurvey to begin the process, but this should ideally be <srai>ed to by the bot after it asks the user whether they want to complete our survey, since we wouldn’t expect the user to type this in manually.
Hopefully, it’s self-explanatory, but I’ll explain what each predicate means:
- surveyemail: Which email address should the bot send the results to?
- surveyname: What is your survey called?
- question1…question4: The questions we want in our survey. This is set up for four questions, but you can make as many as you like by adding new predicates for question5, question6, and question7. The important thing to remember is that the questions must start with the word question followed by the next number in the sequence.
- responses: A string to contain the survey responses, with a header.
- qnum: An internal predicate to allow the bot to remember which question number to ask. It should always be set to 1, since the bot needs to ask the first question before any of the others. Do not amend this predicate.
Step 2. Display the survey questions and quit out at the end
Now that we have everything set up, the bot should display each of the survey questions in turn, ready for the customer to reply to.
This category sets a predicate called question, which contains question number qnum. Remember that this is initially set to 1 in order to ask the first question. We then need to make sure that the question predicate actually contains a question to display to the user. If it’s set to unknown, the question doesn’t exist and so we end the survey. Otherwise, we set the topic to SURVEY and display the question for the user.
Step 3. Get the user’s response
After the question has been displayed, we wait for the user’s response. Why did we set the topic to SURVEY? Because, if the user’s responses matched one of your existing AIML categories, that would be triggered and the bot would forget all about the survey. The underscore wildcard means that whatever the user responds with, this category will be triggered.
We now have the user’s reply, so we can unset the topic, add the user’s response to the list of responses that they have already provided, increase qnum by one in order to ask the next question, and then go back to the main loop so the bot can display the next question.
How can we increase qnum by 1? We do this quite easily in Pandorabots by using a map called successor. This map is not displayed in your bot but is automatically built into all bots on the Pandorabots platform. The map looks a bit like this:
[ ["1", "2"], ["2", "3"], ["3", "4"], ["4", "5"], . . . . . . ]
So, if qnum is set to 1 and we trigger the map, the bot looks down the list to find the number 1 and sees that the response should be 2. Similarly, if qnum is set to something like 372, the bot will know that 373 is the next number in the sequence, although I wouldn’t recommend having a survey with 372 questions!
Step 4. Email the survey results
When there are no more questions to ask, the final category is called in order to send the responses to the email address we initially specified. It automatically includes our survey name in the subject line and puts the responses into the body of the email.
The bot now announces that the survey has finished and provides a button for the user to click. After it is clicked, the user’s default mail program will open, the responses will be displayed for them to check, and they can send the survey to you.
Let’s see it in action.
After the user clicks the Ok button, their default mail program will open, ready for them to send the completed survey to you.
And that’s it—a quick and easy piece of AIML to allow your bot to create a survey and email the results back to you. So how can we improve it?
The biggest thing we can do to make it more streamlined is to remove the need for the user to email the survey manually, since we are assuming that they have a mail client installed on their device. Instead of using mailto, we can use something like PHP to create a script that will email the survey to us. This also has the advantage that the user doesn’t see the work that goes on behind the scenes.
PHP is beyond the scope of this tutorial, but I’ve provided a link that provides more information about the mail function.
Finally, how can we get the bot to let the user know that the survey exists and to prompt them to fill it in? You can amend the opening line in your chat widget, but this may get annoying for the user if the bot asks this question every time they use it.
I personally would include it in your UDC so it’s only triggered if the user says something off topic.
You could also include it in common categories, such as hello or goodbye.
Anything else we can improve? Sure! In the first category, you could give some information about the survey rather than jumping straight in with the first question. Users like to know why they are being asked to fill in details, so something like this would improve the experience.
These are just suggestions, and you are welcome to tweak the code to your own requirements. Take a look at the full AIML code. Please feel free to download it, learn from it, and use it in your own bot.
To bring the best bots to your business, check out www.pandorabots.com or contact us at email@example.com for more details.