As an Amazon Associate I earn from qualifying purchases from amazon.ca

ChatOps: The right way to Construct Your First Webex Bot


Welcome to the primary weblog of our sequence on learn how to use the Webex API to code up ChatOps workflows! On this submit, you’ll discover ways to create a Webex bot, register a Webhook in Webex, and configure your bot to take heed to Webhook – all with loads of code examples. Test again for extra as we construct new use circumstances that leverage totally different elements of automation utilizing chat-driven interfaces.

Within the DevOps world, we’re at all times in search of new methods to drive automation round communication. Once we deploy new code, scale our deployments, or handle our characteristic flags – we wish our groups to find out about it. The Webex API makes it simple to construct announcement flows triggered by profitable occasions in our infrastructure. Nevertheless, if we will set off these occasions from Webex as nicely, then we’ve entered the world of ChatOps.

ChatOps is using chat purchasers like Webex Groups, chatbots, and real-time communication instruments to facilitate how software program improvement and operation duties are communicated and executed. Utilizing Webex APIs, we will construct bots that permit us to enter instructions that handle our infrastructure, set off approval workflows, deploy code, and far more.

Safety Disclaimer

Safety is a high concern right here at Cisco. In regular software improvement, safety ought to at all times be constructed into the preliminary steps of getting code up and operating. As we speak, we’re going to maintain it easy and give attention to the fundamentals. Then, we’ll cowl learn how to authenticate and authorize Webhook requests. We’ll maintain off on safety till the following weblog submit in our ChatOps sequence, as soon as we’ve confirmed an end-to-end connection. 

The right way to create a Webex bot

First, let’s create a Webex bot utilizing the Webex Developer UI.

Webex for Builders has a fantastic step-by-step information right here that can assist you stand up and operating.

Some vital issues to think about:

  • Take into consideration what you wish to identify your bot. It must be intuitive, however distinctive. Relying on the way you arrange your Webhook, chances are you’ll be typing the bot’s identify so much, so take that into consideration.
  • The key token that’s auto-generated in your bot is used for authenticating with the Webex API. Once you use this token, Webex will deal with your bot like an actual person who can create messages, be part of rooms, or be tagged by different customers.
  • Will this bot work together with lots of people? Will it have a really public presence, or will it solely talk with just a few customers? The reply to that query might have an effect on the way you wish to identify it, what icon you choose, and many others.

When you’ve taken all of that into consideration and crammed out the bot creation type, it’s best to see one thing like this, which incorporates the all-important entry token:

Use your bot access token to set up your webhook with Simple Webhook Listener

The right way to obtain Webhook Occasions regionally

Subsequent, you’ll have to host your bot the place it may be accessed by Webex through API calls. For those who’re growing regionally and wish to run a server that’s accessible to the web, the Webex information recommends localtunnel.me or ngrok.  I went with localtunnel.me for my native atmosphere.

$ npm i -g localtunnel
$ lt --port 3000

The ensuing output is the general public area identify that you need to use to tunnel via to a neighborhood port in your machine:

public domain name output used to tunnel through to a local port on laptop

Be aware: For those who’re having bother operating localtunnel through the command line after putting in (as just a few folks have reported right here), be certain that your PATH consists of the listing the place NPM installs your binaries. For instance, on a Mac, that’s /usr/native/bin. This command would possibly assist:

$ npm config set prefix /usr/native
$ npm i -g localtunnel
$ lt --port 3000

The right way to register a Webhook

As soon as your internet-accessible endpoint has been arrange, you now have a site that you need to use to register a Webex Webhook. Your Webex Webhook will take heed to particular occasions that happen inside the Webex platform and notify your internet service through HTTP POST requests.

There are a number of methods to register a webhook. Underneath the hood, nonetheless, all of them boil down to creating your individual HTTP POST request. I’ve posted a Postman assortment that you need to use to make this course of a little bit simpler. Fill in your individual atmosphere’s variables as you go and embrace the entry token used within the header.

That is what my Postman request seems like:

Create Webhook Postman Request to build a Webex bot

Be at liberty to make use of no matter know-how you want, together with good old-fashion CURL:

curl --location --request POST 'https://webexapis.com/v1/webhooks' 
--header 'Authorization: Bearer $BOT_TOKEN 
--header 'Content material-Sort: software/json' 
--data-raw '{
    "identify": "simple-webhook",
    "targetUrl": "https://tidy-falcon-64.loca.lt",
    "useful resource": "messages",
    "occasion": "created",
    "filter": "mentionedPeople=me"
}'

What’s vital to notice, is that Webex will ship notifications to the area that you just specify in your POST request. For those who’re utilizing a tunnel into your native atmosphere, record the area that was given to you whenever you activated your proxy.

A really impactful a part of your Webhook would be the filter property. This determines which Webex occasions are despatched to your bot as notifications (and that are filtered out). To maintain issues easy, my bot is barely notified when customers ship a message that particularly mentions it in a Webex Groups Room:

Building a Webex Bot Filter property for message notications

Webex has a pleasant, handy tag for this: me makes use of the authorization token from the request to find out the identification of the person making that request (on this case, our bot), and applies that identification wherever it sees me referenced.

Alternatively, you may set a filter that solely triggers notifications for direct messages to your bot, versus mentions in Webex rooms. Because the aim of this submit is to broaden visibility into the assorted processes, these examples present interactions in a Webex Groups Room, nonetheless, each are equally viable choices.

