Home / DialogFlow ES / How to create a Quiz Bot in Dialogflow ES
DialogFlow ES

How to create a Quiz Bot in Dialogflow ES

In this article, I will explain how you can build a Quiz bot in Dialogflow ES where the user can answer multiple choice questions (by clicking on a button) and then the bot will calculate and display their quiz score at the end of it.

Here is the list of questions for this bot.

This is what the Mindomo flowchart looks like

I didn’t complete the flowchart, but you should be able to follow the pattern and do it.

But more interestingly, notice that this flowchart includes the “split and merge” pattern.

When you have two different intents which both have the same output context (await_answer_2 in this case), you have a split-and-merge pattern. The blue arrow is actually a merge, although I recommend people number the contexts so that they can avoid arrows which “cross connect” and make the flowchart harder to read.

Let us define the intents

First create an intent where the user will ask to start the quiz

Create an intent for the correct answer for question 1. Notice that we set the score1 to 1.

Add the intent for an incorrect answer for question 1. Notice that we set score1 to 0 in this case.

Now define the intent which handles the correct answer for question 2

Next we add the intent for the incorrect answer for question 2

Now follow the same patterns and create the intents for questions 3 and 4.

Finally, when user answers question 5 correctly, we need to call the webhook.

Similarly we also call the webhook if user provides the incorrect answer for question 5.

This is the core of the Python webhook code

def calculate_score(req):
    query_result = req.get('queryResult')
    session_vars = get_session_vars(query_result)
    sum_of_scores = get_sum_of_scores(session_vars)

    fulfillment_messages = json.dumps(query_result.get('fulfillmentMessages'))
    return_msg = fulfillment_messages.replace('_SCORE_', str(sum_of_scores))

    json_formatted = json.loads(return_msg)
    result = {
        "fulfillmentMessages": json_formatted
    with open('result.json', 'w', encoding='utf-8') as f:
        json.dump(result, f, ensure_ascii=False, indent=4)
    return result

The get_sum_of_scores just calculates the sum of the 5 scores

def get_sum_of_scores(session_vars):
    session_vars_params = session_vars.get('parameters')
    score1 = int(session_vars_params.get('score1'))
    score2 = int(session_vars_params.get('score2'))
    score3 = int(session_vars_params.get('score3'))
    score4 = int(session_vars_params.get('score4'))
    score5 = int(session_vars_params.get('score5'))
    return score1 + score2 + score3 + score4 + score5

Here is what the bot looks like in Dialogflow Messenger

<— End of article —>

This website contains affiliate links. See the disclosure page for more details. 
"The magic key I needed as a non-programmer"

The custom payload generator was the magic key I needed (as a non-programmer) to build a good demo with rich responses in DialogFlow Messenger. I've only used it for 30 minutes and am thrilled. I've spent hours trying to figure out some of the intricacies of DialogFlow on my own. Over and over, I kept coming back to Aravind's tutorials available on-line. I trust the other functionalities I learn to use in the app will save me additional time and heartburn.

- Kathleen R
Cofounder, gathrHealth
"Much clearer than the official documentation to be honest"

Thanks a lot for the advice (of buying and following your videos)! They helped a lot indeed. Everything is very clear when you explain, much clearer than the official documentation to be honest 🙂

Neuraz T
Review for Learn Dialogflow CX
"I will strongly recommend this course because even I can learn how to design chatbot (no programming background)"

I think Aravind really did a great job to introduce dialogflow to people like me, without programming background. He organizes his course in very clear manner since I have been a college professor for 20 years. It is very easy for me to recognize how great Aravind’s course is! Very use-friend and very easy to follow. He doesn’t have any strong accent when he gives the lectures. It is so easy for me to understand. Really appreciate it.

Yes, I will strongly recommend this course because even I can learn how to design chatbot (no programming background) after studying Avarind’s course, you definitely can!

Ann Cai
Review for Learn Dialogflow ES

Similar Posts

  • Hi,
    i have a question about the Python webhook code.
    Can someone explaine me what is “req” in “calculate_score(req)” and what is get_session_vars?