In|Part|2 of our ChatOps blog series we made our bot reachable and have it notify us when it received a new message. Today we'll focus on how to obtain all details about those messages.
Webhooks only notify us about an event that happened (in this case receiving a message) butnot the detailsabout that event (in this case the content of the received message). If you want those details you need tomake a new call to the APIand explicitly ask about the details for that specific event. In this case for that new call you will need themessage idso you can use it to retrieve all details about that specific message. Let's see how you can obtain the text of the received message.
Before working on the new code please go to the terminal window where you were running your python code (the bot logic) and interrupt it with Ctrl+C. In that same window we'll run the new code so no need to close it.
Please review the python code required to extract the text of the received message. Please use your favorite editor or IDE to create a new file in your computer with the same content and call it "2_chatbot.py".
As you'll see from the content of the file it does the following on top of what the previous one did:
Before running the code please make sure to edit the file and assign your bot token to the api_key variable.
Go back to your bot logic terminal window and run the code with:
python 2_chatbot.py
You'll notice the terminal window displays something similar to what appeared last time. Leave it there and test it the same way as before, by sending a message in Webex to your bot. When you do it you will see the content of the message showing up in the terminal window.
OK, so what's happening? Well, you are using your Webex interface to send a message to a bot that resides in the Webex cloud and that bot is then notifying the code running in your laptop (the bot logic) about the new received message. Your bot code is then asking the Webex cloud API for further details about that specific message, extracting the text of the message from the received response and displaying it in your terminal. Cool, huh?
The next step would be to have our bot answer our messages, so let's start with the basics. We will program it to answer with the content of the message we sent it. When we send it a "Hello dear bot!" message, we'd like it to answer us with a "You said: Hello dear bot!".
We should examine the API format tosend a message. You will notice that, in order to be able to send a message, our bot needs to specify aroom id. To answer us, it should use the id of the room where it received our initial message. Thatroom idcan be found in the data received from the webhook notification, so we'll extract it from there.
Then we build our answer message and the JSON dictionary with the two required parameters: room_id and answer message. With that and Webex REST API URL it's time to use the requests library to send a POST message to the specified URL, with the defined headers and the data about the room_id and answer message.
All of this is implemented here, please review the code and use your favorite editor or IDE to create a new file in your computer with the same content and call it "3_chatbot.py".
Go back to your bot logic terminal window, stop the previous execution with Ctrl+C, and run the code with:
python 3_chatbot.py
As usual, please leave that terminal window there and test it the same way as before, by sending a message in Webex to your bot (something like -hello dear bot). When you do it you will see the bot answering with the content of your message (something like -You said: "hello dear bot").
Success!Or not...? Take a look at what happens next:
You said: "hello dear bot"You said: "You said: "hello dear bot""You said: "You said: "You said: "hello dear bot"""You said: "You said: "You said: "You said: "hello dear bot""""You said: "You said: "You said: "You said: "You said: "hello dear bot"""""You said: "You said: "You said: "You said: "You said: "You said: "hello dear bot""""""You said: "You said: "You said: "You said: "You said: "You said: "You said: "hello dear bot"""""""You said: "You said: "You said: "You said: "You said: "You said: "You said: "You said: "hello dear bot""""""""You said: "You said: "You said: "You said: "You said: "You said: "You said: "You said: "You said: "hello dear bot"""""""""......(ad infinitum)
Before anything else let's save your bot (it deserves to befreedfrom this): go to your terminal window and stop the python script (bot logic) execution with Ctrl+C. You'll see the loop stops.
Now that your bot issafelet's examine what happened. Looking at the messages it seems like everytime the bot sent you a message it was coming back to it and being processed again, causing the infinite loop.
The problem comes from the fact that the webhook we configured notifies the bot logic aboutanynew message in a room where it is a member. The moment the bot posts a new message that will also trigger a webhook notification to itself, with the subsequent processing.
So we need to find a way for the bot logic to ignore its own messages. The best way to do it is to take a look at who is the sender for any new message, and filter out the ones where the sender is the bot itself.
Your bot can find information about itself using this API call to extract its own id. With that info, you can filter all webhook notifications that come from messages posted by the bot itself, using a simple if-else statement.
You may check the code with this fix implemented. Please use your favorite editor or IDE to create a new file in your computer with the same content and call it "4_chatbot.py".
Go back to your bot logic terminal window, stop the previous execution with Ctrl+C, and run the code with:
python 4_chatbot.py
The loop is fixed...now it works perfectly!
In a couple of weeks we will explore how we can integrate our bot with other APIs to do something useful. See you then!
If you'd like to learn more about network programmability and Cisco networking platforms visit the DevNet Networking Dev Center.
Any questions or comments please let me know in the comments section below, Twitter or LinkedIn.
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