Cliq Bots - Building conversational bots with a series of questions and user inputs!

Cliq Bots - Building conversational bots with a series of questions and user inputs!

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 : 

  1. response = Map();
  2. if(context_id.matches("QUIZ"))
  3. {
  4. categories = {"Movies":11,"Politics":24,"Sports":21};
  5. if(categories.containKey(answers.get("quiz").get("text")))
  6. {
  7. topic = categories.get(answers.get("quiz").get("text"));
  8. }
  9. urlresponse = getUrl("https://opentdb.com/api.php?amount=5&category=" + topic + "&type=multiple");
  10. questions = urlresponse.get("results");
  11. info questions;
  12. randomNumbers = (getUrl("https://www.random.org/integers/?num=5&min=0&max=3&col=20&base=10&format=plain&rnd=new")).toList("\t");
  13. info randomNumbers;
  14. questionList = list();
  15. options = {0,1,2,3};
  16. count = 0;
  17. for each  question in questions
  18. {
  19. suggList = list();
  20. randomNumber = randomNumbers.get(count);
  21. info randomNumber;
  22. optionIter = 0;
  23. for each  opt in options.toList()
  24. {
  25. if(opt == randomNumber)
  26. {
  27. suggList.add({"text":question.get("correct_answer")});
  28. }
  29. else
  30. {
  31. suggList.add({"text":question.get("incorrect_answers").get(optionIter)});
  32. optionIter = optionIter + 1;
  33. }
  34. }
  35. questionString = question.get("question");
  36. questionString = questionString.replaceAll(""","'").replaceAll("'","'");
  37. q = {"name":"q" + count,"question":questionString,"suggestions":{"list":suggList}};
  38. questionList.add(q);
  39. questionList.add({"name":"a" + count,"question":questionString,"value":{"text":question.get("correct_answer")}});
  40. count = count + 1;
  41. }
  42. return {"text":"All the best for the quiz!  :victory:","context":{"id":"QuizChallenge","timeout":"600","params":questionList}};
  43. }
  44. else if(context_id.equalsIgnoreCase("QuizChallenge"))
  45. {
  46. marks = 0;
  47. q = {0,1,2,3,4};
  48. str = "";
  49. for each  no in q.toList()
  50. {
  51. qno = no + 1;
  52. userAns = answers.get("q" + no);
  53. origAns = answers.get("a" + no);
  54. if(userAns.equalsIgnoreCase(origAns))
  55. {
  56. str = str + qno + ". " + origAns.get("text") + "\n";
  57. marks = marks + 1;
  58. }
  59. else
  60. {
  61. str = str + qno + ". " + origAns.get("text") + "\n";
  62. }
  63. }
  64. str = str + "Score : " + marks + " / 5\n";
  65. quotes = {"5":"You're awesome!","4":"Great","3":"Good!","2":"Ok!","1":"Try again!","0":"Better luck next time!"};
  66. str = str + quotes.get("" + marks);
  67. return {"text":str};
  68. }
  69. return response;

Here's the message handler code :

  1. response = Map();
  2. if(message.containsIgnoreCase("Quotes"))
  3. {
  4. url = getUrl("https://talaikis.com/api/quotes/random/");
  5. quote = url.toMap();
  6. response = {"text":quote.get("quote"),"card":{"theme":"prompt","title":"Here's a quote for you by " + quote.get("author") + " :smile!:"}};
  7. }
  8. else if(message.containsIgnoreCase("Quiz"))
  9. {
  10. 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"}}}}}};
  11. response.put("context",context);
  12. }
  13. else
  14. {
  15. 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"}}}};
  16. }
  17. 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. 

 

Best,

Manasa

Cliq

    Access your files securely from anywhere

        Zoho Developer Community




                                  Zoho Desk Resources

                                  • Desk Community Learning Series


                                  • Digest


                                  • Functions


                                  • Meetups


                                  • Kbase


                                  • Resources


                                  • Glossary


                                  • Desk Marketplace


                                  • MVP Corner


                                  • Word of the Day



                                      Zoho Marketing Automation
                                              • Sticky Posts

                                              • Convert a message on Cliq into a task on Zoho Connect

                                                Message actions in Cliq are a great way to transform messages in a conversation into actionable work items. In this post, we'll see how to build a custom message action that'll let you add a message as a task to board on Zoho Connect. If you haven't created
                                              • Cliq Bots - Post message to a bot using the command line!

                                                If you had read our post on how to post a message to a channel in a simple one-line command, then this sure is a piece of cake for you guys! For those of you, who are reading this for the first time, don't worry! Just read on. This post is all about how
                                              • Cliq Bots - How to make a bot respond to your messages?

                                                Bots are just like your buddies with whom you can interact. They carry out your tasks, keep you notified about your to-dos and come in handy when you need constant updates from a third party application.  So, how can you make your bot respond to a message? The bot message handler is a piece of code triggered when a message is sent to the bot. Message handlers help you customise your bot responses to make it look conversational. The message input from the user can be either a string or an option selected
                                              • Cliq Bots - Get notifications about any action on an application with the incoming webhook handler!

                                                Webhooks can be used to get notified about events happening in other applications inside Cliq. All bots in Cliq have their own incoming webhook endpoint. This makes it simple to post messages to the bot from external applications. Unlike the send message
                                              • The Slash Command Series - Types of Command Suggestions

                                                Hi Everybody! I hope you guys tried the /zdocs command and now have an idea of how command suggestions with click to execute work. If you have no clue of what command suggestion is, I recommend you to take a look at all the Slash Command Series posts, especially the one on Command Suggestions ! This post is all about the different types of command suggestions.  Customise your command suggestions  Did you know you could customise your command suggestion list with a title, description, image? Well,


                                              Manage your brands on social media



                                                    Zoho TeamInbox Resources

                                                      Zoho DataPrep Resources



                                                        Zoho CRM Plus Resources

                                                          Zoho Books Resources


                                                            Zoho Subscriptions Resources

                                                              Zoho Projects Resources


                                                                Zoho Sprints Resources


                                                                  Qntrl Resources


                                                                    Zoho Creator Resources



                                                                        Zoho Campaigns Resources


                                                                          Zoho CRM Resources

                                                                          • CRM Community Learning Series

                                                                            CRM Community Learning Series


                                                                          • Kaizen

                                                                            Kaizen

                                                                          • Functions

                                                                            Functions

                                                                          • Meetups

                                                                            Meetups

                                                                          • Kbase

                                                                            Kbase

                                                                          • Resources

                                                                            Resources

                                                                          • Digest

                                                                            Digest

                                                                          • CRM Marketplace

                                                                            CRM Marketplace

                                                                          • MVP Corner

                                                                            MVP Corner





                                                                              Design. Discuss. Deliver.

                                                                              Create visually engaging stories with Zoho Show.

                                                                              Get Started Now