diff --git a/index.js b/index.js index 82d0c8d..ce4b120 100644 --- a/index.js +++ b/index.js @@ -137,7 +137,17 @@ async function sendSummary() { } async function sendSummaryMatrix(yesterday, stats) { - const html = ` + const message = { + "msgtype": "m.text", + "format": "org.matrix.custom.html", + "body": `Summary from ${new Date(yesterday.start).toDateString()} to ${new Date(yesterday.end).toDateString()}\n +Calls Made: ${stats.totalCallsMade} +Monthly Total: ${stats.totalCallsThisMonth} +Total Calls Ever Placed: ${stats.totalCallsEverPlaced} +System Uptime: ${getSystemUptime().toString(false, false)} +All Time Record: ${stats.allTimeRecord} +${stats.isNewRecord ? `🎉 NEW RECORD! 🎉 A new record has been set, at ${stats.totalCallsMade} calls in a day!` : ''}`.trim(), + "formatted_body": ` @@ -151,25 +161,29 @@ async function sendSummaryMatrix(yesterday, stats) { ${stats.isNewRecord ? `` : ''}
Summary from ${yesterday.startDate.toString()} to ${yesterday.endDate.toString()}
🎉 NEW RECORD! 🎉 A new record has been set, at ${stats.totalCallsMade} calls in a day!
- `; - var message = { - "msgtype": "m.text", - "format": "org.matrix.custom.html", - "body": `Summary from ${new Date(yesterday.start).toDateString()} to ${new Date(yesterday.end).toDateString()}\n -Calls Made: ${stats.totalCallsMade} -Monthly Total: ${stats.totalCallsThisMonth} -Total Calls Ever Placed: ${stats.totalCallsEverPlaced} -System Uptime: ${getSystemUptime().toString(false, false)} -All Time Record: ${stats.allTimeRecord} -${stats.isNewRecord ? `🎉 NEW RECORD! 🎉 A new record has been set, at ${stats.totalCallsMade} calls in a day!` : ''}`, - "formatted_body": html.split('\n').map(s => s.trim()).join(''), + `.split('\n').map(s => s.trim()).join(''), "tel.litenet.call_stats_summary": { ...stats, date: yesterday } } console.log("Sending Matrix message:", JSON.stringify(message, null, 2)); - console.log("Plaintext:\n", message.body) - console.log("HTML:\n", message.formatted_body) + console.log("Plaintext:\n", message.body); + console.log("HTML:\n", message.formatted_body); + + if (!process.env.NOOP) { + if (!process.env.MATRIX_BASE_URL || !process.env.MATRIX_ROOM_ID || !process.env.MATRIX_ACCESS_TOKEN) { + console.warn("MATRIX_BASE_URL, MATRIX_ROOM_ID or MATRIX_ACCESS_TOKEN not set, skipping Matrix message."); + return; + } + await fetch(`${process.env.MATRIX_BASE_URL}/_matrix/client/v3/rooms/${encodeURIComponent(process.env.MATRIX_ROOM_ID)}/send/m.room.message/${Math.random()}`, { + method: 'PUT', + body: message, + headers: { + "Authorization": `Bearer ${process.env.MATRIX_ACCESS_TOKEN}`, + "Content-Type": "application/json" + } + }) + } } async function sendSummaryDiscord(yesterday, stats) { @@ -200,7 +214,7 @@ async function sendSummaryDiscord(yesterday, stats) { const payload = { embeds: [ embed ] }; console.log("Sending Discord message:", JSON.stringify(payload, null, 2)); - if (hook) + if (hook && !process.env.NOOP) await hook.send(payload); }