Cisco Spark bot for monitoring Spark bots. Say whaaat!

The last few weeks I have been working on and testing a new Cisco Spark bot that monitors all my other bots using a RESTful API. Its a relatively simple bot and thanks to an existing Nodejs project from Qawelesizwe Mlilo that was built  to monitor websites I was able to make a few simple changes so it is now a Spark bot that monitors other bots using a RESTful API. Qawelesizwe original project is called node-ping and is posted on GitHub. It has a number of files but the main module is node-monitor which is its own node module available on NPM. I made changes to a couple of the files on the original project with the largest change being removal of the email module to replace with my own Spark message module.

How it Works
This bot works by monitoring a website through the HTML status codes which it pings using a time interval(this is set in the websites.json file). This is very handy because if you are using Express with Nodejs all the status codes are taken care of for you, all you need to do is build a new route on your bot to handle the inquiry from the monitor bot(see the code later in the post). The monitor bot looks at the status code response and adjusts the status of you monitored bot accordingly and sends a Spark message when it changes. If you already have an existing website for your bot that is hosted with the same bot application you could use that as well, but I choose to create a new route so in the future I can deliver more JSON data with my response for future features.

Coding the Monitor Bot
The changes I made to the original node-monitor module are pretty minor. I added a new attribute to the constructor to describe the current bot state. I defaulted all bots to down state. Below is a exert of node-monitor file showing the constructor change.

Once this was done I took the rest of the node-ping sample code and added the ability to send Spark messages using a bot account instead of using email(boo, no one likes more email) and also adjusted the events to relay up and down status so when a bot comes back up the monitor bot lets me know and stops sending me Spark messages.

Sending a Spark message instead of an email used the file below. Make sure to place in your own bot token and adjust the events.js file for where you place this file.

The final change for the bot monitor application was adjusting the server.js file to add the new event for when a monitored bot comes back online.

The last change you will need to make is on the bot you want to monitor. If your using Express see below. The change involves setting up a new route to respond to the request from the bot monitor.Seeing as you may or may not use an actual website to monitor I created a REST response. In my case I give a quick JSON response but in future versions I am planning on generating some data to respond with, like rooms configured and up time stats that can be logged.

This is just the start of my monitor bot which as you can see has some rough edges but it works and just today it alerted me to a down bot which I had to address. One thing I want to do in the future is a daily bot monitor report delivered in Spark to let me know how my other bots are doing. Right now I have no way of knowing if the monitor bot has gone down so a daily report will help there as well, unless I build a bot to monitor the bot that is monitoring all my other bots, but then how would I know if that bot then went down. I know another bot........

If you want to check out the original Monitor module here is a blog post that describes it. Also here is the GitHub site.


XMPP to Spark Bot - Presence Upgrade and Simplification!

Last September I first published my XMPP to Spark bot based on Nodejs that I created to consolidate my collaboration tools. Over the holiday break I made some upgrades and also simplified or I should say reduced the amount of code and modules required. The official Cisco Spark SDK is pretty large and bulky for the purposes of this bot so in the new version I have created a couple of simpler methods to enable its removal. This bot doesn't use webhooks and a bunch of other Spark API's so it was much easier to just create a couple of new methods. This bot is about getting others to use Spark versus a migration or integration tool but with expanded development it could be made to serve both of those purposes as well.

Below is the main server file. I haven't done a lot of commenting inside my code but you will notice I have a bot monitoring service for some experimenting. I am just monitoring and posting my cloud containers CPU up-time to a bot account in Spark. Eventually I am thinking about sending these updates to another monitoring service which posts updates and alerts on bot issues. You will also notice that I have added the ability to translate presence from Spark into XMPP. It is simple and readable. Presence only updates if my Spark presence/activity changes.It is defaulted on startup to "inactive".

This is a simple and fun personal bot to experiment with. Presence is new inside the Spark API and if your building a personal Spark bot the easiest way to get access is through the "get my own details". The "status" label gives your current presence.

Have fun coding!

Getting Started with Spark API's for Non-Coders

Once considered the domain of the technical elite, application programming interface or API's are now more open and available than ever but it's not the API's themselves that are increasing viability. It is an ever increasing ecosystem of platforms that are helping us plug API's into, together and around us that have changed the way we look at how we can get stuff done and allowing the average user access to the forbidden fruit.

