16 How to create a Quiz Bot in Dialogflow ES

Dialogflow ES Quickstart Templates Part 2

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

About this website

I created this website to provide training and tools for non-programmers who are building Dialogflow chatbots.

I have now changed my focus to Vertex AI Search, which I think is a natural evolution from chatbots.


BotFlo was previously called MiningBusinessData. That is why you see that watermark in many of my previous videos.

Leave a Reply