Using Libraries in Integrations

Using Libraries in Integrations


As someone who works in tech, you're likely familiar with the benefits of automation and integrations. However, have you ever considered the role that libraries could play in making your life easier when writing integrations? This article will explore the importance of using libraries in integration development with a practical example.

In our example, we'll discuss a small library that makes API calls to PagerDuty for a Slack bot. This library comprises one file that allows you to save the API key in a more secure place while also letting you use the PagerDuty integration library in different Slack bots.

Using this library means you don't have to worry about repeatedly writing the same integration code. You can focus on the actual logic of the integration instead of updating API calls or keys in five different files. This idea isn't new and is commonly called Abstraction in Object Oriented Programming. Furthermore, libraries save you time and resources, as you don't have to start from scratch every time you develop an integration.

In addition to saving time and resources, using libraries can help you ensure that your integrations are consistent and reliable. Using a library already developed and tested, you can be confident that your integration will work as expected. This can be particularly important if you are developing integrations for a business or enterprise, as consistency and reliability are crucial.

Here's a practical look at the PagerDuty library I built for Google Script:

function Duty() {
  this.token = 'sjdflkas94lkjd'

  this.get = function(url, params) {
    var options = {
        'contentType': 'application/json',
        'headers': {"Authorization": "Token token=" + this.token,
                    "Accept": "application/vnd.pagerduty+json;version=2",
                    "Content-Type" : "application/json"
                    },
        'muteHttpExceptions': true
      };


      var res = UrlFetchApp.fetch('https://api.pagerduty.com' + url + this.params(params), options);

      if(res.getContentText().length === 0){
        return null
      } 

      var data = JSON.parse(res.getContentText())
  
      return data
  }

  //Helper function
  this.params = function(data) {
    const params = [];
    for (var d in data)
        params.push(encodeURIComponent(d) + '=' + encodeURIComponent(data[d]));
    return '?' + params.join('&');
  }

  this.getOnCall = function(schedule){

    var now = new Date()
    var almostNow = new Date(now.setMinutes(now.getMinutes()+5))

    var params = {
      'time_zone': 'CET',
      'since': now.toISOString(),
      'until':almostNow.toISOString(),
      'earliest':true,
      'include[]':'users',
      'schedule_ids[]': schedule
    }

    return this.get('/oncalls', params)
  }

  
 this.listSchedules = function(){
    var params = {

    }
    var res = this.get('/schedules', params)

    Logger.log(res)
  }


}

//Use this to get the scheudle ID's from PagerDuty
function test(){
  var p = new Duty()
  var res = p.listSchedules()
}        

Now the integration gets whoever is on duty and posts it to a channel. This also uses my SlackBot library. Combining these two libraries makes the integration code easier to follow and update.

const SP_USER_ON_CALL = "USER_ON_CALL"

function checkWhoIsOnDuty() {
  //Get existing user
  var scriptProps = PropertiesService.getScriptProperties()
  var sp_user = scriptProps.getProperty(SP_USER_ON_CALL)


  //Check for new user
  var pager = new Pager.Duty() 
  var res = pager.getOnCall('ScheduleID')

  if(res.oncalls.length < 1){
    return
  }

  var onCall = res.oncalls[0]
  var pg_user = onCall.user

  Logger.log(pg_user)

  //Duty has not changed, exit script
  if(pg_user.email == sp_user){
    Logger.log(sp_user + " still on duty")
    return
  }

  //Send message if change
  var slack = new Slack.Bot()
  var slack_id = slack.getSlack(pg_user.email)

  var endTime = Utilities.formatDate(new Date(onCall.end), "CET", "MM/dd/yyyy HH:mm a")

  slack.send("<@" + slack_id + "> is on pager duty till " + endTime + ":pager:", "channelID")

  //Update property
  scriptProps.setProperty(SP_USER_ON_CALL, pg_user.email)
}        

To view or add a comment, sign in

More articles by Peter Flickinger

  • AI Categorization of Hubspot Tickets

    🧠 Using AI to Understand Our Team’s Work at Scale When we started the Technical Account Management (TAM) team, we had…

  • Building a Better AI Support Helper (OpenAI + Intercom)

    Building a Better AI Support Helper with OpenAI + Intercom Ever been in that situation where you're juggling 15 support…

    2 Comments
  • What is SFTP?

    SFTP is a way computers share files with one another. When you hear that Pinpoint will send reports to your HRIS via…

    2 Comments
  • The 5 Parts of an API Call

    In the ever-more-complicated Software as a Service (SaaS) landscape, comprehending the anatomy of API calls is…

  • What is an API?

    Unlocking the Power of APIs in the SaaS World In the Software as a Service (SaaS) realm, where seamless interactions…

    2 Comments
  • SMS Reminders for Google Calendar

    The Situation I help make appointments for a community organization that I store in Google Calendar, but I want to send…

  • The Best Way to Get Survey Responses

    This week I'm collecting feedback on our learning hub. The first step to redesigning our new courses is figuring out…

  • Dealing with Salesforce Rotating Tokens

    As a customer success team, keeping track of all the information related to our accounts can be challenging. From…

  • Add a Course to Multiple Hubs in My Learning Hub

    As I work with our Learning and Training department, my goal is to provide our customers with personalized learning…

Explore content categories