When I recently presented at a conference in Seattle I wanted a group of non-coders to realize that API's are something worth taking a closer look at. I thought what makes more sense than to categorize their use into ever increasing levels of complexity. I come up with four levels.

Level 1 - Easy peasy lemon squeezy (that is British for, "yes, its f@#$%^ easy")

Level 2 - A little harder but my 10 year could still work it out

Level 3 - I might need some help, please standby

Level 4 - Please help me I just had a brain hemorrhage

Level 1 is base level, can't get any simpler out of the box. While most of you might overlook the fact this is using API integrations the Cisco Spark Depot is simple easy to use click and go API and bot integration. There is no coding, you can access it straight from the Spark application from within a room and there is not too much to think about.

Level 2 is getting a little harder by using a API broker. Now when I say harder I mean you have to go outside the application and visit a website. So in reality not too hard. But there are some steps to get you integration working but sites like IFTTT and Zapier make it pretty easy to get your applet/zap/whatever you want to name it working.
  • IFTTT - This is the easier of the two examples to work through. If This Then That makes setting up integrations pretty simple with click and go work flows.
  • Zapier -  Along the same lines as IFTTT but allows more complex work flows and trigger scenario's.
Level 3 is more complex but not beyond the skill level of most IT pro's. While the works flows at level 3 are a mix of drag and drop and there is some coding to create more complex scenario's and integrations. If you want to take a look at some try:
  • Gupshup - This site along with hosting your chatbot across multiple platforms has the ability to also host Javascript code to perform more complex API interactions. While the hosting and initial integrations are taken care of most of the hard work will be done with your hosted code.
  • - Now part of Google this cool NLP platform has a lot of prebuilt NLP domains for you to use to get you started. It also has the ability to host the integration to Spark to leave you to do the business logic which it has the ability to integrate with using Webhooks. This is close to a level 4 but seeing as it does some of the hosting work for you it's making your life easier getting it a level 3.
  • - This is an interesting platform that does drag and drop integrations along with the ability to host custom code. It makes your life easier by doing the hosting for you and providing some nice drag and drop integrations. 
Level 4 is custom code. The most complex of the 4 levels that requires a in depth knowledge of programming although you would be surprised what you can write with less than expert knowledge. Some of the interactions with technical non-programmers over the last year or so have highlighted the fact that people just don't know where to start when it comes to this level. If you have never tackled programming before it is a challenging endeavour but not impossible. The fact there is so many paths you can take can put some people off but sticking with it will in the end bring results. Some of benefits and challenges are:
  • Ultimate in business logic customization
  • Ability to use open source frameworks and platforms (Nodejs, Node-Red, Spark Flint, etc.)
  • Access to all sorts of API providers (Google, Facebook, Salesforce, etc.)
  • Language and platform flexibility (Javascript, Python, .net, Java, etc.)
  • Lots of hosting choices (AWS, Heroku, Azure, etc.)
  • Too much choice leading to analysis paralysis.
Having choice of course is a good thing and while level 1 and 2 are much simpler you would be surprised what sort of integrations you can create. Give it a go and it may inspire you to move on to levels 3 and 4.


Using IBM's Watson Conversation API with Cisco Spark in Node.js

IBM's Watson has a number of APIs but the one I am dealing with in this post is the Conversation API which has some interesting features. Even though IBM are clearly still adding features to this fairly new API it is pretty easy to work with once you understand how context can be used. I am not going to go over the basics of the API which if you have never worked with Watson before there are a million You Tube videos on the basic's. Seems even a 13 year old can put a video together on how to use Watson. I did watch this video BTW and its a handy starting place for the basics but one thing that is missing from all the videos is how to use the context data to get information back and forth from your bot to Watson. The video's mention it and the Watson documentation talks about it but it is a little confusing to start with.

What is Context?

Context is a pretty commonly used phrase in natural language processing(NLP) systems, it allows you to pass information back and forth between your bot and the NLP processor, in our case Watson. It does this using a JSON format which can include basically any attribute you want to put into it. Watson does have its own tracking with the conversation_id but tracking of the user and the conversation the user is currently in is up to you. I like to think of context as a cookie that gets passed back and forth. If you are working with a chat app in the browser the browser can take care of the cookie for you. But in our case with Cisco Spark you have no such luck. You as the developer have to track context which is included with every response from Watson. Watson will update the context with information from your users and information Watson uses to track the conversation. Without the mechanism to track the conversation context Watson assumes every transaction is a new conversation and the conversation_id is reset to a new value.

