Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Building a Chat Integration for Cisco Spark in Minutes

DZone's Guide to

Building a Chat Integration for Cisco Spark in Minutes

What it says on the tin. Learn how to build an integration for Cisco Spark (and PagerDuty) with this easy tutorial.

· Integration Zone
Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

So shortly after Cisco Live, we thought it would be timely to build an integration for Cisco Spark and PagerDuty. So we did — and it only took a few minutes, and we were able to easily embed custom rules and functionality. We did this via the PagerDuty Custom Event Transformer — a very cool feature we launched a few weeks ago that enables you to convert a payload sent by any tool that can send an HTTP request into one understood by PagerDuty. In other words, you can integrate virtually anything with PagerDuty.

This Spark-PagerDuty integration is just one proof point out of many more similar examples to come, that we can easily integrate with any ChatOps (or other) tool to optimize any workflow and extend functionality in any way a customer may desire.

Like many tools, Cisco Spark works great with PagerDuty out of the box, and you can send PagerDuty incidents into your Spark chat rooms. Today we're going to build an integration that enables the reverse — it will enable you to trigger PagerDuty incidents from within Spark.

I'm going to do a few things differently from our other native Slack->PD & Hipchat->PD integrations to show off the fact that, with the Custom Event Transformer, you can very easily structure integrations to enable custom functionality, and the possibilities are endless.

My new Cisco Spark-PagerDuty integration will have the following features:

  • This integration will have a dedicated room, any requests for help in that room will trigger PD (instead of filtering for slash commands).
  • All the chat messages in that room will get rolled into the same PD incident (by using the room as the incident_key).
  • That incident can be closed manually from PD or by saying, "Thanks."

There are a few things specific to Cisco Spark webhooks that I'll convert:

  • All URLs are base64 encoded.
  • Because I'm using the web client, I’ll change URLs from "ciscospark://us/ROOM/" to "https://web.ciscospark.com/#/rooms/."

Let's see how that all comes together. To try this yourself, create a Custom Event Transformer with the following code:

function transform(PD) {
    var webhook = PD.inputRequest.body
    var message = webhook.text || "Error: Not a Spark chat message";
    var room_link = ciscospark2web(b64decode(webhook.roomId)); // clean up the link
    var normalized_event = {
        incident_key: room_link, //So that all messages from a room de_dupee into the same incident
        event_type: PD.Trigger,
        description: message,
        details: {
            email: webhook.personEmail
        },
        client: "Cisco Spark",
        client_url: room_link
    };

    // If the message is "thanks" => automatically close the incident
    if (message.toLowerCase() == "thanks") {
        normalized_event.event_type = PD.Resolve;
    }

    // Create the PD event
    PD.emitEvents([normalized_event])
}

// Helper functions:
function b64decode(b) {
    return new Buffer(b || "", "base64").toString("utf8");
}

function ciscospark2web(url) {
    // from: ciscospark://us/ROOM/31ce2d70-3f09-11e6-946f-11a974144a8d
    // to: https://web.ciscospark.com/#/rooms/31ce2d70-3f09-11e6-946f-11a974144a8d
    return url.replace("ciscospark://us/ROOM/", "https://web.ciscospark.com/#/rooms/")
    // TODO: Links to 1-1 chats & direct to messages
}


Hit save, and your integration will look like this:

Image title


Plugging it into Cisco Spark

Once you've created your Custom Event Transformer in PagerDuty, point a Cisco Spark "Outbound webhook" at the:

Image title

Image title


How does it look in action?

Here are a few chat messages in Spark:

Image title


And in PagerDuty, notice that the second message is appended to the log, just like I had wanted!

Image title

And if the requester says, "Thanks," it'll automatically close in PagerDuty.

To sum it up, our Spark-PagerDuty integration was easily configured as an outbound webhook in a couple of clicks, and it adhered to all the custom rules and functionality that I defined in my code.

Tips for Writing Your Own Custom Event Transformer

Again, the best part of all this is that you can do this too, and for any use case.

I typically use Runscope API Traffic Inspector to proxy all my webhooks around because it lets me see, modify, and re-send them. So during development, I'll point tools at https://events-pagerduty-com-aaaabbbbcccc.runscope.net/integration/aaaabbbbcccc111222333/enqueue. You can see one of the Spark events I tested with here.

Learn more about Custom Event Transformers and get started with creating your own to integrate anything with PagerDuty. And for those of you in the Bay Area on Sept. 13, be sure to stop by and visit us at our very own PagerDuty Summit, where leaders from Google, Linkedin, IBM, and Yahoo will share their key learnings for building a modern operations environment.

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

Topics:
chat ,cisco ,integration ,spark ,embed ,pagerduty

Published at DZone with permission of David Hayes. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}