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.
Once you have done that, you can check out the current structure of the project. There is also a file called EXAMPLES.php that shows a few more examples.
I also prepared a few small scripts that you can use as a basis to make your own scripts:
To download my two minimal examples, run the following commands, and edit the file whatsapp_whatsapi_config.php to set your own user credentials:

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:
The script includes the configuration for your WhatsApp username, password and display name. It’s very easy to use and quite self-explanatory: The WhatsProt class is the only thing you need. Simple Connect to the WhatsApp servers and LoginWithPassword to authenticate yourself. After that, you can use the following methods:
  • 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.
The tiny script from above obviously only sends plain text messages. You can use it from the command line like this:
The script is particularly useful as a WhatsApp notifier, allowing you to receive notifications from your servers whenever you want — for example, if the CPU temperature rises above a certain threshold, the load is too high for a certain amount of time or one of your scripts failed/succeeded. This is particularly interesting in combination with a system monitoring service such as Nagios or Monit.

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:
Each WhatsApp message has a set of standard attributes ($m->_attributeHash) such as from (sender number) or t (send timestamp). Additionally, it has different kind of child nodes that contain additional/optional information, depending on what type of message it is: a notify child node, for instance, tells the conversation partner that he or she is online and still writing, and the body child node contains the text contents. There are many more of these. You can see for yourself by calling print_r($msgs).
The following snippet shows an excerpt of one message — refer to this example output to see more:
My example server script whatsapp_whatsapi_listen.php extends the above snippet and processes the messages like this: It takes the time (t) and sender number (from) from $m->_attributeHash and the name and _data from the child nodes. Each non-empty message is printed to STDOUT, like this:
If the message body is “exit”, the script exits.
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

Postagens mais visitadas deste blog

Rails CanCan

Meus insights mais valiosos sobre criptomoedas para 2018 e além

O Melhor de 2012 para Designers