Building a Cisco Spark Bot with Watson

I recently built a Spark bot that started out using an array to track context but I quickly moved to MongoDB database (I wanted some persistence on restarts of node) . Seeing as Watson's context is already formatted in JSON it is easy to slip this straight into Mongo. Below is an example of some of the attributes I add to the context parameter before I send my message to Watson. Email, roomId, user and orderComplete (it is a pizza bot) are all created and added before the first message is sent to Watson. The combination of email and roomId from a Spark point of view makes every conversation unique as well as giving me some handy attributes. I, as a person can have multiple conversations with Watson in either a one-on-one room or multiple participant room so having a way to track that is important.

{ _id: '',
  email: '',
  roomId: '
  user: 'Christopher Norman',
  orderComplete: 'true',
  conversation_id: 'af4487c4-5543-4bf8-ab9c-f4611b3498bd',
   { dialog_stack: [ 'node_9_1475525825835' ],
     dialog_turn_counter: 9,
     dialog_request_counter: 9 },
  pizzaType: 'vegetarian',
  hackerNumber: 5 }

PizzaType and hackerNumber are appended during the conversation by Watson, which I will cover in just a bit. Obviously there are some other attributes Watson will add like what node it is up to in your conversation with node_9 as an example. This is its own tracking system and as long as you relay this back to Watson during your conversation it will keep your place in the dialog exchange. The exception to the this is a flat dialog structure where every node in the Watson dialog is a conversation entry point. In this case you don't care where the conversation is because the dialog has no depth. A FAQ bot where every question is the start and end and there is no follow up question as an example. I was going to say data collection as well but you can still make context updates even in this more simple dialog structure.

Below is a sample of what I used to build the context request using the Node.js Watson module. You may notice this is different than the example from the module. That is because the module example doesn't show you how to pass context through your conversation, this does. Watson's Node.js module is pretty useless without a way to collect and pass context. I worked this out by looking at this code, go look its worth the time. Stefania is an IBM dev evangelist, she has written some great examples over on Github that helped me a great deal. Just remember you have to save the returned context and retrieve what you have saved to send back a reply when your user responds to Watson. Watsons NLP engine will append entity information to context as you dictate but your still need to save it.

If you are interested in what I did to write my context to a MongoDB see my gist below. I am not going to cover it blow by blow but some might find it handy so I have included it for completeness. There is nothing to complicated in there as I replace the previous context Watson sends me rather than update the one in the database. There are quite few fields that require updating in every transaction so replacing context was just simpler than updating fields in the stored JSON record. If you prefer another database go for it this is just a quick solution I put it together in a couple of days. I didn't have weeks to explore the perfect database or why one option may be better than another or even put much thought into the data structure. I am just throwing JSON data chunks into the MongoDB and retrieving them later. I only mention this after reading this article while doing some research. It is not so much the article, which is interesting (although not sure I agree with), but some of the comments. The blogger should have mentioned to check your ego at the door before writing your comment. Is it just me or are people on the Internet snarky? lol.

So now we have covered how to build the context but what else can we do with it? Obviously I am going to use the classic pizza bot example, doesn't everyone. As you build out your dialog you can use Watson to pick out and update information in the context as well as create some more complex logic for dialog decision making. In the first dialog box even though you can not quite see it, I am checking the content of an attribute in my context using $orderComplete.contains('new'). This allows me to see if this person has ordered yet. If they had that same attribute "orderComplete" would have read true and I could have put them in a different thread.

In the first dialog box we ask the user what Pizza type they want. In the second box we are accepting their response, relaying it back and prompting for more info. But first we must record their response in our context. In our case it is the pizza selection using NLP we identify with entities(sorry I never covered this earlier. Now is the time to go read about intents and entities on IBM's Watson documentation site if you don't know WTF I am talking about).

Quick tip. If you need access to info in your context at any stage to respond to your user, using $ will get it for you. Below I added $user to grab the user name attribute from my context to add a personal touch to the interaction but this can be anything stored in the context.

Watson has a very interesting NLP engine that is actually pretty easy to work with once you get past the initial knowledge gap. Not to say there are not a load of other possible alternatives such as etc. or a node module that does NLP this is just one engine. If you have tried others feel free to comment about it. Always interested to check out new stuff especially if you have done NLP using a node module.

Thanks for reading.