feat: mirror WAN reports to Matrix
This commit is contained in:
parent
04f31306b5
commit
dc57990bca
3 changed files with 39 additions and 1 deletions
30
index.js
30
index.js
|
|
@ -15,6 +15,9 @@ const CONFIG = {
|
|||
whatsappApi: process.env.WHATSAPP_API_ENDPOINT || 'http://localhost:3001',
|
||||
whatsappSession: process.env.WHATSAPP_SESSION || 'EdGpt',
|
||||
adminPhone: process.env.ADMIN_PHONE,
|
||||
matrixApi: process.env.MATRIX_API_ENDPOINT || 'https://whatsed.eddydevink.nl',
|
||||
matrixTokenFile: process.env.MATRIX_TOKEN_FILE || '/home/aiaiaiaiagent/whatsed-new/secrets/sherpa-token',
|
||||
matrixRoomFile: process.env.MATRIX_ROOM_FILE || '/home/aiaiaiaiagent/whatsed-new/secrets/eddy-report-room',
|
||||
dnsRecords: (process.env.DNS_RECORDS || '').split(',').map(s => s.trim()).filter(Boolean),
|
||||
};
|
||||
|
||||
|
|
@ -133,6 +136,31 @@ async function sendWhatsApp(message) {
|
|||
}
|
||||
}
|
||||
|
||||
async function sendMatrix(message) {
|
||||
try {
|
||||
const token = fs.readFileSync(CONFIG.matrixTokenFile, 'utf8').trim();
|
||||
const room = fs.readFileSync(CONFIG.matrixRoomFile, 'utf8').trim();
|
||||
if (!token || !room) throw new Error('Matrix token or room is empty');
|
||||
const txn = `${Date.now()}-${process.pid}`;
|
||||
const url = `${CONFIG.matrixApi}/_matrix/client/v3/rooms/${encodeURIComponent(room)}/send/m.room.message/${txn}`;
|
||||
const resp = await fetchWithTimeout(url, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ msgtype: 'm.text', body: message }),
|
||||
}, 10000);
|
||||
if (!resp.ok) {
|
||||
logError(`Matrix notification failed: ${resp.status}`);
|
||||
} else {
|
||||
log('Matrix notification sent.');
|
||||
}
|
||||
} catch (e) {
|
||||
logError(`Matrix notification error: ${e.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
log('WAN IP Guard check starting.');
|
||||
|
||||
|
|
@ -193,7 +221,7 @@ async function main() {
|
|||
].join('\n');
|
||||
|
||||
await sendWhatsApp(message);
|
||||
|
||||
await sendMatrix(message);
|
||||
if (failCount === 0) {
|
||||
writeState(currentIp);
|
||||
log(`State saved: ${currentIp}`);
|
||||
|
|
|
|||
Loading…
Reference in a new issue