Creating Twitter & PagerDuty integration

In this age of extensive social networking, it is essential for organizations to monitor customer sentiment via solutions that monitor platforms such as twitter. This document provides guidance on how to create integration between a tweet analysis solution, and PagerDuty.

Use case:
Allow PagerDuty to create an incident whenever a tweet contains a predefined keyword and in the PagerDuty Infrastructure Health Console place a "T” marker on the graph.

Prereqs:

These procedures assumes you have:

  1. A PagerDuty account with admin authority
  2. A twitter account with developer access (needed in order to get required API tokens)
  3. A small server (virtual or otherwise) with python 2.7 or higher installed

PART 1: PAGERDUTY SETUP:

  1. Select Configuration/Services then create/modify a service you want to receive the tweet notification on. Select the integration tab the click “new integration”
  2. Name your integration “Twitter Custom Event Transformer”, and select from the pulldown menu “custom event transformer”, then choose an existing escalation policy. Since this service is meant to be automatic you may wish to create a “no ops” escalation policy so no one gets notified. You may elect to have the incidents auto resolve after 10 minutes. In our Java script below we also set the severity to “info” so that the alert gets tagged as a “low priority” notification. More information on Custom Event Transformer can be found here: https://developer.pagerduty.com/docs/cet
  3. Select “edit integration” to modify the provided default java script provided. Replace the existing js example with the following code (cut and paste the following segment):
var body = PD.inputRequest.body;
var twitter_description = String(PD.inputRequest.body.description);
var cef_event_deploy = {
    event_type: 'cef',
    event_action: PD.Trigger,
    summary: twitter_description,
    description: twitter_description,
    message: body.description,
    client_url: 'https://twitter.com/',
    client: 'Twitter',
    source_origin: 'Twitter via Tweepy API',
    severity: 'info',
    event_class: 'tweet'
};
PD.emitCEFEvents([cef_event_deploy]);
  1. Save your changes and copy to your clipboard the integration key.

PART 2: TWEEPY SETUP:

  1. We have tested this approach on two types of servers. One was a local CentOS VM running off of a Macbook pro via the VirtualBox. The other was an AMI instance off of AWS running Ubuntu. You can select whichever system is more appropriate for you.
  2. Ensure you have a python environment available in order to execute the code below
  3. Download and install the PagerDuty agent. The PagerDuty agent is needed to send the information over to pagerduty via the ‘pd-send’ command line utility. Details and Instructions for installing the pdagent API are located here: https://www.pagerduty.com/docs/guides/agent-install-guide/
  4. Download and install the tweepy API.
    Here are instructions for getting it for a CentOS distro: https://www.mikesel.info/install-tweepy-centos-6-6/
  5. Modify the tweepy code lines 11,12,13,14 to use the twitter API tokens from your twitter developer account. Also change the “YOUR_PD_API_Key” in the code below with your key captured from Part 1, step 4 above. Notice line 15 “keywords” contains the twitter “string” to listen for (ie. “pagerduty”). Make your modifications and save the file as tweepypd.py
  6. We now need to start a terminal session and run your modified script. At the command line type: python ./tweepypd.py
#!/usr/bin/python

# Import the necessary package to process data in JSON format
try:
import json
except ImportError:
  import simplejson as json

# Import the tweepy library
import tweepy
import os

# Put keywords that you want to filter into an array.
keywords = ['pagerduty']

# Variables that contains the user credentials to access Twitter API
ACCESS_TOKEN = '<YOUR_TWITTER_API_DEV_TOKENS>'
ACCESS_SECRET = '<YOUR_TWITTER_API_DEV_TOKENS>'
CONSUMER_KEY = '<YOUR_TWITTER_API_DEV_TOKENS>'
CONSUMER_SECRET = '<YOUR_TWITTER_API_DEV_TOKENS>'

# Setup tweepy to authenticate with Twitter credentials:
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)

# Create the api to connect to twitter with your credentials
api = tweepy.API(auth, wait_on_rate_limit = True, wait_on_rate_limit_notify = True, compression = True)
class StreamListener(tweepy.StreamListener):
  def on_status(self, status):
  status_var = "\"" + status.text + "\""
os.system('pd-send -k <YOUR_PD_API_KEY> -t trigger -d \""' + status_var.encode("utf-8") + '\""')
def on_error(self, status_code):
  if status_code == 420:
  return False
stream_listener = StreamListener()
stream = tweepy.Stream(auth = api.auth, listener = stream_listener)
stream.filter(track = keywords)

PART 3: GENERATING THE INCIDENT

  1. You can wait for a PagerDuty incident to be created when a tweet containing your “keyword” shows up or you can login to your twitter account and post a tweet with the “keyword” that you would like to trigger on (ie. “pagerduty” in our example).

  2. The PagerDuty twitter service will receive and surface an incident. It will also create a “t” marker in the visibility infrastructure health console, indicating a tweet with the “keyword” was
    captured.

Conclusion:

Disruptive organizations are leveraging sentiment analysis and can use PagerDuty to quickly recover from negative tweets, ensuring customer loyalty and helping reduce churn risk.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.