hall-integrations
Version:
Node.js wrapper for Hall Custom Integration API. Send custom messages to your rooms on Hall.
73 lines (58 loc) • 1.86 kB
JavaScript
var https = require('https'),
when = require('when'),
constants = require('./constants');
/**
* Hall Integrations API
* @constructor
*/
function HallIntegrations (roomApiToken) {
if (!roomApiToken || !roomApiToken.length) {
throw Error('Missing Hall Room API Token. For details, please see ' + constants.DOCS_URL);
}
this.apiPath = constants.API_PATH.replace('%s', roomApiToken);
}
/**
* Send a custom integration message
* @param {string} title to display for message
* @param {string} message text to send, may contain some HTML. For details, please see <https://hall.com/docs/integrations/generic/>
* @param {string} [pictureUrl] url of photo to display next to message in chat view.
* @param {function} [callback] will be called with success/failure details
* @return {promise} promise to track success/failure of sending message
*/
HallIntegrations.prototype.send = function (title, message, pictureUrl, callback) {
var def, request, postData;
def = when.defer();
// Payload JSON
postData = JSON.stringify({
title: title,
message: message,
picture: pictureUrl
});
// Create HTTP Request
request = https.request({
hostname : constants.API_HOST,
path : this.apiPath,
method : constants.API_METHOD,
port : constants.API_PORT,
headers : {
'Content-Type' : 'application/json',
'Content-Length' : postData.length
}
}, function (response) {
def.resolve(response);
if (typeof callback === 'function') {
callback(null, response);
}
});
// Handle request error
request.on('error', function (error) {
def.reject(error);
if (typeof callback === 'function') {
callback(error);
}
});
request.write(postData);
request.end();
return def.promise;
};
module.exports = HallIntegrations;