2019-10-13 03:38:25 +02:00
|
|
|
/**
|
|
|
|
* Slack webhooks component
|
|
|
|
*
|
|
|
|
* Create webhooks publisher
|
|
|
|
*
|
|
|
|
* @file This files defines the webhooks publisher
|
|
|
|
* @author nioc
|
|
|
|
* @since 1.0.0
|
|
|
|
* @license AGPL-3.0+
|
|
|
|
*/
|
|
|
|
|
2019-10-20 17:36:09 +02:00
|
|
|
module.exports = (logger, config, xmpp, user, destination, message, sendToGroup, code, callback = () => {}) => {
|
2019-10-13 03:38:25 +02:00
|
|
|
let webhook = config.getOutgoingWebhook(code)
|
|
|
|
if (!webhook) {
|
|
|
|
logger.warn(`There is no webhook with code "${code}"`)
|
2019-10-20 17:36:09 +02:00
|
|
|
callback(new Error(`There is no webhook with code "${code}"`), null, null)
|
2019-10-13 03:38:25 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
const request = require('request')
|
|
|
|
// request.debug = true
|
|
|
|
let options = {
|
|
|
|
method: 'POST',
|
|
|
|
url: webhook.url,
|
|
|
|
strictSSL: webhook.strictSSL
|
|
|
|
}
|
|
|
|
logger.trace('Outgoing webhook url:', webhook.url)
|
|
|
|
switch (webhook.authMethod) {
|
|
|
|
case 'basic':
|
|
|
|
logger.trace(`Basic auth method user: ${webhook.user} pass: ${webhook.password}`)
|
|
|
|
options.auth = {
|
|
|
|
user: webhook.user,
|
|
|
|
pass: webhook.password
|
|
|
|
}
|
|
|
|
break
|
|
|
|
case 'bearer':
|
|
|
|
logger.trace(`Bearer token auth method bearer: ${webhook.bearer}`)
|
|
|
|
options.auth = {
|
|
|
|
user: null,
|
|
|
|
pass: null,
|
|
|
|
sendImmediately: true,
|
|
|
|
bearer: webhook.bearer
|
|
|
|
}
|
|
|
|
break
|
|
|
|
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
switch (webhook.contentType) {
|
|
|
|
case 'application/x-www-form-urlencoded':
|
|
|
|
logger.trace('Content-type: application/x-www-form-urlencoded')
|
|
|
|
options.form = {
|
|
|
|
from: user,
|
|
|
|
message: message,
|
|
|
|
channel: destination
|
|
|
|
}
|
|
|
|
logger.trace('Outgoing webhook request:', options.form)
|
|
|
|
break
|
|
|
|
case 'application/json':
|
|
|
|
logger.trace('Content-type: application/json')
|
|
|
|
options.json = {
|
|
|
|
from: user,
|
|
|
|
message: message,
|
|
|
|
channel: destination
|
|
|
|
}
|
|
|
|
logger.trace('Outgoing webhook request:', options.json)
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
logger.trace('Outgoing webhook options:', options)
|
|
|
|
request(options, function (error, response, body) {
|
|
|
|
logger.debug('statusCode:', response && response.statusCode)
|
|
|
|
if (error) {
|
|
|
|
logger.error('HTTP error:', error)
|
2019-10-20 17:36:09 +02:00
|
|
|
callback(new Error('HTTP error:', error), null, null)
|
2019-10-13 03:38:25 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if (response.statusCode === 200) {
|
|
|
|
logger.trace('Response:', body)
|
2019-10-20 17:36:09 +02:00
|
|
|
if (body && typeof (body) === 'object' && 'reply' in body === true) {
|
2019-10-13 03:38:25 +02:00
|
|
|
logger.debug(`There is a reply to send back in chat ${destination}: ${body.reply}`)
|
|
|
|
xmpp.send(destination, body.reply, sendToGroup)
|
2019-10-20 17:36:09 +02:00
|
|
|
callback(null, `Message sent. There is a reply to send back in chat ${destination}: ${body.reply}`, null)
|
|
|
|
return
|
2019-10-13 03:38:25 +02:00
|
|
|
}
|
2019-10-20 17:36:09 +02:00
|
|
|
callback(null, 'Message sent', null)
|
|
|
|
return
|
2019-10-13 03:38:25 +02:00
|
|
|
}
|
2019-10-20 17:36:09 +02:00
|
|
|
callback(new Error('HTTP error:', response.statusCode), null, null)
|
2019-10-13 03:38:25 +02:00
|
|
|
})
|
|
|
|
}
|