coffeescript - calling 2 https requests simultaneously in hubot coffescript -
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 error sending log." msg .http("https://xyz.pagerduty.com/api/v1/incidents/#{incident_key}/acknowledge")
i trying auto acknowledge event triggered in pagerduty. first http request takes effect. second http request (last line of code) never takes effect. have tried varipus combinations. not help. new coffeescript , use hubot.can me ?
first, don't specify http action doing:
msg.http("https://xyz.pagerduty.com/api/v1/incidents/#{incident_key}/acknowledge")
should end .get()
or .post()
.
also, possibly due bad paste, indentation if off bit in 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 error sending 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 error sending log."
another thing, due nature of nodejs, these http calls made asynchronously, , there chance second call finishes before first 1 does. avoid that, run second request callback of 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 error sending log."
you can find examples of hubot http requests , other scripting tricks here.
Comments
Post a Comment