Quiz Bot
Walkthrough Level: Advanced
The only time people seem to love taking tests is when that test happens to be a personality quiz! Luckily, chatbots lend themselves quite well as an interactive medium for personality quizzes that engage users with your specific brand.
In this walkthrough, we’ll make our own Buzzfeed-esque personality quiz to sort users into one of the four Hogwarts houses from the popular Harry Potter book and movie series.
Follow Along!
Follow along with this walkthrough by clicking the Quiz Bot theme when you “+ New Bot”.
Alternatively, after you've logged in to your Dexter account, click the button below to clone this bot into your account!
The Sorting Ceremony
All first-year students to Hogwarts begin their school years in a Sorting Ceremony where the Sorting Hat serenades the new students in song and explains the traits and virtues of each House. Our Sorting Hat Bot is no different --
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Upon the users (or new students, if you will) first interaction ("Hi!") with the Sorting Hat bot, we break into song and prompt them to get started with the Sorting Process a button labelled “Get Sorted!”.
We make use of a few fun concepts here:
- A slight <delay> is added to build in the effect of someone clearing their throat prior to a speech.
- The
^
and\n
characters are used here in combination to allow a bot creator to visualize their drafted response in the intended poetic verse format.
Keeping Score, That’s What the Sorting Hat is For...
Let’s say the student is ready to get sorted ("Get sorted!") and matches the first trigger below:
|
|
|
|
|
|
|
Let’s break down the response in this first trigger, step-by-step:
- With
{@ clear results}
, we look into the trigger titled+ clear results
. We begin by creating four bot variables: one for each house (“gryffindor”, “ravenclaw”, “hufflepuff”, and “slytherin”) and setting the score for getting into each house equal to zero. - Next, we use
{topic=question-1}
to redirect the user out of the default “topic” and into a new topic “question-1” for the first question. - Now, we still haven’t responded to the user with actual text yet:
- The first bit
{@ clear results}
only sets variables, it has no text associated with it. - Meanwhile, the second bit
{topic=question-1}
is only a redirect and also has no text associated with it. - The last bit, however, will look for a trigger that matches
question
in the “question-1” topic and will respond with any text associated with that response.
- The first bit
If you look in the “question-1” topic, you’ll see that the “question” trigger does indeed have a text response that asks the user the first Sorting Hat Bot question:
|
|
|
Now, the student can respond with their favorite pet. This will match against one of the four triggers in the topic.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Depending on the pet choice the student has made, we will add a point to the user variable associated with that house and respond with some smart Sorting Hat Bot commentary on the student choice.
<add>
(and its sibling directions (<sub>, <mult>, <div>
) make use of scripting concepts we don’t cover in the Dexter docs, but you can learn more about here.
You'll see we also include a <send>
direction here. This is because we also want to have a split to make sure that the repartee is separate from the text in the second question {topic=question-2} {@ question}
.
The next three questions follow a similar format, so rather than repeating ourselves (keeping it DRY), we’ll skip ahead to the end of question four when the student is ready to be sorted.
Let the Sorting Now Begin...
As we reach the end of “question-4”, we'll need to enter our final topic called ”results”, which we use to direct the user towards their quiz results or sorting assignment in the catch-all trigger of the “results” topic. When the student says "Tell me my house!", they will match the following trigger in the "question-4" topic:
|
|
|
We've also included a fun little easter egg within the quiz!
Rather than redirecting straight to their house assignment decision in the {@}
trigger, we first direct them to the {@ results-first-time}
trigger which uses makes use of <delay> to simulate the Sorting Bot’s thinking process.
|
|
|
|
|
|
|
Macro Magic
Now comes the hard stuff, we actually need to figure out what house the student belongs in. We do this with a little magic by writing up a Javascript macro.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
If you have any experience with Javascript before, hopefully, the following should be fairly straight-forward.
For those new to the programming language, we’ll cover the bare basics of how this macro works:
var user = rs.currentUser()
: We begin by capturing the internal userId of the current user in theuser
var.var scores
is a Javascript object we create that has an item for each “house” and a ”val” associated with the score they received for that house throughout the quiz. We can access this score by usingrs.getUservar(user, [houseName])
.var houseDescriptions
is a Javascript object with the house name as the key and the house description as the value.- Next, we iterate in the
forEach
loop through each item inscores
to find the house item with the highest score. - Lastly, we return a string with the result and the house description associated with that result.
We are able to use this macro by calling it with <call>getResult</call>
. When we do this, we replace that <call>
with the String result of the macro.
|
|
|
Finally, we end our quiz with a chipper response and ^buttons()
to once again guide the users on possible ways they can further engage with the bot.
Did You Put Me in the Right House?
If the user chooses to “See Results Again”, the same catch-all will be hit and the macro will be called once more, but if the user chooses to restart the quiz by asking the bot to “Sort Me Again”, we will instead match against the following trigger that lives in the “default” topic:
|
|
|
|
|
|
|
|
|
|
|
|
|
The reason why this trigger is matched instead of the “catch-all” in the results topic is because of the “weight” functionality of trigger. Once we reach this trigger, we once again clear the results and finally reset the topic to “default” so that the user is no longer in the “results” topic.
We Want to Take Your Quizzes
Phew, that was a long one to get through, but thanks for sticking with us through it!
We hope you have as much fun thinking up quiz ideas as we had writing this one. If you come up with any cool quiz chatbots, or if you need something a tad bit more complicated that we haven’t covered above, please do shoot us an email!
After all, who doesn’t want to learn what type of cheese they are?