The repo which this package relied on is no longer working, sorry. Hopefully whatsapp will bring out their business API soon. If so I'll implement the new API in this laravel package.
This is a Laravel wrapper for the whatsapp/chat-api package
- Include the package in your application
- Register the service provider and aliases
- Create a listener
- Registration
- Sending a message
composer require lucasvdh/laravelwhatsapp:dev-master
Or add a requirement to your project's composer.json
"require": {
"lucasvdh/laravelmacros": "dev-master"
},
Edit the config/app.php
file. Append the following to the providers
array:
'providers' => [
// ...
Lucasvdh\LaravelWhatsapp\WhatsappServiceProvider::class,
// ...
],
Register the aliases:
'aliases' => [
// ...
'Whatsapp' => Lucasvdh\LaravelWhatsapp\Facades\Whatsapp::class,
// ...
],
<?php namespace App\Listeners;
use Lucasvdh\LaravelWhatsapp\Abstracts\Listener as WhatsappListener
class WhatsappEventListener extends WhatsappListener
{
// Check the Lucasvdh\LaravelWhatsapp\Interface\Listener for all events
}
The registration flow.
$phone_number = '31612345678' // Your phone number including country code
$type = 'sms';
// $type = 'voice';
$result = Whatsapp::requestCode($phone_number, $type);
$phone_number = '31612345678' // Your phone number including country code
$code = '123-456'; // The code you've received through sms or voice
try {
$result = Whatsapp::register('$phone_number, $code);
}
catch(Exception $e) {
die('Something went wrong during the registration: ' . $e->getMessage());
}
// Store this password, you'll need this to connect to the network
$password = $result['pw']
$phone_number = '31612345678' // Your phone number including country code
$display_name = 'Foo Bar'; // This is the name that other Whatsapp users will see
$password = '****************************'; // The password you received from the registration process
// Fetch the connection
$connection = Whatsapp::getConnection($phone_number, $display_name);
// Initialize your listener
$listener = new WhatsappEventListener($connection);
// Connect to the network
$connection = Whatsapp::connect($connection, $password, $listener);
// You are now connected
After connecting to the network you can send a message like this
$target = '3112345678'; // The phone number of the person you are sending the message to
$message = 'This is a message';
$message_hash = $connection->sendMessage($target , $message);
I recommend using Supervisor to run an artisan command in the background, but there are other ways to solve this. I've written an example for receiving messages with an artisan command using Supervisor.