Documentation

Do you use documentation to use NewBot Framework

Ask a question in a loop

In the previously generated skills/quiz/quiz.converse file, we write a function that will be triggered when the user says play

@Intent(/play/i)
play() {
   
}

This function contains a table of questions:

@Intent(/play/i)
play() {
    questions = [
        {
            str: 'What is the capital of France?',
            choices: ['Paris', 'Lyon', 'Marseille'],
            correct: 0
        },
        {
            str: 'What is the largest planet in the solar system?',
            choices: ['Earth', 'Jupiter', 'Saturn'],
            correct: 1
        },
        {
            str: 'Who bought Youtube in 2006?',
            choices: ['Microsoft', 'Yahoo', 'Google'],
            correct: 2
        }
    ]
}

The painting has objects with

  1. The question (str)
  2. An array of choices (choices)
  3. The index of the correct answer (correct)

The complete code is as follows:

$score = 0

@Intent(/play/i)
play() {
    questions = [
        {
            str: 'What is the capital of France?',
            choices: ['Paris', 'Lyon', 'Marseille'],
            correct: 0
        },
        {
            str: 'What is the largest planet in the solar system?',
            choices: ['Earth', 'Jupiter', 'Saturn'],
            correct: 1
        },
        {
            str: 'Who bought Youtube in 2006?',
            choices: ['Microsoft', 'Yahoo', 'Google'],
            correct: 2
        }
    ]
    for (question of questions) {
        
        @Format('quickReplies', question.choices)
        > { question.str }

        Prompt()

        index = Array.indexOf(question.choices, :text)
        
        if (index == question.correct) {
            > Very good answer
            $score++
        }
        else {
            ---
            > It's wrong
            > Wrong answer
            ---
        }

    }

    > Your score : { $score } / { questions.length }
}
  1. We go through the board with the for loop
  2. We post the question
  3. We are waiting for the user's response with Prompt()
  4. We retrieve the index of the text entered which is in the table of choice
  5. A condition makes it possible to check if the index is the same as the index of the correct answer
  6. If the answer is good, we increment the global variable $score. This keeps the user's score in mind.

© 2019 NewBot