Question

module.exports = (robot) ->
  robot.respond /log (.*)/i, (msg) ->
    group = "test"
    incident_message = msg.match[0]
    service_key = keys[group]
    curtime = new Date().getTime()
    incident_key = "hubot/#{curtime}"
    reporter = msg.message.user.name
    query = {
      "service_key": service_key,
      "incident_key": incident_key,
      "event_type": "trigger",
      "description": "Change Log #{reporter}",
      "details": "#{incident_message}"
    }
    string_query = JSON.stringify(query)
    content_length = string_query.length
    msg
      .http("https://events.pagerduty.com/generic/2010-04-15/create_event.json")
      .headers
      "Content-type": "application/json",
      "Content-length": content_length
      .post(string_query) (err, res, body) ->
      result = JSON.parse(body)
      if result.status == "success"
        msg.send "Your log has been sent"
      else
        msg.send "There was an error sending your log."
    msg
      .http("https://xyz.pagerduty.com/api/v1/incidents/#{incident_key}/acknowledge")

I am trying to auto acknowledge an event triggered in pagerduty. The first http request takes effect. But the second http request (last line of the code) never takes effect. I have tried varipus combinations. But it does not help. I am new to coffeescript and only use it for hubot.Can anyone help me ?

Was it helpful?

Solution

First, you don't specify what HTTP action you are doing:

msg.http("https://xyz.pagerduty.com/api/v1/incidents/#{incident_key}/acknowledge")

Should end with .get() or .post().

Also, possibly due to bad paste, your indentation if off a bit in the middle:

  .post(string_query) (err, res, body) ->
  result = JSON.parse(body)
  if result.status == "success"
    msg.send "Your log has been sent"
  else
    msg.send "There was an error sending your log."

Should be:

  .post(string_query) (err, res, body) ->
    result = JSON.parse(body)
    if result.status == "success"
      msg.send "Your log has been sent"
    else
      msg.send "There was an error sending your log."

Another thing, due to nature of NodeJS, these HTTP calls are made asynchronously, and there is a good chance that second call finishes before first one does. To avoid that, run the second request from callback of the first one:

msg
  .http("https://events.pagerduty.com/generic/2010-04-15/create_event.json")
  .headers
  "Content-type": "application/json",
  "Content-length": content_length
  .post(string_query) (err, res, body) ->
    result = JSON.parse(body)
    if result.status == "success"
      msg.send "Your log has been sent"
      msg
        .http("https://xyz.pagerduty.com/api/v1/incidents/#{incident_key}/acknowledge")
        .get()
    else
      msg.send "There was an error sending your log."

You can find some examples of Hubot HTTP requests and other scripting tricks here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top