Skip to content

Commit

Permalink
Added Simple CLI and fixes to exampleFunctional
Browse files Browse the repository at this point in the history
  • Loading branch information
mgp25 committed Jul 13, 2014
1 parent 7376c66 commit 39c2d43
Showing 1 changed file with 85 additions and 14 deletions.
99 changes: 85 additions & 14 deletions examples/exampleFunctional.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
<?php
set_time_limit(10);
require_once '../src/whatsprot.class.php';
//Change to your time zone
date_default_timezone_set('Europe/Madrid');


// phone number, deviceIdentity, and name.
$options = getopt("d::", array("debug::"));
$debug = (array_key_exists("debug", $options) || array_key_exists("d", $options)) ? true : false;

$username = "**your phone number**"; // Telephone number including the country code without '+' or '00'.
$identity = "**unique ID generated by WhatsApp client**"; // Obtained during registration with this API or using MissVenom (https://github.com/shirioko/MissVenom) to sniff from your phone.
$password = "**server generated whatsapp password**"; // A server generated Password you received from WhatsApp. This can NOT be manually created
$nickname = "**your nickname**"; // This is the username (or nickname) displayed by WhatsApp clients.
$target = "**contact's phone number**"; // Destination telephone number including the country code without '+' or '00'.
########## DO NOT COMMIT THIS FILE WITH YOUR CREDENTIALS ###########
///////////////////////CONFIGURATION///////////////////////
//////////////////////////////////////////////////////////
$username = "**your phone number**"; // Telephone number including the country code without '+' or '00'.
$password = "**server generated whatsapp password**"; // A server generated Password you received from WhatsApp. This can NOT be manually created
$identity = "**unique ID generated by WhatsApp client**"; // Obtained during registration with this API or using MissVenom (https://github.com/shirioko/MissVenom) to sniff from your phone.
$nickname = "**your nickname**"; // This is the username (or nickname) displayed by WhatsApp clients.
$target = "**contact's phone number**"; // Destination telephone number including the country code without '+' or '00'.
$debug = false; // Set this to true, to see debug mode.
///////////////////////////////////////////////////////////

function fgets_u($pStdn)
{
$pArr = array($pStdn);

if (false === ($num_changed_streams = stream_select($pArr, $write = NULL, $except = NULL, 0))) {
print("\$ 001 Socket Error : UNABLE TO WATCH STDIN.\n");

return FALSE;
} elseif ($num_changed_streams > 0) {
return trim(fgets($pStdn, 1024));
}
return null;
}

//This function only needed to show how eventmanager works.
function onGetProfilePicture($from, $target, $type, $data)
Expand All @@ -26,20 +48,39 @@ function onGetProfilePicture($from, $target, $type, $data)
fwrite($fp, $data);
fclose($fp);
}

echo "- Profile picture saved in /".WhatsProt::PICTURES_FOLDER."\n";
}

function onPresenceReceived($username, $from, $type)
{
$dFrom = str_replace(array("@s.whatsapp.net","@g.us"), "", $from);
if($type == "available")
echo "<$dFrom is online>\n\n";
else
echo "<$dFrom is offline>\n\n";
}

echo "[] Logging in as '$nickname' ($username)\n";
//Create the whatsapp object and setup a connection.
$w = new WhatsProt($username, $identity, $nickname, $debug);
$w->connect();

// Now loginWithPassword function sends Nickname and (Available) Presence
$w->loginWithPassword($password);

echo "[*] Connected to WhatsApp\n\n";

//Retrieve large profile picture. Output is in /src/php/pictures/ (you need to bind a function
//to the event onProfilePicture so the script knows what to do.
$w->eventManager()->bind("onGetProfilePicture", "onGetProfilePicture");
$w->sendGetProfilePicture($target, true);

//Print when the user goes online/offline (you need to bind a function to the event onPressence
//so the script knows what to do)
$w->eventManager()->bind("onPresence", "onPresenceReceived");


//update your profile picture
$w->sendSetProfilePicture("demo/venom.jpg");

Expand All @@ -58,7 +99,10 @@ function onGetProfilePicture($from, $target, $type, $data)


// Implemented out queue messages and auto msgid
$w->sendMessage($target, "Sent from WhatsApi at " . time());
$w->sendMessage($target, "Guess the number :)");
$w->sendMessage($target, "Sent from WhatsApi at " . date('H:i'));

$w->pollMessages();

/**
* You can create a ProcessNode class (or whatever name you want) that has a process($node) function
Expand All @@ -68,13 +112,37 @@ function onGetProfilePicture($from, $target, $type, $data)
$pn = new ProcessNode($w, $target);
$w->setNewMessageBind($pn);

echo "\n\nYou can also write and send messages to $target (interactive conversation)\n\n> ";

while (1) {
$w->pollMessages();
$msgs = $w->getMessages();
foreach ($msgs as $m) {
# process inbound messages
//print($m->NodeString("") . "\n");
}
$line = fgets_u(STDIN);
if ($line != "") {
if (strrchr($line, " ")) {
$command = trim(strstr($line, ' ', TRUE));
} else {
$command = $line;
}
//available commands in the interactive conversation [/lastseen, /query]
switch ($command) {
case "/query":
$dst = trim(strstr($line, ' ', FALSE));
echo "[] Interactive conversation with $target:\n";
break;
case "/lastseen":
echo "[] last seen: ";
$w->sendGetRequestLastSeen($target);
break;
default:
$w->sendMessage($target , $line);
break;
}
}
}

/**
Expand All @@ -101,15 +169,18 @@ public function process($node)
$text = $node->getChild('body');
$text = $text->getData();
if ($text && ($text == "5" || trim($text) == "5")) {
$iconfile = "../../tests/Gift.jpgb64";
$fp = fopen($iconfile, "r");
$icon = fread($fp, filesize($iconfile));
fclose($fp);
$this->wp->sendMessageImage($this->target, "https://mms604.whatsapp.net/d11/26/09/8/5/85a13e7812a5e7ad1f8071319d9d1b43.jpg", "hero.jpg", 84712, $icon);
$this->wp->sendMessageImage($this->target, "https://s3.amazonaws.com/f.cl.ly/items/2F3U0A1K2o051q1q1e1G/baby-nailed-it.jpg");
$this->wp->sendMessage($this->target, "Congratulations you guessed the right number!");
} else {
$this->wp->sendMessage($this->target, "I'm sorry, try again!");
}
elseif (ctype_digit($text)) {
if( (int)$text != "5")
$this->wp->sendMessage($this->target, "I'm sorry, try again!");
}
}
$text = $node->getChild('body');
$text = $text->getData();
$notify = $node->getAttribute("notify");

echo "\n- ".$notify.": ".$text." ".date('H:i')."\n";

}
}

0 comments on commit 39c2d43

Please sign in to comment.