With all the big WebEx news – including a new logo – I wanted to revisit the basic #chatbots I have working using pyATS, Python requests, and the WebEx API after the conversation came up in the #pyATS WebEx Community space today:
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-70.png)
First, let’s take a look at what this does, and this is not limited to Merlin; any pyATS job has this capability
If you create the pyats.conf file as Takashi suggests and add the [webex] information it will enable the pyATS job to report the job summary into the WebEx space you provide the config file.
This looks something like this inside of WebEx:
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-71-1024x493.png)
This in itself is pretty handy! And all you need to do is go to the Cisco WebEx for Developers portal and either make a Bot under My Apps
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-72.png)
Or, right from the browser, grab one of the 12 hour tokens
The easiest way to get one of these is to go to the Documentation
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-73.png)
Find the API Reference
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-74.png)
Find Messages
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-75.png)
Pick POST
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-76-1024x601.png)
COPY THIS BEARER TOKEN
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-77-1024x581.png)
Paste that into your pyats.conf
But how do I get the Room / Channel / Space ID?
If you browse to Rooms
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-78.png)
You can GET your current Room list
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-79-1024x678.png)
This will give you the JSON list – here is the Merlin Room ID
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-80.png)
Thats it you are ready to connect your pyATS jobs for a job summary as a WebEx message!
Adding Network State Data
With the above foundational WebEx integration with pyATS and WebEx’s simplicity I thought I would integrate a few sample commands into a Merlin pyATS job for the community to see how you can send Network State data to WebEx!
I want the message to be in Markdown so I am going to use a Jinja2 template to craft the JSON we can POST with Python requests after pyATS has parsed or learned the function
We don’t need a lot to make this happen either here is everything I import
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-81.png)
- Update – I’ve also come to discover we need 1 more import and pip install requests_toolbelt in order to attach files to WebEx messages
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-92.png)
We setup our WebEx room and token (12 hour or bot) as variables we can call later
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-93.png)
The general_functionalities are important these are object oriented code that gets reused per pyATS learn or parse library call.
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-82.png)
Then for this example I will do 2 learn functions, platform and routing and see if I can transform real network state data into meaningful WebEx messages
I tell Python where to find the Jinja2 templates and setup a variable I can use later to load said templates
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-83.png)
We then setup our pyATS framework and connect (testbed.connect) to our topology
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-84.png)
Again the testbed file looks like this
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-85.png)
Now that we have connected we can begin our Test Steps ultimately looping (for) over each device in our topology (testbed)
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-86.png)
Yes in this Sandbox there is only 1 device but this could scale to X devices. Just add them to the testbed.
Now we can learn platform
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-89.png)
As of right now we have the following JavaScript Object Notation (JSON) data inside the self.learned_platform variable
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-96.png)
Our goals:
- Send a log of our pyATS Merlin job to a WebEx Room or Individual
- Send this data as a human friendly message
- Create an XLSX spreadsheet we can attach to our message
Now we start our test steps
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-90.png)
We will get a boolean pass/fail from the Create CSV and Sent to WebEx WebEx step
Next I set up a few variables – namely the Jinja2 references, the directory for the XLSX file, and the file name.
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-91.png)
Also – for attachments we will declare another variable, the MultipartEncoder with the information required to attach the Learned_Platform.csv file
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-94-1024x56.png)
Next we template the .xlsx file from the Jinja2 template
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-95.png)
Which looks like this:
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-98-1024x74.png)
That renders the file that looks like this
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-97-1024x43.png)
We will use 2 more Jinaj2 templates for the actual message we will send. Because The JSON body we post to WebEx is a single line, and in Markdown a header row starts with a # symbol, to avoid making the whole thing a header we will send it first.
Here is the line in Python
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-99.png)
And the matching Jinja2 template
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-100.png)
Remember, we are sending a long single line / string, as markdown, so if we want multi-line we need to add <br/> the Markdown linebreak command
Here is how we send the header
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-101-1024x58.png)
Which looks like this in WebEx:
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-102.png)
Now let’s go ahead and template the Markdown
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-103.png)
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-104-1024x83.png)
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-105-1024x59.png)
Which looks like:
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-106.png)
Important! I had to “trim” this from what is in the “full” Markdown as there *is* a character limit so watch for that!
But that is also why we can attach the full CSV
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-107-1024x58.png)
![](https://www.automateyournetwork.ca/wp-content/uploads/2021/06/image-108.png)
So go get #chatbotting using real network state data!
Reach out if you hit any snags and watch for the full development video!