Verified Commit f2858c3a authored by CompileNix's avatar CompileNix

add MS Teams support

parent fe48f6bd
......@@ -65,6 +65,9 @@ class Config {
this.slackChannel = '#general'
this.slackUsername = 'webserver-access_log-bot'
this.enableMsTeams = false
this.teamsWebHookUri = 'https://outlook.office.com/webhook/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx@xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/IncomingWebhook/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
this.botName = 'web-access_log2email'
this.botIcon = 'https://compilenix.org/cdn/Compilenix.png'
this.debug = true
......@@ -91,26 +94,49 @@ class Config {
}
},
{
match: / status:5(?!03)\d{2} /, // matches all 5xx status codes, except a 503 (using regex negative lookahead)
match: / status:5\d{2} /, // matches all 5xx status codes
// filter: (/** @type {string} */ line) => { return line.match(/./); }, // use custom regex filter instead of this.defaultMatchingGroupName
subject: (/** @type {string[]|string} */ match) => {
if (match[this.defaultMatchingGroupName.Path].startsWith('/RequestToIgnore')) return false // return false to ignore/dismiss this message
return `HTTP ${match[this.defaultMatchingGroupName.StatusCode]}\n`
},
template: (/** @type {string[]|string} */ match) => {
return `${match[this.defaultMatchingGroupName.Method]} ${match[this.defaultMatchingGroupName.Domain]} \`${match[this.defaultMatchingGroupName.Path]}\`\nUser-Agent: \`${match[this.defaultMatchingGroupName.UserAgent]}\``
},
webhookUri: 'https://hooks.slack.com/services/xxxxxx/xxxxxx/xxxxxx',
slackOptions: {
channel: this.slackChannel,
username: this.slackUsername,
attachments: [{
footer: this.botName,
footer_icon: this.botIcon,
color: '#c4463d',
mrkdwn_in: ['text', 'pretext']
}]
return `{
"@type": "MessageCard",
"@context": "http://schema.org/extensions",
"summary": "Server Error post",
"themeColor": "c4463d",
"sections": [
{
"activityTitle": "Server Error (500)",
"facts": [
{
"name": "When:",
"value": "\`${match[this.defaultMatchingGroupName.DateTime]}\`"
},
{
"name": "Domain:",
"value": "\`${match[this.defaultMatchingGroupName.Domain]}\`"
},
{
"name": "Request:",
"value": "\`${match[this.defaultMatchingGroupName.Request]}\`"
},
{
"name": "User-Agent:",
"value": "\`${match[this.defaultMatchingGroupName.UserAgent]}\`"
},
{
"name": "Referer:",
"value": "\`${match[this.defaultMatchingGroupName.Referer]}\`"
}
],
"text": ""
}
]
}`
}
// webhookUri: 'https://outlook.office.com/webhook/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx@xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/IncomingWebhook/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
}
]
}
......
......@@ -8,9 +8,14 @@ const Tail = require('tail').Tail
const config = require('./config.js')
let transporter
let mailOptions = {}
const mailOptions = {
from: config.mailfrom,
to: config.mailto,
subject: '',
text: ''
}
const fileWatchers = {}
let messages = []
const messages = []
let queueWorkerRunning = false
async function notificationQueueWorker () {
......@@ -18,7 +23,7 @@ async function notificationQueueWorker () {
queueWorkerRunning = true
while (messages.length > 0) {
let message = messages[0]
const message = messages[0]
messages.shift()
if (!message) {
......@@ -47,29 +52,32 @@ async function notificationQueueWorker () {
message.expression.template = () => oldTemplateValue
}
let subject = message.expression.subject(match)
let template = message.expression.template(match)
const subject = message.expression.subject(match)
const template = message.expression.template(match)
let messageFiltered = false
if (subject === false || template === false) {
messageFiltered = true
}
if (!messageFiltered && config.enableEmail) {
await sendMail({
from: config.mailfrom,
to: config.mailto,
subject: `${config.subjectPrefix}${subject}`,
text: template
})
mailOptions.text = template
mailOptions.subject = `${config.subjectPrefix}${subject}`
await sendMail(mailOptions)
mailOptions.text = ''
mailOptions.subject = ''
}
const oldSlackOptions = message.expression.slackOptions
if (!messageFiltered && config.enableSlack) {
let payload = message.expression.slackOptions
if (!messageFiltered && config.enableSlack && oldSlackOptions) {
const payload = message.expression.slackOptions
payload.attachments[0].fallback = `${subject}${template}`
payload.attachments[0].text = payload.attachments[0].fallback
payload.attachments[0].ts = Date.now() / 1000
await sendWebook(payload, message.expression.webhookUri)
await sendWebook(payload, message.expression.webhookUri || config.slackWebHookUri)
}
if (!messageFiltered && config.enableMsTeams) {
await sendWebook(JSON.parse(template), message.expression.webhookUri || config.teamsWebHookUri)
}
message.expression.subject = oldSubject
......@@ -154,17 +162,10 @@ function setupSmtp () {
pass: config.smtpPassword
}
})
mailOptions = {
from: config.mailfrom,
to: config.mailto,
subject: `${config.subjectPrefix} -`,
text: ''
}
}
async function sendWebook (payload, uri, rejectUnauthorized = true) {
return new Promise(async (resolve, reject) => {
return new Promise((resolve, reject) => {
try {
const data = JSON.stringify(payload, /* replacer */ null, /* space */ 0)
const url = new URL(uri)
......@@ -176,6 +177,7 @@ async function sendWebook (payload, uri, rejectUnauthorized = true) {
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length,
// eslint-disable-next-line quote-props
'Accept': 'application/json, text/json;q=0.9, */*;q=0',
'Accept-Language': 'en',
'Accept-Encoding': 'identity'
......
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