Commit f81a83bd authored by Jim Tittsler's avatar Jim Tittsler

Add incoming webhook for receiving from RocketChat

parent 9122acce
# WEnotes-server
This is a simple NodeJS-based web and [Faye](http://faye.jcoglan.com)server used for WEnotes. Its main purpose is to allow WEnotes
This is a simple NodeJS-based web and [Faye](http://faye.jcoglan.com)
server used for WEnotes. Its main purpose is to allow WEnotes
clients to subscribe to topics (channels, tags, rooms) of
interest.
......
......@@ -11,7 +11,8 @@
"dependencies": {
"faye": "1.1.x",
"log": "1.4.0",
"node-static": "0.7.x"
"node-static": "0.7.x",
"cradle": "0.7.1"
},
"engine": "node >= 4.4.3"
}
......
......@@ -15,12 +15,37 @@ var util = require('util'),
nodeStatic = require ('node-static'),
url = require('url'),
faye = require('faye'),
cradle = require('cradle'),
options = require('./options.json');
var log = new Log(options.log.server.level,
fs.createWriteStream(options.log.server.file, {
flags: 'a'}));
// split http://user:pass@host:port into a map of its parts
function getConnectionMap(cs) {
var r = {},
parts = cs.split('@');
if (parts.length === 2) {
let userpass = parts[0].replace(/https?:\/\//, '').split(':');
if (userpass.length === 2) {
r.user = userpass[0];
r.pass = userpass[1];
}
parts = parts[1].split(':');
if (parts.length === 2) {
r.host = parts[0];
r.port = parts[1];
}
}
return r;
}
var cdb = getConnectionMap(options.url),
couch = new(cradle.Connection)(cdb.host, cdb.port, {
auth: {username: cdb.user, password: cdb.pass}}),
db = couch.database(options.db);
process.addListener('uncaughtException', (err, stack) => {
log.alert('Exception: ' + err);
log.alert(err.stack);
......@@ -69,6 +94,33 @@ function createBayeuxServer(mount) {
return bayeux;
}
// FIXME: remove RocketChatisms like bold/italic markers?
function cleanText(t) {
// remove :xxx: style emoji
return t.replace(/:[_a-z]+:/g, '');
}
/* incoming webhook integration */
function saveNote(b) {
var datetime = new Date(b.timestamp),
note = {
created_at: datetime.toString(),
from_user: b.user_name,
from_user_name: b.user_name,
profile_image_url: `https://chat.oeru.org/avatar/${encodeURIComponent(b.user_name)}.jpg`,
profile_url: `https://chat.oeru.org/direct/${encodeURIComponent(b.user_name)}`,
text: cleanText(b.text),
we_source: 'chat',
url: `${options.RocketChat.baseURL}channel/${encodeURIComponent(b.channel_name)}`,
we_tags: [ options.RocketChat.tag ],
we_timestamp: b.timestamp
};
log.debug('=============incoming webhook================');
log.debug(b);
db.save(note);
}
function httpHandler(request, response) {
log.debug(`handle ${request.method} request ${request.url}`);
var file = new nodeStatic.Server('./public', {
......@@ -130,15 +182,29 @@ function httpHandler(request, response) {
response.write(jsonString);
response.end();
} else if ((location.pathname.indexOf('/_in/') === 0) && request.method === 'POST') {
let status = 200;
jsonp = params && params.callback;
log.debug('POST: params:', params);
//log.debug('POST: location:', location);
//log.debug('POST: request: ', util.inspect(request));
try {
body = JSON.parse(body);
} catch (e) {
log.alert('incoming webhook JSON parsing error', e);
body = {};
}
log.debug('POST: body:', body);
response.writeHead(200, {
if (!body.token || (body.token !== options.RocketChat.token)) {
log.alert(`bad incoming webook token ${body.token}`);
status = 403;
} else {
saveNote(body);
}
response.writeHead(status, {
'Content-Type': jsonp ? 'application/javascript' : 'application/json'
});
jsonString = JSON.stringify({text: 'Right!'});
jsonString = JSON.stringify({});
if (jsonp) {
jsonString = params.callback + '(' + jsonString + ')';
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment