venomous0x / WhatsAPI

Interface to WhatsApp Messenger
2.59k stars 2.14k forks source link

How to get the received message and the number and store it in php variable #1198

Open syamsoul opened 8 years ago

syamsoul commented 8 years ago

below is my code... but it is not working

<?php

require_once 'src/whatsprot.class.php'; require_once 'src/events/MyEvents.php'; //require_once 'src/Registration.php';

$username = '60123456789'; // Your number with country code, ie: 34123456789 $nickname = 'Unknown'; // Your nickname, it will appear in push notifications $debug = true; // Shows debug log, this is set to false if not specified $log = true; // Enables log file, this is set to false if not specified $password = "12312312312312312312312";

$w = new WhatsProt($username, $nickname, $debug, $log); $events = new MyEvents($w); $events->setEventsToListenFor($events->activeEvents); $w->eventManager()->bind("onGetMessage", "onMessage");

$w->connect(); $w->loginWithPassword($password);

while (true){ $w->pollMessage(); if ($message_received=="hello") { $target = $message_received_number; $message = 'Hello to you too'; $w->sendMessage($target , $message); } }

?>

s4mpl3d commented 8 years ago

What's the problem here? On Feb 20, 2016 4:50 PM, "matramlee" notifications@github.com wrote:

<?php

require_once 'src/whatsprot.class.php'; require_once 'src/events/MyEvents.php'; //require_once 'src/Registration.php';

$username = '60123456789'; // Your number with country code, ie: 34123456789 $nickname = 'Unknown'; // Your nickname, it will appear in push notifications $debug = true; // Shows debug log, this is set to false if not specified $log = true; // Enables log file, this is set to false if not specified $password = "12312312312312312312312";

$w = new WhatsProt($username, $nickname, $debug, $log); $events = new MyEvents($w); $events->setEventsToListenFor($events->activeEvents); $w->eventManager()->bind("onGetMessage", "onMessage");

$w->connect(); $w->loginWithPassword($password);

while ($bulian){ $w->pollMessage(); if ($message_received=="hello") { $target = $message_received_number; $message = 'Hello to you too'; $w->sendMessage($target_group , $message); } }

?>

— Reply to this email directly or view it on GitHub https://github.com/venomous0x/WhatsAPI/issues/1198.

derN3rd commented 8 years ago

You are trying to bind an event "onGetMessage" to a function called "onMessage" but you did not define the function. The onGetMessage event is defined like this: onGetMessage( $mynumber, $from, $id, $type, $time, $name, $body )

Your code should look like:

<?php

require_once 'src/whatsprot.class.php';
require_once 'src/events/MyEvents.php';
//require_once 'src/Registration.php';

$username = '60123456789'; // Your number with country code, ie: 34123456789
$nickname = 'Unknown'; // Your nickname, it will appear in push notifications
$debug = true; // Shows debug log, this is set to false if not specified
$log = true; // Enables log file, this is set to false if not specified
$password = "12312312312312312312312";

$w = new WhatsProt($username, $nickname, $debug, $log);
$events = new MyEvents($w);
$events->setEventsToListenFor($events->activeEvents); 
$w->eventManager()->bind("onGetMessage", "onMessage");

$w->connect(); 
$w->loginWithPassword($password);

onMessage( $mynumber, $from, $id, $type, $time, $name, $body ){
  $message = 'Hello to you too';
  if ($body=="hello") $w->sendMessage($from , $message);
}

while (true){
$w->pollMessage();
}

?>

Anyway you should have a look here: https://github.com/mgp25/Chat-API/wiki/WhatsAPI-Documentation