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-11-23 19:18:35 +01:00
|
|
|
module.exports = async (logger, config, xmpp, user, destination, message, type, code) => {
|
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-11-23 19:18:35 +01:00
|
|
|
throw new Error(`There is no webhook with code "${code}"`)
|
2019-10-13 03:38:25 +02:00
|
|
|
}
|
2019-11-23 19:18:35 +01:00
|
|
|
const { promisify } = require('util')
|
|
|
|
const request = promisify(require('request'))
|
2019-10-13 03:38:25 +02:00
|
|
|
// 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
|
|
|
|
}
|
2019-11-23 19:37:28 +01:00
|
|
|
options.timeout = webhook.timeout || 5000
|
2019-10-13 03:38:25 +02:00
|
|
|
logger.trace('Outgoing webhook options:', options)
|
2019-11-23 19:18:35 +01:00
|
|
|
try {
|
|
|
|
const { statusCode, body } = await request(options)
|
|
|
|
if (statusCode === 200) {
|
2019-10-13 03:38:25 +02:00
|
|
|
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}`)
|
2019-11-21 01:44:43 +01:00
|
|
|
xmpp.send(destination, body.reply, type)
|
2019-11-23 19:18:35 +01:00
|
|
|
return `Message sent. There is a reply to send back in chat ${destination}: ${body.reply}`
|
2019-10-13 03:38:25 +02:00
|
|
|
}
|
2019-11-23 19:18:35 +01:00
|
|
|
return 'Message sent'
|
2019-10-13 03:38:25 +02:00
|
|
|
}
|
2019-11-23 19:18:35 +01:00
|
|
|
throw new Error(`HTTP error: ${statusCode}`)
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(`Error during outgoing webhook request: ${error.message}`)
|
|
|
|
throw error
|
|
|
|
}
|
2019-10-13 03:38:25 +02:00
|
|
|
}
|