Enviando Mensagem pelo Whatsapp usando PHP via WhatsappAPI
I recently discovered that once you have acquired your WhatsApp account password, it’s relatively easy to send and receive WhatsApp messages via PHP. Using the PHP-based framework WhatsAPI, a simple WhatsApp notifier script only has a dozen lines of code.
This tiny tutorial shows how to use the two very basic functions of WhatsAPI, namely to send simple outgoing messages to any number and to listen for new incoming messages from your own WhatsApp account. This is the second part of a two-part tutorial. The first part demonstrated how to sniff the WhatsApp password from your Android phone or iPhone.
Contents
1. Get your WhatsApp password
This little demonstration only works if you have already obtained your WhatsApp password. If you have not and have no idea how to do it, please check out the first part of this tutorial.2. Get WhatsAPI and send/receive messages
Assuming you have your WhatsApp password at hand, let’s see how easy the usage of WhatsAPI is.2.1. Download WhatsAPI and test scripts
Downloading WhatsAPI is really simply since it is hosted on Github. Simply make a new directory and retrieve WhatsAPI from Github.
1
2
3
4
|
mkdir whatsapp
cd whatsapp
sudo apt-get install git
git clone https://github.com/venomous0x/WhatsAPI
|
I also prepared a few small scripts that you can use as a basis to make your own scripts:
- whatsapp_whatsapi_send.php is a command line script to send any strings to a given number.
- whatsapp_whatsapi_listen.php listens for incoming messages and outputs them to STDOUT.
- whatsapp_whatsapi_config.php defines the configuration (source/destination numbers and WhatsApp password) for the send/listen scripts
- whatsapp_whatsapi_example_messages.txt shows the structure of a few WhatsApp messages (print_r($msgs) output).
1
2
3
4
5
6
|
wget -O whatsapp_whatsapi_send.php http://blog.philippheckel.com/uploads/2013/07/whatsapp_whatsapi_send.php.txt
wget -O whatsapp_whatsapi_listen.php http://blog.philippheckel.com/uploads/2013/07/whatsapp_whatsapi_listen.php.txt
wget -O whatsapp_whatsapi_config.php http://blog.philippheckel.com/uploads/2013/07/whatsapp_whatsapi_config.php.txt
wget -O whatsapp_whatsapi_example_messages.txt http://blog.philippheckel.com/uploads/2013/07/whatsapp_whatsapi_example_messages.txt
chmod +x *.php
vi whatsapp_whatsapi_config.php
|
2.2. Send WhatsApp messages
As you might know from your smartphone client, you can send different kind of messages through WhatsApp: Besides text, you can send audio and video files, locations and contacts. WhatsAPI can do all of those things in just one line of code.My simple sample script whatsapp_whatsapi_send.php just shows how to send a regular text message. The script is meant to be called by the command line, but the code can also be used in a web application:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#!/usr/bin/php
<?php
require_once('whatsapp_whatsapi_config.php');
$destinationPhone = '495553333333';
$w = new WhatsProt($userPhone, $userIdentity, $userName, $debug);
$w->Connect();
$w->LoginWithPassword($password);
$w->Message($destinationPhone, $argv[1]);
?>
|
- Message($to, $msg): Simply send a regular text message to $to.
- MessageImage($to, $imageURI): Send images by URL or local path (jpg) to $to.
- MessageVideo($to, $videoURI): Send videos by URL or local path (mp4) to $to.
- MessageAudio($to, $audioURI): Send audios by URL or local path (mp3) to $to.
- Location($to, $lng, $lat): Send GPS coordinates to $to
- vCard($to, $vCardName, $vCard): Send a vCard to $to.
- WaitForReceipt(): Wait for the WhatsApp servers to confirm the delivery.
1
|
./whatsapp_whatsapi_send.php "Warning: CPU temperature at 65°C"
|
2.3. Receive WhatsApp messages
To be able to receive WhatsApp messages using PHP, you need to listen for new messages. WhatsAPI’s PollMessages does exactly that. It reads messages from the WhatsApp server socket and puts them in a local queue for processing. The method blocks if there are no messages and waits for the server to send a message indefinitely — just like any other server does. Using GetMessages you can pull the messages from the queue and process them in your application.A minimal script would look very similar to the example from above, except that instead of calling Message(), you need to call PollMessages() and GetMessages() in a server loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?php
require_once('whatsapp_whatsapi_config.php');
$w = new WhatsProt($userPhone, $userIdentity, $userName, $debug);
$w->Connect();
$w->LoginWithPassword($password);
while (true) {
$w->PollMessages();
$msgs = $w->GetMessages();
// Do something with the messages ...
}
?>
|
The following snippet shows an excerpt of one message — refer to this example output to see more:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
ProtocolNode Object
(
[_tag] => message
[_attributeHash] => Array
(
[from] => 491231234567@s.whatsapp.net
[id] => 1373204559-6
[type] => chat
[t] => 1373205620
)
[_children] => Array
(
...
[2] => ProtocolNode Object
(
[_tag] => body
...
[_children] =>
[_data] => Hallo blog readers
)
)
[_data] =>
)
|
1
2
3
4
|
./whatsapp_whatsapi_listen.php
[07/07/2013 15:57] From: 491231234567, Name: Philipp, Message: Hallo blog readers
[07/07/2013 15:57] From: 491231234567, Name: Philipp, Message: Everything I write is printed to STDOUT
[07/07/2013 15:57] From: 491231234567, Name: Philipp, Message: Exit
|
That’s it. I hope this tutorial helped a little in understanding how WhatsAPI works. If you have any suggestions or questions, please let me know in the comments.
Comentários
Postar um comentário