I have access to the WhatsApp API panel. I want to create a script that notifies me on WhatsApp whenever my device goes offline or comes online in GenieACS. Can anyone tell me how to do this?
I added the following script under “alert” in provisioning. However, I’m not receiving alerts when the device goes offline. Can someone tell me if this script is correct or what changes need to be made?
“Can anyone help me so that I can purchase for you the WhatsApp API panel that I am using with my money for one month.”
Here is my script:
const axios = require(‘axios’);
const genieacsUrl = ‘https://localhost:3000’;
const username = ‘testuser’;
const password = ‘test321’;
const whatsappClientId = ‘eyJ1aWQiOiJsOE9rS0JxdTZ1cjNLZWVSUUpEcXNZbjVSOHN’;
const whatsappMobile = ‘919145406872’;
const whatsappToken = ‘eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1S0JxdTZ1cjNLZWVSUUpEcXGK8wtXv3MeyLTr9M’;
(async () => {
try {
const response = await axios.get(‘https://connect.gnetworkservices.in/api/user/v2/send_message_url’, {
params: {
client_id: whatsappClientId,
mobile: whatsappMobile,
text: “Provisioning script executed successfully!”,
token: whatsappToken
}
});
console.log('Message sent successfully:', response.data);
// Upload script to GenieACS
const scriptRes = await axios.post(`${genieacsUrl}/provision`, {
name: 'offline_alert_script',
file: `
async function main() {
while (true) {
const devices = await genieACSRequest('GET', '/devices', {});
const offlineDevices = devices.filter(device => device.status === 'offline');
if (offlineDevices.length > 0) {
const offlineMessage = offlineDevices.map(device => {
return \`Device Serial Number: \${device.DeviceID.SerialNumber}, PPPoE Username: \${device.InternetGatewayDevice.WANDevice.1.WANConnectionDevice.1.WANPPPConnection.1.Username}, MAC Address: \${device.InternetGatewayDevice.WANDevice.1.WANConnectionDevice.1.WANPPPConnection.1.MACAddress}\`;
}).join('\\n');
await sendWhatsappMessage(offlineMessage);
}
await sleep(24 * 60 * 60 * 1000); // Sleep for 24 hours
}
}
async function genieACSRequest(method, path, data) {
const response = await axios({
method: method,
url: genieacsUrl + path,
auth: {
username: username,
password: password
},
data: data
});
return response.data;
}
async function sendWhatsappMessage(message) {
const response = await axios.get('https://connect.gnetworkservices.in/api/user/v2/send_message_url', {
params: {
client_id: '${whatsappClientId}',
mobile: '${whatsappMobile}',
text: message,
token: '${whatsappToken}'
}
});
console.log('Message sent successfully:', response.data);
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
main();
`
}, {
auth: {
username: username,
password: password
}
});
console.log('Script uploaded successfully:', scriptRes.data);
// Add provisioning rule
const ruleRes = await axios.post(`${genieacsUrl}/provision`, {
name: 'offline_alert_rule',
key: 'timestamp',
interval: 24,
unit: 'hours',
action: 'exec',
execTimeout: 3600,
execData: {
name: 'offline_alert_script',
parameters: []
}
}, {
auth: {
username: username,
password: password
}
});
console.log('Provisioning rule added successfully:', ruleRes.data);
} catch (error) {
console.error('Error:', error.response ? error.response.data : error.message);
}
})();
my working whatsapp api example is
curl -X GET 'https://connect.gnetworkservices.in/api/user/v2/send_message_url?client_id=CLIENT_ID&mobile=918888888888&text=Hello&token=YOUR_API_KEYS'