We all know how important it is for a bot to manage the conversation flow with a user. And so, here we bring to you the bot context handler! This post introduces the concept of a context and how a chain of questions and user inputs can be achieved through the context.
Simply put, the context allows the bot to ask a series of questions pertaining to a topic of interest. The responses or the user inputs are then collected to perform an action at the end via the context handler. Sounds interesting? Let's jump straight into the working of the bot's context handler!
So how does the bot context handler work?
The bot context handler is designed to handle multiple questions and collect the user responses. That is, a 'context' map has to be defined with the list of questions and their expected responses (as bot suggestions) from the user. This way your bot can easily ask questions to the user and collect all the information required for performing an intended action. The nitty-gritty of this handler is explained on our
help page
Key points about the context handler:
- The context handler allows the bot to build and maintain a conversational flow, more like a dialogue exchange with a series of questions.
- This will be triggered only upon the initiation of the context map.
- Other bot handlers will be triggered to execute only if there is no ongoing context.
- Also, an added advantage of using this handler is, your bot can handle multiple conversation chains smoothly unlike other handlers!
So the context handler for the win!
Get started with a sample use case...
Let's say you are planning on creating a Quiz Bot for your team. A quiz bot that can engage the user by asking questions from a list of topics as suggestions. Once the user selects a topic, the bot will get started with the questions. Once the user answers a question, the answer will be saved followed by initiation of the next question. Take a look at how the context handler can be used in this scenario!
The sample context handler code is given below :
- response = Map();
- if(context_id.matches("QUIZ"))
- {
- categories = {"Movies":11,"Politics":24,"Sports":21};
- if(categories.containKey(answers.get("quiz").get("text")))
- {
- topic = categories.get(answers.get("quiz").get("text"));
- }
- urlresponse = getUrl("https://opentdb.com/api.php?amount=5&category=" + topic + "&type=multiple");
- questions = urlresponse.get("results");
- info questions;
- randomNumbers = (getUrl("https://www.random.org/integers/?num=5&min=0&max=3&col=20&base=10&format=plain&rnd=new")).toList("\t");
- info randomNumbers;
- questionList = list();
- options = {0,1,2,3};
- count = 0;
- for each question in questions
- {
- suggList = list();
- randomNumber = randomNumbers.get(count);
- info randomNumber;
- optionIter = 0;
- for each opt in options.toList()
- {
- if(opt == randomNumber)
- {
- suggList.add({"text":question.get("correct_answer")});
- }
- else
- {
- suggList.add({"text":question.get("incorrect_answers").get(optionIter)});
- optionIter = optionIter + 1;
- }
- }
- questionString = question.get("question");
- questionString = questionString.replaceAll(""","'").replaceAll("'","'");
- q = {"name":"q" + count,"question":questionString,"suggestions":{"list":suggList}};
- questionList.add(q);
- questionList.add({"name":"a" + count,"question":questionString,"value":{"text":question.get("correct_answer")}});
- count = count + 1;
- }
- return {"text":"All the best for the quiz! :victory:","context":{"id":"QuizChallenge","timeout":"600","params":questionList}};
- }
- else if(context_id.equalsIgnoreCase("QuizChallenge"))
- {
- marks = 0;
- q = {0,1,2,3,4};
- str = "";
- for each no in q.toList()
- {
- qno = no + 1;
- userAns = answers.get("q" + no);
- origAns = answers.get("a" + no);
- if(userAns.equalsIgnoreCase(origAns))
- {
- str = str + qno + ". " + origAns.get("text") + "\n";
- marks = marks + 1;
- }
- else
- {
- str = str + qno + ". " + origAns.get("text") + "\n";
- }
- }
- str = str + "Score : " + marks + " / 5\n";
- quotes = {"5":"You're awesome!","4":"Great","3":"Good!","2":"Ok!","1":"Try again!","0":"Better luck next time!"};
- str = str + quotes.get("" + marks);
- return {"text":str};
- }
- return response;
Here's the message handler code :
- response = Map();
- if(message.containsIgnoreCase("Quotes"))
- {
- url = getUrl("https://talaikis.com/api/quotes/random/");
- quote = url.toMap();
- response = {"text":quote.get("quote"),"card":{"theme":"prompt","title":"Here's a quote for you by " + quote.get("author") + " :smile!:"}};
- }
- else if(message.containsIgnoreCase("Quiz"))
- {
- context = {"id":"QUIZ","timeout":"300","params":{{"name":"quiz","question":"Well, let us get started by choosing the category! Shall we? :grinning:","suggestions":{"list":{{"text":"Movies"},{"text":"Politics"},{"text":"Sports"}}}}}};
- response.put("context",context);
- }
- else
- {
- response = {"text":"Hi! I am Cupcake, your not so human friend. My motto in life is to stay happy always and I try to make everyone else happy too! :happy!: By just being myself. I am super cute. Just try asking me something, will you?","suggestions":{"list":{{"text":"Motivational Quotes"},{"text":"Quiz"}}}};
- }
- return response;
Check out this video for the working of the above given example!
We hope the context handler gave you an idea of how easily bot conversations can be framed! Let us know how this post helped you in the comments.