Merge branch 'dev' of https://gitea.verdnatura.es/verdnatura/salix into dev
gitea/salix/dev This commit has test failures Details

This commit is contained in:
Javi Gallego 2019-10-29 12:58:39 +01:00
commit 7dfdfc45c7
2 changed files with 171 additions and 0 deletions

View File

@ -0,0 +1,170 @@
/*
Author : Enrique Blasco BLanquer
Date: 29 de octubre de 2019
*/
let request = require('request-promise-native');
let UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethod('sendMessage', {
description: 'Send a RocketChat message',
accessType: 'WRITE',
accepts: [{
arg: 'from',
type: 'String',
required: true,
description: 'user who sends the message'
}, {
arg: 'to',
type: 'String',
required: true,
description: 'user (@) or channel (#) to send the message'
}, {
arg: 'message',
type: 'String',
required: true,
description: 'The message'
}],
returns: {
type: 'boolean',
root: true
},
http: {
path: `/sendMessage`,
verb: 'POST'
}
});
Self.sendMessage = async(from, to, message) => {
const rocketUser = await getRocketUser();
const userId = rocketUser.data.userId;
const authToken = rocketUser.data.authToken;
if (to.includes('@')) return await sendUserMessage(to.replace('@', ''), userId, authToken, '@' + from + ' te ha mandado un mensaje: ' + message);
else return await sendChannelMessage(to.replace('#', ''), userId, authToken, '@' + from + ' dice: ' + message);
};
/**
* Returns a rocketchat token
* @return {Object} userId and authToken
*/
async function getRocketUser() {
const url = 'https://chat.verdnatura.es/api/v1/login';
const options = {
method: 'POST',
uri: url,
body: {
user: 'VnBot',
password: 'Ub606cux7op.'
},
headers: {
'content-type': 'application/json'
},
json: true
};
return await request(options)
.then(function(parsedBody) {
return parsedBody;
})
.catch(function(err) {
throw new UserError(err);
});
}
/**
* Send a user message
* @param {String} to user to send the message
* @param {String} userId rocket user id
* @param {String} authToken rocket token
* @param {String} message The message
* @return {Object} rocket info
*/
async function sendUserMessage(to, userId, authToken, message) {
const url = 'https://chat.verdnatura.es/api/v1/chat.postMessage';
const options = {
method: 'POST',
uri: url,
body: {
'channel': '@' + to,
'text': message
},
headers: {
'X-Auth-Token': authToken,
'X-User-Id': userId,
'content-type': 'application/json'
},
json: true
};
return await request(options)
.then(function(parsedBody) {
return parsedBody;
})
.catch(function(err) {
throw new UserError(err);
});
}
/**
* Send a channel message
* @param {String} to channel to send the message
* @param {String} userId rocket user id
* @param {String} authToken rocket token
* @param {String} message The message
* @return {Object} rocket info
*/
async function sendChannelMessage(to, userId, authToken, message) {
const channelInfo = await getChannelId(to, userId, authToken);
const url = 'https://chat.verdnatura.es/api/v1/chat.sendMessage';
const channelId = channelInfo.channel._id;
const options = {
method: 'POST',
uri: url,
body: {
'message': {
'rid': channelId,
'msg': message
}
},
headers: {
'X-Auth-Token': authToken,
'X-User-Id': userId,
'content-type': 'application/json'
},
json: true
};
return await request(options)
.then(function(parsedBody) {
return parsedBody;
})
.catch(function(err) {
throw new UserError(err);
});
}
/**
* Get channel id
* @param {String} to channel to get id
* @param {String} userId rocket user id
* @param {String} authToken rocket token
* @return {Object} rocket info
*/
async function getChannelId(to, userId, authToken) {
const url = 'https://chat.verdnatura.es/api/v1/channels.info?roomName=' + to;
const options = {
method: 'GET',
uri: url,
headers: {
'X-Auth-Token': authToken,
'X-User-Id': userId,
'content-type': 'application/json'
},
json: true
};
return await request(options)
.then(function(parsedBody) {
return parsedBody;
})
.catch(function(err) {
throw new UserError(err);
});
}
};

View File

@ -3,4 +3,5 @@ module.exports = Self => {
require('../methods/worker/mySubordinates')(Self);
require('../methods/worker/isSubordinate')(Self);
require('../methods/worker/getWorkerInfo')(Self);
require('../methods/worker/sendMessage')(Self);
};