venomous0x / WhatsAPI

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

Receiving message multiple times #1045

Open jolle opened 9 years ago

jolle commented 9 years ago

Hi!

I am receiving messages now multiply times.

Example:

<?php
date_default_timezone_set("Europe/Helsinki");
include("config.php");
$w = new WhatsProt($userPhone, $userIdentity, $userName, false);
$w->eventManager()->bind("onGetGroupMessage", "onMessage");
$w->Connect();
$w->LoginWithPassword($password);
while($w->PollMessage());
function onMessage($mynumber, $from_group_jid, $from_user_jid, $id, $type, $time, $name, $body) {
    $userPhone = '358123456789';
    $userIdentity = '012345678901234';
    $userName = 'Username'; 
    $password = 'sniffed-password-here'; 
    $to = "group-id";
    $w = new WhatsProt($userPhone, $userIdentity, $userName, false);
    $w->Connect();
    $w->LoginWithPassword($password);
    if(strpos($body, 'hi') !== false) {
        $w->sendMessage($to, "Hi, " . $name . "!");
    }
}
?>

And it will send two (usually 4) or more messages. Example:

[Me] hi how are you
[Bot] Hi, Me!
[Bot] Hi, Me!
[Bot] Hi, Me!
[Bot] Hi, Me!

I think I am using whole api incorrect, so how should I really do this?

And is there any other way to do this bot, because I need to use cronjob every minute that php script, and it is slow and makes my server slow...

mgp25 commented 9 years ago

Use WhatsAPI Official code: https://github.com/mgp25/WhatsAPI-Official