Once you ship your POST request, Webex will reply with a physique that incorporates an ID in your Webhook. Whereas you need to use the Webex API to GET an inventory of your Webhooks, it is likely to be a good suggestion to carry onto this, in case you wish to rapidly replace or delete this Webhook sooner or later.  The Postman assortment linked above shops probably the most just lately created Webhook ID in an active_webhook atmosphere variable mechanically, which then powers the DELETE name in that assortment.

Postman creates Webhook ID to power DELETE call

The right way to create your bot server

For easy use circumstances, chances are you’ll wish to use the Webex Node Bot Framework, which is nice for fast implementation. In an effort to get extra acquainted with the totally different elements concerned on this sequence, we’ll begin from scratch, diving into the step that powers your Webex bot.

Getting Began with Categorical

Let’s arrange an internet server that may pay attention for POST requests from the Webex Webhook that we’ll create in a minute. This doesn’t must be sophisticated for now, simply one thing to exhibit that we’re in a position to obtain requests. For simplicity, we will use the ExpressJS generator, however you need to use any internet framework or know-how that you just like.

$ npm i -g express-generator
$ cd the place/you/need/your/undertaking
$ specific

Since my IDE handles JavaScript Modules so much higher than it handles require statements, I opted to go along with a extra trendy method for my dependency administration. That is completely optionally available and has no bearing on the way you arrange your code. Nevertheless, if you wish to observe the code snippets as I’ve laid them out, you’ll wish to do the identical. Step one is so as to add the next key/worth pair to your package deal.json file, wherever within the root of the JSON object:

"sort": "module",

Lots of the boilerplate code could be stripped out if you happen to like – we received’t want a favicon, a public/ folder, or a customers route handler. Right here’s what my code regarded like after I stripped plenty of the straightforward stuff out:

// in app.js

// discover that I modified the require statements to make use of JS modules import statements
import specific from 'specific';
import logger from 'morgan';
import indexRouter from './routes/index.js';

const app = specific();
app.use(logger('dev'));
app.use(specific.json());
app.use(specific.urlencoded({ prolonged: false }));

app.use('/', indexRouter);

// boilerplate error code didn’t change
// …

// **remember to keep in mind to set app because the default export on the finish of the file**
export default app;

Since I’m utilizing JS Modules, I additionally needed to change the executed file in an Categorical app www/bin to www/bin.js, and revise the boilerplate require statements there as nicely to make use of import syntax:

// in www/bin.js

/**
* Module dependencies.
*/

import app from '../app.js';
import _debugger from 'debug';
const debug = _debugger('chatops-webhook:server');
import http from 'http';

// nothing else on this file wanted to vary

Including a Route Handler

That takes care of the vast majority of the boilerplate. At this level, I solely have 4 recordsdata in my codebase, regardless of what number of Categorical provides me out of the field:

  • app.js
  • package deal.json
  • bin/www.js
  • routes/index.js

We’ll wish to add a route handler that lets us know once we’ve obtained a POST request from our Webex Webhook. It may be a easy operate that prints the request physique to the appliance console – nothing sophisticated, only a few traces of code:

// in routes/index.js

import specific from 'specific'

const router = specific.Router();

router.submit('/', async operate(req, res) {
  console.log(`Obtained a POST`, req.physique);
  res.statusCode = 201;
  res.finish();
});

export default router;

Give it a attempt

You now have all the vital elements for receiving message notifications from Webex:

  • A bot to behave as an identification in your Webex interactions
  • If relevant, a community tunnel to reveal your native internet service to the general public web
  • A Webhook arrange by your bot to obtain Webex notifications
  • An online service to obtain Webex notifications on a POST endpoint

Let’s try it out!  To maintain issues easy for now, create a brand new room in Webex Groups and add your bot as a member. Subsequent, begin typing your message, mentioning your Bot (you need to use the @ image or sort its identify) as a part of the textual content. Once you hit enter, after a quick pause, it’s best to see a request come via to your operating internet service, which ought to log the POST physique that it obtained in its console output:

running web service request for webex bot POST body console output

Congratulations, you’ve simply arrange your very personal Webex bot!

 What’s subsequent

As promised, our subsequent submit will stroll via the extraordinarily vital facet of securing our bot. We’ll make it possible for solely Webex can entry it and solely approved customers can set off automation. After that, we’ll transfer on to new and thrilling methods which you can automate on a regular basis workflows proper from a Webex Groups Room!

Study, prepare, and certify in Cisco Collaboration

As you make your means via this ChatOps sequence, contemplate validating your abilities with a Cisco Certification.

The 300-835 CLAUTO: Automating and Programming Cisco Collaboration Options is a 90-minute examination that counts towards three certifications — the CCNP Collaboration, Cisco Licensed DevNet Skilled, and Cisco Licensed DevNet Specialist – Collaboration Automation and Programmability certifications. Take a look at the CLAUTO examination matters, and also you’ll discover that 25% of the examination covers Cloud Collaboration applied sciences. Earlier than we meet once more, take a while to flick thru the free CLAUTO Examine Supplies out there on the Cisco Studying Community, which is able to enable you to solidify at the moment’s ChatOps give attention to constructing your first Webex bot.

Did you construct a Webex bot? I’d love to listen to the way it went. Go away me a remark beneath and let me know what you suppose!

Comply with Cisco Studying & Certifications

Twitter, Fb, LinkedIn and Instagram.

Share:



We will be happy to hear your thoughts

Leave a reply

flyviolette
Logo
Enable registration in settings - general
Compare items
  • Total (0)
Compare
0
Shopping cart