There are a number of WhatsAPI forks out there at the moment that allow the user to send messages via Whatsapp with PHP. However all of them are quite cumbersome when it comes to writing the code necessary to make it all work.
A lot of us are now using Laravel in our PHP projects and this project attempts to take the wonderful readability of Laravel’s syntax and apply it to the WhatsApi code.
Laravel does all the grunt work in setting up and maintaining the connection with Whatsapp and also allows you to use multiple accounts if you have them!
Ok ok, wouldn’t it be nice if we could do this in our projects at ANY stage:
$message = "Hello $user->name and welcome to our website"; $user = User::find(1); WA::sendMessage($user->phoneNum, $message);
and have a message instantly sent? Instead of all this:
$user = User::find(1); // Initializing client $localizationService = new LocalizationService(); $phone = new Phone($user->phoneNum); // your phone number with international prefix $localizationService->injectPhoneProperties($phone); $identity = new Identity(); $identity->setNickname($user->nickName); // your name $identity->setIdentityToken($user->token); // your token $identity->setPassword($user->password); // your password $identity->setPhone($phone); $client = new Client($identity); $client->setChallengeDataFilepath(__DIR__ . '/data/nextChallenge.dat'); $client->connect(); $client->login(); $number = ''; // number to send message // Sending composing notification (simulating typing) $client->send(new ActionChatState($number, ActionChatState::STATE_COMPOSING)); $client->send(new ActionChatState($number, ActionChatState::STATE_PAUSED)); $messagetext = "Hello $user->name and welcome to our website" $message = new ActionMessageText($identity->getNickname(), $number); $message->setBody($messageText); $client->send($message);
Installation
- Assuming you already have composer installed on your system – install a new Laravel Project into
whatsapidemoproject
folder:composer create-project laravel/laravel whatsapidemoproject --prefer-dist
- Ensure that you set your webserver to use
whatsapidemoproject/public
as it’s webroot. If you visit http://localhost (or whatever domain name you are using) you should now see a welcome to laravel message. If you have problems, check out the official documents on the Laravel Home Page. A quick alternative is to use thephp artisan serve
command - Now change into your new
whatsapiprojectdemoproject
foldercd whatsapiprojectdemo
- Require the needed packages (enter each of these lines as a command).
composer require mgp25/whatsapi:@dev composer require williamson/larawhatsapi:dev-master --update-no-dev
- Now we need to create the config file that will allow the user to very easily add all their numbers/names/password/identities.
php artisan config:publish williamson/larawhatsapi
- Finally, we need to tell Laravel that there is a LaraWhatsApi ServiceProvider to be used when Laravel starts up. In the project folder, edit the
app/config/app.php
file. Find the end of the providers array, it should look like this:'IlluminateValidationValidationServiceProvider', 'IlluminateViewViewServiceProvider', 'IlluminateWorkbenchWorkbenchServiceProvider'
and add the last line to make it look like this:
'IlluminateValidationValidationServiceProvider', 'IlluminateViewViewServiceProvider', 'IlluminateWorkbenchWorkbenchServiceProvider', 'WilliamsonLarawhatsapiLaraWhatsapiServiceProvider'
Configuration
Now everything has been installed, you just need to add your Whatsapp account details into the config file.
There will now be a personal config file created for you in whatsapidemoproject/app/config/packages/williamson/larawhatsapi
.
Open this file and edit the details with your account info.
Don’t forget that nextChallengeDir
MUST be writable by the webserver otherwise you will get errors.
Once saved, you’re good to use the API!
Which Fork?
In the config file you will see an option for fork. There are two leading php whatsapi forks out there at the moment, WhatsAPI-Official and TmvWhatsApi. Both of them have pros and cons.
WhatsAPI-Official gets updated more often, but the code is very messy and hard to maintain.
TmvWhatsApi doesn’t get updated as often, but the code is very well structured and laid out. As time progresses and more features are added, I believe this will be a solid foundation to use for a php version of whatsapi.
However the choice is yours, by changing the fork option in your config file, you will be able to pick which library is being used in the background to make all the magic happen.
Usage
Lets make a simple example.
Open, the routes file whatsapidemoproject/app/routes.php
. Lets add a new route:
Route::get("demo", function()
{
echo "I'm sending a message via Whatsapi!";
WA::sendMessage('353xxxxxxxx', "My boring Hello World Text!");
});
Obviously edit the phone number to one you wish to receive the message on. Save the file. We’re good to go!
You can now access the demo and send the message simply by visiting: http://localhost/demo
i’m getting below while updating composer
Your requirements could not be resolved to an installable set of packages.
Problem 1
– Installation request for williamson/larawhatsapi dev-master -> satisfiable by williamson/larawhatsapi[dev-master].
– williamson/larawhatsapi dev-master requires thomasvargiu/tmv-whatsapi 0.* -> no matching package found.
Potential causes:
– A typo in the package name
– The package is not available in a stable-enough version according to your minimum-stability setting
see for more details.
Read for further common problems.
your laravel version ?
5.1.11
i will be contacting package author ! i guess there will be solution ASAP
thanks for your help.. thrilled to know the process for making it work with laravel..
no problem , i will forward your request to my team !
Hi, Deven. Did you have now a solution for the missing typo? I mean, in my country Whatsapp it is the “standard” for this scenario…
soory bad news from author This package is abandoned and no longer maintained.
Use Telegram and the proper BOT API as a much better alternative.
thanks
This not working with laravel 5.1 :(
can you help me how to send a message to group whatapps? :D
Yeah . sure
https://github.com/xaamin/whatsapi
Hola estoy tratando de implementar en laravel 5.8 saben si aun funciona el envio.??