Closed faisalcse closed 10 years ago
@faisalcse
$url = "http://example.com/image.jpg";
$w->sendMessageImage($target, $url);
Which php file will use?
@faisalcse you will always need whatsprot.class.php
require_once 'whatsprot.class.php';
But which pho file I will use for sending image?
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) {
//print($m->NodeString("") . "\n");
}
}
@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.
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.
@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.
OK. Can I send to 8 contacts with one connection? Because I will send only image.
@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();
}
In the above code you posted, what is $identity?
@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);
Sometimes, $w->pollMessages(); takes more time. What is the reason and is any way to overcome?
@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++;
}
If I do the connect and how long it will be connected because while sending it might be disconnect?
$w->connect(); $w->loginWithPassword($password);
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.
@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.
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?
@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
Yes. Now it is clear. I thought PollMessage() make problem. Thanks for your great advise and help.
To Send Text I see there is time, we need this time?
$w->sendMessage($target, "Sent from WhatsApi at " . time());
@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.
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.
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.