Building an SAP Conversational AI Chatbot that Integrates with Qualtrics and SAP Customer Experience (3/4)
All good things come to those who wait..
Yes, here comes the most exciting blog post in this series where we are going to create a Qualtrics survey and then programmatically (Phython-ically) create the survey session inside the chat itself. Cool, eh? Let’s go!
Building the Qualtrics Survey and App
Qualtrics surveys are typically presented to end-users as a link where the survey opens inside the Qualtrics platform. But what if, there is a need to present the survey in a different UI altogether? This is particularly the case with a chatbot where presenting a survey link would force the end-user to navigate away from the chatbot. An already engaged end-user inside a conversation with the chatbot would consider survey questions coming from chatbot as part of the overall chatbot experience. Fortunately, Qualtrics offers a Survey Sessions API that enables creation of a survey session outside of the Qualtrics platform. Once the survey session is created, it is possible to use this API to make updates to the survey and record responses.
For the purpose of the scenario presented in this blog series, we are going to create a Python app that would use the Qualtrics Survey Sessions API to create the survey session and orchestrate the survey flow.
But before we start with the development of our app, we need to choose an IDE. Microsoft Visual Studio Code (VS Code) is a favorite amongst application developers today. It offers a Python Development workload (extension) that comes with the necessary Python libraries and frameworks installed (even Flask).
Creating the (Flask) Project
We will start by creating a new Blank Flask Web Project in VS Code.
The template will generate two files, requirements.py and app.py with default code for a Flask object instance as well as a basic route definition. Navigate to app.py and change the host value from ‘localhost’ to ‘0.0.0.0’ in the generated code.
Let’s try and deploy our app. I know we did not put any code in the app yet. However, let’s try and create the endpoint for the API that our app is going to implement. First, make sure you have the CF command line interface installed. Open the Terminal and logon to the API endpoint on SCP where you want to deploy your app. Choose the correct dev space (if you have more than one).
Notice how the cf push command has the app name included. Alternatively, you can create a manifest.yml file in the project folder that would include the deployment values like app name, path, buildpack etc. Once deployed, the app would be started. Navigating to the app route/endpoint, we can see the root/default value coming from the default code in the app. We are now ready to build our app/API.
Let’s go back to the chatbot on your SAP Conversational AI platform and navigate to Settings > Versions. Under the Bot Builder we are going to put our app route/base URL. This would help us access the API using relative URLs. We will explore this in a later step.
Create Qualtrics Survey
Now we will go to the Qualtrics platform and create a survey that would be updated using our chatbot.
To keep it simple, we have created three questions in the survey, the first two being Multiple Choice questions and the third question is of type Text Entry.
We will need the Qualtrics data center, survey Id and the API token to access the survey from our app. Navigate to the Account Settings menu, go to the Qualtrics IDs tab and note down the survey Id and the API token. The data center is part of the platform URL.
Let’s now go back to the chatbot and create a new skill called qsurvey. For the skill trigger, we can create an intent @yessurvey with expressions indicating end-user’s consent for taking the survey. Going to the actions tab, we will create memory fields for the survey values we noted from the Qualtrics platform in the previous step. We will also create a field session that would be set to false once the survey session is created.
Create Survey Session Request
We now need to create a post request which would bring the data from our chatbot to our python app in order to create a survey session.
Adding to the qsurvey skill, select CONNECT EXTERNAL SERVICE > CALL WEBHOOK.
We will create a post request with a relative URL /createSurveySession (use the default body). If you notice here, we have the memory data structure getting sent inside the request. We will use this to access the saved data/field values inside the app.
Let’s go back to our app now in order to add the logic for the createSurveySession route. You can follow the API reference guide here to get sample code for the survey API requests and to understand the resulting responses in context of conversational APIs.
We need to retrieve the memory field values for surveyId, dataCenter and apiToken and create the baseURL, headers and the data to generate a response.
From the response object, we can retrieve the survey sessionId. The response object will also include all the questions with type and choices if any, default responses, embedded data and an end of survey message.
Next, we will store the questions from the response object inside a variable and access the first question. Depending on the type of question, the logic to display the question would differ.
In this blog post, we are going to create the logic to display a multiple-choice (Qualtrics question type ‘mc’) question. To display the multiple choices, we would be using the list structured message offered by the SAP Conversational AI platform.
Every element in the list would correspond to a choice in the multiple-choice question.
The elements along with the question text would then be added to a question object that would be sent in the response to the webhook call originating from the chatbot.
Next, we are going to update the chatbot memory with a couple of field values that will help us keep track of the current status of the survey in order to be able to advance the survey to the subsequent questions, if any.
Finally, we are going to return the response wherein we have the updated memory variable as well as the question that would appear inside the chat.
|The next step would be to record the response to the question coming in the chat. Going back to the Qualtrics platform, we can see the option numbers that correspond to the multiple choices in our question. This would mean that if a number (1-5) is entered inside the chat and the session field value is True, then this number would be treated as a response to a Qualtrics multiple choice (MC) question. For this purpose, you will create a new skill qMC that would get triggered if the respective conditions are met. We will save the MC option number inside a memory field value in order to use it inside our app.|
Next, we will add the code for the updateSurveyMC event that is going to be like the create survey event but this time on we would have the sessionId stored and coming from the chatbot memory. The option number would be recognized as a scalar entity by the SAP Conversational AI NLP engine.
The response is sent back to the Qualtrics platform using the surveyId, the sessionId and the response to the question.
This would also be an opportunity to determine whether the question asked would be the last question in the survey in which case, parameter ‘advance’ would have the value ‘false’.
Similarly, we can create the logic inside our app to record and send responses to text questions and so on. Once done, we will deploy the app and test the survey flow with the flow of the conversation inside the chatbot.
Conclusion: You can get creative and include a variety of question types in your Qualtrics survey and adapt your application to include the logic to process different types of questions. The fundamental idea is the same as explained in this blog post. In the next and final blog post, we will look at some additional API calls from inside the chatbot that would help un enrich our scenario and showcase the other neat features offered by the SAP Conversational AI platform.
For more information about SAP Conversational AI: