Cliq Bots - How to make a bot respond to your messages?

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 from the bot suggestions. 

Let us try building a utility bot, that will bring us the top news from a particular category chosen from the list of suggestions. 
The sample workflow on triggering this handler is given below. 
  • The user pings the bot to get a list of current top trending news. 
  • Message handler of the bot identifies a keyword from the user's input and is triggered to respond with a list of categories for the user to choose from. 
  • Once the user selects an option, the message input is again captured to call the corresponding API.
  • This API response is shared by the bot to the user in a card format!
A sample code snippet for the above-mentioned scenario is given below. Take a look!

  1. response = Map();
  2. categories = {"Business","Entertainment","Gaming","General","Health-and-Medical","Music","Politics","Science-and-Nature","Sport","Technology"};
  3. if(message.containsIgnoreCase("NEWS") || message.containsIgnoreCase(" BREAKING NEWS") || message.containsIgnoreCase("HEADLINES"))
  4. {
  5. list = List();
  6. for each  category in categories
  7. {
  8. entry = Map();
  9. entry.put("text",category);
  10. list.add(entry);
  11. }
  12. suggestion = Map();
  13. suggestion.put("list", list);
  14. response.put("text","Hey " + user.get("first_name") + " ,choose one option from the list! I can help you with these. :smile:");
  15. response.put("suggestions",suggestion);
  16. }
  17. else if(categories.containsIgnoreCase(message))
  18. {
  19. url = getUrl("https://newsapi.org/v2/top-headlines?category=" + message.toLowerCase() + "&language=en&apiKey=<Insert_API_Key>");
  20. newslist = url.get("articles");
  21. rows = List();
  22. count = 0;
  23. for each  news in newslist
  24. {
  25. count = count + 1;
  26. row = Map();
  27. row.put("Headline",news.get("title"));
  28. row.put("View Link","[Read More](" + news.get("url") + ")");
  29. if(count <= 10)
  30. {
  31. rows.add(row);
  32. }
  33. }
  34. return {"text":"Hello! Here's the top news in the " + message + " category!","card":{"theme":"modern-inline","title":"Hi " + user.get("first_name") + " :smile:"},"slides":{{"type":"table","data":{"headers":{"Headline","View Link"},"rows":rows}}}};
  35. }
  36. else 
  37. {
  38. return {"text": "Hello There, this looks like an invalid category! Just try typing *News* or *Breaking News* or *Headlines*"};
  39. }
  40. return response;



The bot message handler is the easiest way to get started with building an interactive bot. So get started with it right away! 

Few useful links:




Comments and suggestions are welcome!

Best,
Manasa
Cliq


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