venomous0x / WhatsAPI

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

Best way to send image #796

Closed faisalcse closed 10 years ago

faisalcse commented 10 years ago

What is the best way to send image via WhatsAPI or WhatsAPINet because I have tried with WhatsAPINet and it hang in the line of this.PollMessage() and wait for around 10 minutes. If I remove this line, then send image not work. Please advise and help.

mgp25 commented 10 years ago

@faisalcse

$url = "http://example.com/image.jpg";

$w->sendMessageImage($target, $url);
faisalcse commented 10 years ago

Which php file will use?

mgp25 commented 10 years ago

@faisalcse you will always need whatsprot.class.php

 require_once 'whatsprot.class.php';
faisalcse commented 10 years ago

But which pho file I will use for sending image?

faisalcse commented 10 years ago

To send image I am using this way:

<?php set_time_limit(10); require_once 'whatsprot.class.php';

// 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'.

//This function only needed to show how eventmanager works. function onGetProfilePicture($from, $target, $type, $data) { if ($type == "preview") { $filename = "preview_" . $target . ".jpg"; } else { $filename = $target . ".jpg"; } $filename = WhatsProt::PICTURES_FOLDER."/" . $filename; $fp = @fopen($filename, "w"); if ($fp) { fwrite($fp, $data); fclose($fp); } }

//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);

//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);

//send picture $w->sendMessageImage($target, "demo/x3.jpg");

while (1) { $w->pollMessages(); $msgs = $w->getMessages(); foreach ($msgs as $m) {

process inbound messages

    //print($m->NodeString("") . "\n");
}

}

mgp25 commented 10 years ago

@faisalcse do you know php? Have you read the API code? ... Create a php file, e.g: test.php This is an example of a script for sending an image

<?php
 require_once 'whatsprot.class.php';

 $username =  "";
 $password = "";
 $debug = false;
 $identity = "";

$nickname = "";

$target "";
$url = "";                    

$w = new WhatsProt($username, $identity, $nickname, $debug);
$w->connect();
$w->loginWithPassword($password);

$w->sendMessageImage($target, $url);

?>

Yes, you can also use exampleFunctional for testing.

faisalcse commented 10 years ago

I was also trying to send image using Bulk.php because it was using one connection to send but I see it takes time in this line:

while(static::$sendLock) { //wait for server receipt sleep(1); }

Because there is an event bind: $this->wa->eventManager()->bind("onMessageReceivedServer", "WaBulkSender::event_onMessageReceivedServer");

Please advise.

mgp25 commented 10 years ago

@faisalcse the bulk.php is for sending massive messages / broadcast messages. If you want to send an image only, the simplest script is the one i posted above.

faisalcse commented 10 years ago

OK. Can I send to 8 contacts with one connection? Because I will send only image.

mgp25 commented 10 years ago

@faisalcse Yes you can.

$url = "";

$contacts = array("num1","num2","num3","num4","num5","num6","num7","num8");
 foreach ($contacts as $contact) 
{   
    $w->sendMessageImage($contact, $url);
    $w->pollMessages();
 }
faisalcse commented 10 years ago

In the above code you posted, what is $identity?

mgp25 commented 10 years ago

@faisalcse if you have registered a number correctly, for logging in, $identity is not required you can do it this way:

$w = new WhatsProt($username, null, $nickname, $debug);
faisalcse commented 10 years ago

Sometimes, $w->pollMessages(); takes more time. What is the reason and is any way to overcome?

mgp25 commented 10 years ago

@faisalcse You can do something like this... Most of the logic you can use can be found searching on google if you have any doubts...

$j=0;
for($i=0; $i<count($contacts); $i++){
    $w->sendMessageImage($contacts[$i], $url);
    if($j == 4){
        $w->pollMessages();
        $j=0;
    }
    $j++;
}
faisalcse commented 10 years ago

If I do the connect and how long it will be connected because while sending it might be disconnect?

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

faisalcse commented 10 years ago

One more thing, using the above code

$j=0;
for($i=0; $i<count($contacts); $i++){
    $w->sendMessageImage($contacts[$i], $url);
    if($j == 4){
        $w->pollMessages();
        $j=0;
    }
    $j++;
}

If I send 4, I receive only 1. If I do like this:

$j=0;
for($i=0; $i<count($contacts); $i++){
    $w->sendMessageImage($contacts[$i], $url);

        $w->pollMessages();
    }

}

then it comes 4.

mgp25 commented 10 years ago

@faisalcse i really dont know what you want to do. You have the code and you have everything you need to send the image. You can send the image to the targets you want, there is no problem.

faisalcse commented 10 years ago

I follow the whole code that you send. I try to send image to 8 contacts. When I write this below code: $j=0; for($i=0; $i<count($contacts); $i++){ $w->sendMessageImage($contacts[$i], $url); if($j == 8){ $w->pollMessages(); $j=0; } $j++; }

I receive 1 image. When I change it to : $j=0; for($i=0; $i<count($contacts); $i++){ $w->sendMessageImage($contacts[$i], $url); $w->pollMessages(); } } Then I receive 8. My question is: Shall I have to call $w->pollMessages(); after each send?

mgp25 commented 10 years ago

@faisalcse omg.... did you saw the code?

this will only send one image because you code it that way, pollMessages does not affect to send messages, just look the for loop, the problem is there, not pollMessages.

$j=0;
for($i=0; $i $w->sendMessageImage($contacts[$i], $url);
if($j == 8){
$w->pollMessages();
$j=0;
}
$j++;
}

pollMessages only pull from the socket, and place incoming messages in the message queue. You can call pollMessages the times you want.

You should check this: PHP for

faisalcse commented 10 years ago

Yes. Now it is clear. I thought PollMessage() make problem. Thanks for your great advise and help.

faisalcse commented 10 years ago

To Send Text I see there is time, we need this time?

$w->sendMessage($target, "Sent from WhatsApi at " . time());

mgp25 commented 10 years ago

@faisalcse I recommend you to take a look at whatsprot.class.php, in that file you will find all the functions, which params. uses and what returns.

sendMessage only need two params: target and the text message.

$w->sendMessage($target, $message);
echo time(); // this is a function in php

This -> PHP: time

So the text message you are sending is: Sent from WhatsApi at (seconds returned by time()). Just try it and you will see that is working that way.

I seriously recommend you to read the whole API and understand it. I also advise you to check all the php function you dont know that in the php manual or search them in google. That way you will learn better and faster.

faisalcse commented 10 years ago

I know some of the functions but to confirm is there any relation from internally in any methods. That's why. I really grateful to your for your great help. Thank you so much.