Avatar

Two weeks ago we had our bot extract all message details and answer back to us. Today we’ll focus on how we can integrate it with a different API.

Let’s review what we have learned until now in this blog series on ChatOps:

  • How to configure a webhook to have Webex notify our bot logic about new messages
  • How to retrieve all details about a new message, including its text content
  • How to answer back to the user
  • How to ignore our bot’s messages and avoid processing them

We’ve gone a long way, but now we need to make our bot useful. Let’s explore how we can make it perform a specific task upon receiving a message from us.

 

Chuck Norris jokes and other useful APIs

Who doesn’t like Chuck Norris? And who doesn’t like jokes? We definitely need our bot to tell us Chuck Norris jokes! Luckily enough there is an API for that (what…?!?) and we are going to use it in our bot logic.

ChatOps

On top of what we had already implemented we’ll add an if-else statement to check if the received message is a /chuck command:

  • If it is a /chuck command we will send an HTTP GET to the Chuck Norris jokes API asking for a new random joke
  • Else we will do the same as before, and echo the received message

As simple as that. Please check the code with this new feature implemented here and use your favorite editor or IDE to create a new file in your computer with the same content and call it “5_chatbot.py”.

Go back to your bot logic terminal window, stop the previous execution with Ctrl+C, and run the code with:

python 5_chatbot.py

Working fine!

Now let’s add a parameter to our message that defines what specific jokes we are interested in. You can check the complete list of categories here, but for this exercise we can use just a couple of them: sport and travel. This way we can ask our bot for jokes on a specific topic just by typing /chuck sport or /chuck travel.

In order to implement this new feature we just need to use the split method to extract the parameter after /chuck. That parameter will be in the second position (number 1, as the first position is number 0) and we will use it as a parameter for the API URL where we’ll send the HTTP GET.

Easy peasy!

Please check the code with this new feature implemented here. You should use your favorite editor or IDE to create a new file in your computer with the same content and call it “6_chatbot.py”.

Go back to your bot logic terminal window, stop the previous execution with Ctrl+C, and run the code with:

python 6_chatbot.py

As long as it’s really difficult to refrain yourself from spending 3 hours enjoying Chuck Norris jokes, we will need to ask our bot to use something different and useful… how about the Meraki API?

Integration with Meraki API

Let’s say we want to create a chatbot that tells users about the Meraki networks they manage. If you are familiar with Meraki, probably you know it has a certain hierarchy where your user belongs to a certain organization. And, that organization manages a number of networks. So if you want to list your networks first you need to know what organization you belong to.

Integration with Meraki API Let's say we want to create a chatbot that tells users about the Meraki networks they manage. If you are familiar with Meraki probably you know it has a certain hierarchy where your user belongs to a certain organization, and that organization manages a number of networks. So if you want to list your networks first you need to know what organization you belong to.

Before reviewing the code you might remember I mentioned previously it is not recommended to include your API key in the code. Now that we are about to start working with the Meraki API let’s explore how to do things in a different way:

  1. Instead of including the Meraki API key in our code we’ll do it properly and define it as an environment variable
  2. Instead of using native calls to the API, we’ll try the meraki library and see how it simplifies management

For the first one about the API key you will have to enable the API from your Meraki dashboard. You just need to login and go to “Organization” – “Settings” – “Dashboard API access” and click on “Enable access to the Cisco Meraki Dashboard API”. Once done, go to your profile and “Generate new API key”. Same as with the bot key, please write it down (copy/paste) somewhere safe, as once you close this window you won’t be able to recover it.

With that now you need to create the required environment variable in the same terminal window from where you will run your python code (bot logic) later:

export MERAKI_DASHBOARD_API_KEY=<your_meraki_api_key>

Nice!

Time to take a look at the code, this is what you’ll see on top of what it was doing previously (I’ve removed the Chuck Norris jokes code for the sake of clarity):

  • Import the meraki library
  • Instantiate the dashboard API with your API key
  • Define what to do if the command is /meraki
  • Define what to do if the command action is networks (so the overall message to send the bot is /meraki networks)
    • Extract the organization id (first position in the list, numbered with 0)
    • Extract a list of all networks under that organization id
    • Build the answer message the bot will send to the user with info about the networks

Please check the code with this new feature implemented here and use your favorite editor or IDE to create a new file in your computer with the same content and call it “7_chatbot.py”.

Go to your bot logic terminal window, where you defined the Meraki API key environment variable, and run the code with:

python 7_chatbot.py

Test it by sending your bot a message like this: /meraki networks, and you will see it answers with the complete list of networks that belong to your organization.

Superb!

Now we can implement an additional command action that displays the SSIDs available for our first network, by adding an elif statement for the new ssids action. The code included there will extract the id of the first listed network (position 0) and use it to get the complete list of SSIDs under that network. With that it will build the formatted answer message to display the required info. And while we’re at it, we’ll make this ssids action the default one in case you just send the bot a message with /meraki.

Please check the code with this new feature implemented here and use your favorite editor or IDE to create a new file in your computer with the same content and call it “8_chatbot.py”.

Go to your bot logic terminal window, where you defined the Meraki API key environment variable, stop the running code for the previous version with Ctrl+C, and run the new code with:

python 8_chatbot.py

Test it by sending your bot a message like this: /meraki ssids, and you will see it answers with the complete list of SSIDs that belong to the first network in your organization. You can also test that you get the same result when typing just /meraki, as we have defined the ssids action as the default one if nothing is specified.

Well done!

As you can see this is an easy way to implement different actions based on pre-defined commands that let you interact easily with your bot.

Nice job, really well done! You are now ready to explore your own integrations with any other APIs out there, so you can accomplish any specific goals that address your own real business challenges.

Hope you enjoyed this mini-series! Any questions or comments please let me know in the comments section below, or via Twitter or LinkedIn.

If you’d like to learn more about network programmability and Cisco networking platforms visit the DevNet Networking Dev Center.

Additional resources

 


We’d love to hear what you think. Ask a question or leave a comment below.
And stay connected with Cisco DevNet on social!

Twitter @CiscoDevNet | Facebook | LinkedIn

Visit the new Developer Video Channel



Authors

Julio Gomez

Programmability Lead, EMEAR

Systems Engineers