Add support for sending stats to Matrix, split out some code into more generic classes. #1
46
index.js
46
index.js
|
@ -137,7 +137,17 @@ async function sendSummary() {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function sendSummaryMatrix(yesterday, stats) {
|
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": `
|
||||||
<table>
|
<table>
|
||||||
<thead>
|
<thead>
|
||||||
<tr><td colspan="2"><strong>Summary from ${yesterday.startDate.toString()} to ${yesterday.endDate.toString()}</strong></td></tr>
|
<tr><td colspan="2"><strong>Summary from ${yesterday.startDate.toString()} to ${yesterday.endDate.toString()}</strong></td></tr>
|
||||||
|
@ -151,25 +161,29 @@ async function sendSummaryMatrix(yesterday, stats) {
|
||||||
${stats.isNewRecord ? `<tr><td colspan="2"><span data-mx-color="#FFD700"><font color="#FFD700">🎉 NEW RECORD! 🎉</font></span> A new record has been set, at ${stats.totalCallsMade} calls in a day!</td></tr>` : ''}
|
${stats.isNewRecord ? `<tr><td colspan="2"><span data-mx-color="#FFD700"><font color="#FFD700">🎉 NEW RECORD! 🎉</font></span> A new record has been set, at ${stats.totalCallsMade} calls in a day!</td></tr>` : ''}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
`;
|
`.split('\n').map(s => s.trim()).join(''),
|
||||||
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(''),
|
|
||||||
"tel.litenet.call_stats_summary": {
|
"tel.litenet.call_stats_summary": {
|
||||||
...stats, date: yesterday
|
...stats, date: yesterday
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log("Sending Matrix message:", JSON.stringify(message, null, 2));
|
console.log("Sending Matrix message:", JSON.stringify(message, null, 2));
|
||||||
console.log("Plaintext:\n", message.body)
|
console.log("Plaintext:\n", message.body);
|
||||||
console.log("HTML:\n", message.formatted_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) {
|
async function sendSummaryDiscord(yesterday, stats) {
|
||||||
|
@ -200,7 +214,7 @@ async function sendSummaryDiscord(yesterday, stats) {
|
||||||
|
|
||||||
const payload = { embeds: [ embed ] };
|
const payload = { embeds: [ embed ] };
|
||||||
console.log("Sending Discord message:", JSON.stringify(payload, null, 2));
|
console.log("Sending Discord message:", JSON.stringify(payload, null, 2));
|
||||||
if (hook)
|
if (hook && !process.env.NOOP)
|
||||||
await hook.send(payload);
|
await hook.send(payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue