Apr
21
2015

Send WhatsApp alert during a network fault

A good network engineer must react quickly during a fault. On the market, there are several solutions to monitor the network malfunctions: HP Openview, Solarwinds, PRTG and other solutions (open source or not).

Generally, when an alert/warning is detected an event is triggered: email, SMS or text to a monitor. But why don’t you send these messages via whatsapp? It’s free and can reach everyone, everywhere!

What you need:

  • PHP with openssl extension enabled
  • WART
  • WhatsAPI-Official
  • SIM

In this tutorial, I use XAMPP Portable version 1.8.3 (http://sourceforge.net/projects/xampp/) on a Windows7 64bit machine.

Note: The WhatsAPI-Official requires openssl extension enabled in the PHP settings; whitout this library the script cannot work!

Let’s start!

 

1. Retrieve your Whatsapp password

Download the “WART“, decompress it and execute the .exe file.

Ciscozine-wart-whatsapp

In the “Phone number” field insert your number; the number must contain the country code without the ’00’ or the ‘+’. For instance, for an italian number insert something like this “39xxxxxxxxxx’.

Click “Request Code“; you will receive a registration code in the format “yyy-yyy”. If for some reasons you will request the code again, you will receive a call with the registration code.

Insert the number received in the “Code” field without the ‘-‘ and click “Confirm Code”. A dialog box appears showing you the Whatsapp Password; save it!

Note: You can retrieve the password through the “registerTool.php” (script included in WhatsAPI-Official). The result is the same!

Remember: When you retrieve the new password for your SIM, the client on your smartphone will be disconnected!

 

2. Install WhatsAPI

Download the WhatsAPI-Official PHP class and decompress the archive (in my case into “E:\Portable\xampp\htdocs\WhatsAPI\”).

 

2a. Send a WhatsApp message to a list of numbers

Save this code into “send-message.php” (in my case saved in “E:\Portable\xampp\htdocs\WhatsAPI\examples\ folder”).

<?php
// Set to true, to see debug mode
$debug = false;

if (!$debug){
 header("Content-type: text/plain");
}

set_time_limit(0);

if (isset($_GET['msg']) || isset($argv[1]) ){
 $message=@$_GET['msg'];
 $message.=@$_SERVER['argv'][1];
}
else{
 exit('YOU MUST DEFINE A MESSAGE'); 
}

//Change to your time zone
date_default_timezone_set('Europe/Rome');


###############YOUR SETTINGS###############
// Telephone number including the country code without '+' or '00'
$username = "yyxxxxxxxxxx";
// Password retrieved with WART software 
$password = "XXXXXXXXXXXXXXXXXXXXXXXXXX=";
// This is nickname displayed by WhatsApp clients
$nickname = "Ciscozine";
// Destination telephone number including the country code without '+' or '00'
$target = array("yyxxxxxxxxxx","yyxxxxxxxxxx");
//Define the absolute path where the whatsprot.class.php file is.
$whatsapi_class_path="E:/Portable/xampp/htdocs/WhatsAPI/src/";
##########################################

require_once $whatsapi_class_path."whatsprot.class.php";

// Create a instance of WhastAPI
$w = new WhatsProt($username, $nickname, $debug);
// Connect to WhatsApp network
$w->connect(); 
// logging in with the password you got
$w->loginWithPassword($password); 

echo "MESSAGE: $message\n\n";
$i=0;
while($i < count($target)) {
 echo "TO: $target[$i]\n";
 $w->sendMessage($target[$i] , $message);
 $i++; 
}
?>

Change the variables:

  • $whatsapi_class_path
  • $username
  • $password
  • $nickname
  • $target

Now it is possible to send the message using one of these methods:

  1. Via Browser, calling the web page:
    http://localhost/WhatsAPI/examples/send-message.php?msg=YOUR_MESSAGE
  2. Via CLI, using the command:
    php e:\Portable\xampp\htdocs\WhatsAPI\examples\send-message.php "YOUR_MESSAGE"

If all it’s fine, the numbers in the $target varialbe will receive a message!

 

2b. Send a whatsapp message to a group

Save this code into “send-to-group.php” (in my case in “E:\Portable\xampp\htdocs\WhatsAPI\examples\” folder).

<?php
// Set to true, to see debug mode
$debug = false;

if (!$debug){
 header("Content-type: text/plain");
}

set_time_limit(0);

if (isset($_GET['msg']) || isset($argv[1]) ){
 $message=@$_GET['msg'];
 $message.=@$_SERVER['argv'][1];
}
else{
 exit('YOU MUST DEFINE A MESSAGE'); 
}

//Change to your time zone
date_default_timezone_set('Europe/Rome');


###############YOUR SETTINGS###############
// Telephone number including the country code without '+' or '00'
$username = "yyxxxxxxxxxx";
// Password retrieved with WART software 
$password = "XXXXXXXXXXXXXXXXXXXXXXXXXX=";
// This is nickname displayed by WhatsApp clients
$nickname = "Ciscozine";
// Destination telephone number including the country code without '+' or '00'
$target = array('yyxxxxxxxxxx','yyxxxxxxxxxx');
//Define the absolute path where the whatsprot.class.php file is.
$whatsapi_class_path="E:/Portable/xampp/htdocs/WhatsAPI/src/";
//Group name
$group_name="My test ciscozine group";
//If you want make a new group leave it blank, otherwise insert the group_id
$group_id='';
##########################################

require_once $whatsapi_class_path."whatsprot.class.php";

// Create a instance of WhastAPI
$w = new WhatsProt($username, $nickname, $debug);
// Connect to WhatsApp network
$w->connect(); 
// logging in with the password you got
$w->loginWithPassword($password);

if($group_id==''){
 echo "Group: ".$group_name." created\n";
 $group_id=$w->sendGroupsChatCreate($group_name, $target);
 echo "Group_id: ".$group_id."\n\n";
}

$w->sendMessage($group_id , $message);
echo "Message to group: ";
echo $message;
?>

Change the variables:

  • $whatsapi_class_path
  • $username
  • $password
  • $nickname
  • $target
  • $group_name or, if you have an existing group, $group_id

Now it is possible send a message to the group using one of these methods:

  1. Via Browser, calling the web page
    http://localhost/WhatsAPI/examples/send-to-group.php?msg=YOUR_MESSAGE
  2. Via CLI, using the command:
    php e:\Portable\xampp\htdocs\WhatsAPI\examples\send-to-group.php "YOUR_MESSAGE"

If all it’s ok, the group will receive a message!

 

Other useful functions

To make someone admin of the group (you must be the owner of the group):

$w->sendPromoteParticipants($group_id, $participants);

To demote an user, make an admin a normal user again:

$w->sendDemoteParticipants($group_id, $participants);

To lock the group so anyone can’t change the profile picture or the status of the group:

$w->sendLockGroup($group_id);

To unlock the group:

$w->sendUnlockGroup($group_id);

To remove a group:

$w->sendGroupsLeave($group_id);

To add new users to a group:

$w->sendGroupsParticipantsAdd($group_id, $target);

To view all group_id, define the $debug to true and use the function:

$w->sendGetGroups();

 

I hope everything is clear! If yes, you can integrate your network monitoring tools with these scripts to send alerts during a network fault!

Note: This article is only a starting point, but with this API you can create several useful scripts! For instance, you can make a PHP BOT that can interact with the users, but for that check the “Simple CLI client.php” file!

If you want more info about WhatsAPI, see the official documentation https://github.com/…/WhatsAPI-Documentation.

Summary
Article Name
Send WhatsApp alert during a network fault
Description
A good network engineer must react quickly during a fault. So, why don't you send these alerts via whatsapp? It's free and can reach everyone, everywhere!
Author