diff --git a/bot-monitor/Alert.ts b/bot-monitor/Alert.ts index 6e10e64..f9dcb63 100644 --- a/bot-monitor/Alert.ts +++ b/bot-monitor/Alert.ts @@ -61,18 +61,18 @@ export class Alert { log(`[i] Sending email for "${this.name}" to ${this.rule.email}`); let subject = `[${this.rule.bot}] ${this.rule.task} failure`; - const webKey = crypto.randomBytes(32).toString('hex'); + this.rule.webKey = crypto.randomBytes(32).toString('hex'); if (this.rule.email.includes('@')) { await mailTransporter.sendMail({ from: 'tools.sdzerobot@tools.wmflabs.org', to: this.rule.email, subject: subject, - html: this.getEmailBodyHtml(webKey), + html: this.getEmailBodyHtml(), }); } else { await new bot.User(this.rule.email).email( subject, - this.getEmailBodyPlain(webKey), + this.getEmailBodyPlain(), {ccme: true} ).catch(err => { if (err.code === 'notarget') { @@ -88,24 +88,24 @@ export class Alert { }); } - getEmailBodyHtml(webKey: string): string { + getEmailBodyHtml(): string { const taskKey = `${this.rule.bot}: ${this.rule.task}`; return `${this.rule.bot}'s task ${this.rule.task} failed to run per the configuration specified at Wikipedia:Bot activity monitor/Configurations. Detected only ${this.actions} ${this.rule.action === 'edit' ? 'edit' : `"${this.rule.action}" action`}s in the last ${this.rule.duration}, whereas at least ${this.rule.minEdits} were expected.` + `

` + `If your bot is behaving as expected, then you may want to modify the task configuration instead. Or to unsubscribe from these email notifications, remove the |email= parameter from the {{/task}} template.` + `

` + - `To temporarily pause these notifications, click here: https://sdzerobot.toolforge.org/bot-monitor/pause?task=${encodeURIComponent(taskKey)}&webKey=${webKey}` + + `To temporarily pause these notifications, click here: https://sdzerobot.toolforge.org/bot-monitor/pause?task=${encodeURIComponent(taskKey)}&webKey=${this.rule.webKey}` + `

` + `Thanks!`; } - getEmailBodyPlain(webKey: string): string { + getEmailBodyPlain(): string { const taskKey = `${this.rule.bot}: ${this.rule.task}`; return `${this.rule.bot}'s task "${this.rule.task}" failed to run per the configuration specified at Wikipedia:Bot activity monitor/Configurations (). Detected only ${this.actions} ${this.rule.action === 'edit' ? 'edit' : `"${this.rule.action}" action`}s in the last ${this.rule.duration}, whereas at least ${this.rule.minEdits} were expected.` + `\n\n` + `If your bot is behaving as expected, then you may want to modify the task configuration instead. Or to unsubscribe from these email notifications, remove the |email= parameter from the {{/task}} template.` + `\n\n` + - `To temporarily pause these notifications, click here: https://sdzerobot.toolforge.org/bot-monitor/pause?task=${encodeURIComponent(taskKey)}&webKey=${webKey}` + + `To temporarily pause these notifications, click here: https://sdzerobot.toolforge.org/bot-monitor/pause?task=${encodeURIComponent(taskKey)}&webKey=${this.rule.webKey}` + `\n\n` + `Thanks!`; } diff --git a/bot-monitor/AlertsDb.ts b/bot-monitor/AlertsDb.ts index 905fffc..f2909b7 100644 --- a/bot-monitor/AlertsDb.ts +++ b/bot-monitor/AlertsDb.ts @@ -3,7 +3,6 @@ import {MwnDate} from "mwn"; import {getKey, Rule} from "./Rule"; import {bot, toolsdb} from "../botbase"; import * as fs from "fs/promises"; -import * as crypto from "crypto"; import {getRedisInstance, Redis} from "../redis"; import {ResultSetHeader} from "mysql2"; import {CustomError} from "../utils"; @@ -46,7 +45,7 @@ class MariadbAlertsDb implements AlertsDb { async saveLastEmailedTime(rule: Rule): Promise { await this.db.run( `REPLACE INTO alerts (name, lastEmailed, webKey) VALUES(?, UTC_TIMESTAMP(), ?)`, - [ getKey(rule, 250), crypto.randomBytes(32).toString('hex') ] + [ getKey(rule, 250), rule.webKey ] ); } diff --git a/bot-monitor/Rule.ts b/bot-monitor/Rule.ts index c190fa0..616f029 100644 --- a/bot-monitor/Rule.ts +++ b/bot-monitor/Rule.ts @@ -18,6 +18,7 @@ export interface Rule { fromDate: MwnDate alertPage: MwnTitle email: string + webKey?: string // pingUser: string }