Last active
August 29, 2015 14:13
-
-
Save hakobera/d7c3c39e59bac1026a85 to your computer and use it in GitHub Desktop.
HipChatClient for Google Apps Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* HipChat client for Google Apps Script. | |
* This code is based on node-hipchat https://github.com/nkohari/node-hipchat/blob/master/lib/hipchat.js | |
*/ | |
(function(global) { | |
var HipChatClient = (function () { | |
HipChatClient.prototype.host = 'api.hipchat.com'; | |
function HipChatClient(apikey) { | |
this.apikey = apikey; | |
this.rateLimits = { | |
limit: 0, | |
remaining: 0, | |
reset: 0 | |
}; | |
} | |
HipChatClient.prototype._querystringify = function (obj) { | |
var qs = ''; | |
for (var key in obj) { | |
if (qs !== '') { | |
qs += '&'; | |
} | |
qs += (key + '=' + obj[key]); | |
} | |
return qs; | |
}; | |
HipChatClient.prototype.postMessage = function(params) { | |
var data, options, _ref, _ref1, _ref2; | |
data = { | |
room_id: params.room_id, | |
from: (_ref = params.from) != null ? _ref : 'gas-hipchat', | |
message: params.message, | |
notify: params.notify ? 1 : 0, | |
color: (_ref1 = params.color) != null ? _ref1 : 'yellow', | |
message_format: (_ref2 = params.message_format) != null ? _ref2 : 'html' | |
}; | |
options = this._prepareOptions({ | |
method: 'post', | |
path: '/v1/rooms/message', | |
data: data | |
}); | |
return this._sendRequest(options); | |
}; | |
HipChatClient.prototype._prepareOptions = function (op) { | |
op.host = this.host; | |
if (op.query == null) { | |
op.query = {}; | |
} | |
op.query['auth_token'] = this.apikey; | |
op.query = this._querystringify(op.query); | |
op.path += '?' + op.query; | |
if (op.method === 'post' && (op.data != null)) { | |
op.payload = this._querystringify(op.data); | |
if (op.headers == null) { | |
op.headers = {}; | |
} | |
} | |
op.url = op.host + op.path; | |
return op; | |
}; | |
HipChatClient.prototype._sendRequest = function(options) { | |
var res = UrlFetchApp.fetch(options.url, options); | |
if (res.getResponseCode() === 200) { | |
return JSON.parse(res.getContentText()); | |
} else { | |
Logger.log(res.getContentText()); | |
return null; | |
} | |
}; | |
return HipChatClient; | |
})(); | |
global.HipChatClient = HipChatClient; | |
})(this); | |
/** | |
* Create HipChatClient | |
* | |
* <h3>Usage</h3> | |
* <pre> | |
* var hipchat = HipChatClient.create('your-api-key'); | |
* hipchat.postMessage({ room_id: 'xx', message: 'yy' }); | |
* </pre> | |
* @param {string} apikey | |
*/ | |
function create(apikey) { | |
return new HipChatClient(apikey); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I can't get around this error: "Request failed for https://api.hipchat.com/v2/room/message?auth_token=mytokenishere returned code 405. Truncated server response: { "error": { "code": 405, "message": "
The method is not allowed for the requested URL.
", "type": "Method Not Allowed" } } (use muteHttpExceptions option to examine full response)"Any ideas?