2021-03-12 16:15:26 +00:00
|
|
|
const got = require('got');
|
2020-01-20 06:33:24 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('send', {
|
|
|
|
description: 'Send a RocketChat message',
|
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [{
|
|
|
|
arg: 'to',
|
2021-03-15 11:26:11 +00:00
|
|
|
type: 'string',
|
2020-01-20 06:33:24 +00:00
|
|
|
required: true,
|
2020-01-23 12:31:07 +00:00
|
|
|
description: 'User (@) or channel (#) to send the message'
|
2020-01-20 06:33:24 +00:00
|
|
|
}, {
|
|
|
|
arg: 'message',
|
2021-03-15 11:26:11 +00:00
|
|
|
type: 'string',
|
2020-01-20 06:33:24 +00:00
|
|
|
required: true,
|
|
|
|
description: 'The message'
|
|
|
|
}],
|
|
|
|
returns: {
|
2021-03-15 11:26:11 +00:00
|
|
|
type: 'object',
|
2020-01-20 06:33:24 +00:00
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/send`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Self.send = async(ctx, to, message) => {
|
|
|
|
const models = Self.app.models;
|
|
|
|
const accessToken = ctx.req.accessToken;
|
|
|
|
const sender = await models.Account.findById(accessToken.userId);
|
|
|
|
const recipient = to.replace('@', '');
|
|
|
|
|
2021-03-12 16:15:26 +00:00
|
|
|
if (sender.name != recipient) {
|
|
|
|
let {body} = await sendMessage(sender, to, message);
|
|
|
|
if (body)
|
|
|
|
body = JSON.parse(body);
|
|
|
|
else
|
|
|
|
body = false;
|
|
|
|
|
|
|
|
return body;
|
|
|
|
}
|
2020-02-14 06:53:13 +00:00
|
|
|
|
|
|
|
return false;
|
2020-01-20 06:33:24 +00:00
|
|
|
};
|
|
|
|
|
2020-01-23 12:31:07 +00:00
|
|
|
async function sendMessage(sender, channel, message) {
|
|
|
|
const config = await getConfig();
|
|
|
|
const avatar = `${config.host}/avatar/${sender.name}`;
|
|
|
|
const uri = `${config.api}/chat.postMessage`;
|
2020-01-20 06:33:24 +00:00
|
|
|
|
2020-01-23 12:31:07 +00:00
|
|
|
return sendAuth(uri, {
|
|
|
|
'channel': channel,
|
|
|
|
'avatar': avatar,
|
2020-02-07 13:04:18 +00:00
|
|
|
'alias': sender.nickname,
|
2020-01-20 06:33:24 +00:00
|
|
|
'text': message
|
|
|
|
}).catch(async error => {
|
2020-05-03 12:30:19 +00:00
|
|
|
if (error.statusCode === 401) {
|
2020-02-07 10:50:09 +00:00
|
|
|
this.auth = null;
|
2020-01-20 06:33:24 +00:00
|
|
|
|
2020-01-23 12:31:07 +00:00
|
|
|
return sendMessage(sender, channel, message);
|
2020-01-20 06:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(error.message);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a rocketchat token
|
|
|
|
* @return {Object} userId and authToken
|
|
|
|
*/
|
2020-01-23 12:31:07 +00:00
|
|
|
async function getAuthToken() {
|
|
|
|
if (!this.auth || this.auth && !this.auth.authToken) {
|
|
|
|
const config = await getConfig();
|
|
|
|
const uri = `${config.api}/login`;
|
2021-03-12 16:15:26 +00:00
|
|
|
let {body} = await send(uri, {
|
2020-01-23 12:31:07 +00:00
|
|
|
user: config.user,
|
|
|
|
password: config.password
|
|
|
|
});
|
|
|
|
|
2021-03-12 16:15:26 +00:00
|
|
|
if (body) {
|
|
|
|
body = JSON.parse(body);
|
|
|
|
this.auth = body.data;
|
|
|
|
}
|
2020-01-23 12:31:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.auth;
|
2020-01-20 06:33:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 12:31:07 +00:00
|
|
|
/**
|
|
|
|
* Returns a rocketchat config
|
|
|
|
* @return {Object} Auth config
|
|
|
|
*/
|
|
|
|
async function getConfig() {
|
|
|
|
if (!this.chatConfig) {
|
|
|
|
const models = Self.app.models;
|
|
|
|
|
|
|
|
this.chatConfig = await models.ChatConfig.findOne();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.chatConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send unauthenticated request
|
|
|
|
* @param {*} uri - Request uri
|
2021-03-12 16:15:26 +00:00
|
|
|
* @param {*} params - Request params
|
2020-01-23 12:31:07 +00:00
|
|
|
* @param {*} options - Request options
|
|
|
|
*
|
|
|
|
* @return {Object} Request response
|
|
|
|
*/
|
2021-03-12 16:15:26 +00:00
|
|
|
async function send(uri, params, options = {}) {
|
2020-01-20 06:33:24 +00:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
return new Promise(resolve => {
|
2021-03-15 11:26:11 +00:00
|
|
|
return resolve({
|
|
|
|
body: JSON.stringify(
|
|
|
|
{statusCode: 200, message: 'Fake notification sent'}
|
|
|
|
)
|
|
|
|
});
|
2020-01-20 06:33:24 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-23 12:31:07 +00:00
|
|
|
const defaultOptions = {
|
2022-01-12 07:32:01 +00:00
|
|
|
form: params
|
2020-01-20 06:33:24 +00:00
|
|
|
};
|
|
|
|
|
2020-01-23 12:31:07 +00:00
|
|
|
if (options) Object.assign(defaultOptions, options);
|
|
|
|
|
2021-03-12 16:15:26 +00:00
|
|
|
return got.post(uri, defaultOptions);
|
2020-01-23 12:31:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send authenticated request
|
|
|
|
* @param {*} uri - Request uri
|
|
|
|
* @param {*} body - Request params
|
|
|
|
*
|
|
|
|
* @return {Object} Request response
|
|
|
|
*/
|
|
|
|
async function sendAuth(uri, body) {
|
|
|
|
const login = await getAuthToken();
|
|
|
|
const options = {
|
2021-03-12 16:15:26 +00:00
|
|
|
headers: {}
|
2020-01-23 12:31:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (login) {
|
|
|
|
options.headers['X-Auth-Token'] = login.authToken;
|
|
|
|
options.headers['X-User-Id'] = login.userId;
|
2020-01-20 06:33:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 12:31:07 +00:00
|
|
|
return send(uri, body, options);
|
2020-01-20 06:33:24 +00:00
|
|
|
}
|
|
|
|
};
|