Simple Tree Example

Below is the setup for an example survey which asks three basic questions to a user. There are no branches and all responses allow for free text. The questions are based on the bridgekeeper scene from “Monty Python and the Holy Grail”.

Creating Questions

We will create the three questions via the admin interface with the following data:

# Questions
pk: 1
text: What is your name?

pk: 2
text: What is your quest?

pk: 3
text: What is your favorite color?


The pks are listed for future reference but these would be auto generated by the database.

Creating Answers

Next we will create an allowable answer in the admin. These questions don’t require exact matches so we will just accept any text:

# Answers
pk: 1
name: Free Text
type: Regular Expression
answer: .*


This regular expression will match anything. You may need to make this express less allowing depending on your needs.

Associating Questions and Answers

Questions and answers are associated via tree states and transitions. Since we don’t have any branches the tree states and questions will be tied in a one to one fashion:

# Tree States
pk: 1
question: 1

pk: 2
question: 2

pk: 3
question: 3

Within a tree state you can also optionally specify the number of allowable retries but these have been excluded for simplicity.

Transitions determine how users are moved through the tree based on their answers. Here we will allow any text for each question and simply move the user on to the next question:

# Transitions
pk: 1
current state: 1
answer: 1
next state: 2

pk: 2
current state: 2
answer: 1
next state: 3

pk: 3
current state: 3
answer: 1
next state: null

Transitions can also be tagged and notifications can be sent when those tags are triggered. For instance you may create a new answer which is for the text ‘blue’ and have a new transition which handles the case when the user had the favorite color blue. Again this has been excluded for simplicity.

The Survey Tree

At this point the tree structure is in place to ask the series of questions but we need a way to start the survey. Again in the admin we would create a Tree with a trigger keyword which asks the user the first question:

# Tree
pk: 1
trigger: #test
root state: 1
completion text: Go on. Off you go.

Now when an incoming SMS matches the trigger text #test we will respond with the question from state one “What is your name?”. They will proceed on with each question and when finished we will respond “Go on. Off you go.” This completion text is optional.

At this point we have a simple yet functioning linear survey tree. An example SMS workflow is given below:

555-555-1234 >>> #test
555-555-1234 <<< What is your name? # This is state 1, question 1
555-555-1234 >>> My name is Sir Lancelot of Camelot.
555-555-1234 <<< What is your quest? # This is state 2, question 2
555-555-1234 >>> To seek the Holy Grail.
555-555-1234 <<< What is your favorite color? # This is state 3, question 3
555-555-1234 >>> Blue.
555-555-1234 <<< Go on. Off you go. # End of questions

Continuing reading to see how we can add branches to this series of questions.