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.

Hogwhat? : In the Harry Potter series by J.K. Rowling, a teenage boy wizard is enrolled at a magical academy known as Hogwarts, which is divided into four houses: Gryffindor, Ravenclaw, Hufflepuff, and Slytherin, each with their own unique personality. Students are sorted into these four houses by the wise and somewhat cheeky Sorting Hat -- yes, a talking hat -- which just so happens to make for a perfect character to build our bot off of!

Sorting Hat Quiz

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 --

+ [*] (hey|hi|hello|sup) [*]
- Ahem... <delay seconds=1> Oh you may not think I'm pretty,\n 
^ but don't judge on what you see.\n
^ I'll eat myself if you can find \n 
^ a smarter hat than me! <send> There's nothing hidden in your head\n 
^ the Sorting Hat can't see,\n
^ So try me on and I will tell you \n
^ Where you ought to be. ^buttons("Get Sorted!")

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:

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:

+ get sorted {weight=10}
- {@ clear results}{topic=question-1} {@ question}
+ clear results {weight=2}
- <set gryffindor=0><set ravenclaw=0><set hufflepuff=0><set slytherin=0>

Let’s break down the response in this first trigger, step-by-step:

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:

+ question
- Alright, first question! Tell me what creature you want to bring with you on this journey. ^buttons(🦉 owl, 🐱 cat, 🐶 dog, 🐍 snake)
This method is one way we’ve taken to structuring topics for quizzes, but don’t be alarmed if this doesn’t match the same way you’ve written your bot script up! There’s definitely more than one way to structure a botscript!

Now, the student can respond with their favorite pet. This will match against one of the four triggers in the topic.

+ [*] owl [*]
- <add gryffindor=1> A most curious creature! <send> {topic=question-2} {@ question}
+ [*] cat [*]
- <add ravenclaw=1> Wise choice! <send> {topic=question-2} {@ question}
+ [*] dog [*]
- <add hufflepuff=1> A loyal companion! <send> {topic=question-2} {@ question}
+ [*] snake [*]
- <add slytherin=1> Slither on to the next question... <send> topic=question-2} {@ question}

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}.

Send splits messages

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:

+ [*] house|results [*]
- {topic=results} {@ results-first-time}

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.

+ results-first-time
- Hmm... difficult. <delay seconds=1> VERY difficult. <delay seconds=1> Plenty of courage, I see. Not a bad mind, either. There's talent, oh yes. And a thirst to prove yourself. But where to put you? <delay seconds=2> {@}
+ *
- <call>getResult</call> <send> Have I chosen wisely? ^buttons(See Results Again, Sort Me Again)

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.

> object getResult javascript
  var user = rs.currentUser();
  var scores = [
    {house: 'Gryffindor', val: rs.getUservar(user, 'gryffindor')}
  , {house: 'Ravenclaw', val: rs.getUservar(user, 'ravenclaw')}
  , {house: 'Hufflepuff', val: rs.getUservar(user, 'hufflepuff')}
  , {house: 'Slytherin', val: rs.getUservar(user, 'slytherin')}];
  var houseDescriptions = {
    Gryffindor: "You belong in Gryffindor, where dwell the brave at heart. Their daring, nerve, and chivalry set Gryffindors apart."
  , Hufflepuff: "You belong in Hufflepuff, where they are just and loyal. Those patient Hufflepuffs are true and unafraid of toil."
  , Ravenclaw: "You belong in wise old Ravenclaw, if you've a ready mind, where those of wit and learning, will always find their kind."
  , Slytherin: "You belong in Slytherin. You'll make your real friends, those cunning folks use any means to achieve their ends."
  var result, resultVal = 0;
  scores.forEach(function (s) {
    if (s.val > resultVal)
      result = s.house;
  return result + "\n " + houseDescriptions[result];
< object

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:

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.

+ *
- <call>getResult</call> <send> Have I chosen wisely? ^buttons(See Results Again, Sort Me Again)

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:

+ [*] (esc|restart|leave|exit|quit|sort me again|resort|restart) [*] {weight=10}
- {@ clear results}{topic=default} Are you afraid of what you'll hear?\n
^ Afraid I'll speak the name you fear?\n
^ Not Slytherin! Not Gryffindor!\n
^ Not Hufflepuff! Not Ravenclaw!\n
^ Don't worry, child, I know my job,\n
^ You'll learn to laugh, if first you sob! ^buttons("Get Sorted!")

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?