lucasvdh / laravelwhatsapp
A Laravel wrapper for 'whatsapp/chat-api'
Requires
- php: >=5.3.0
- laravel/framework: 5.*
- whatsapp/chat-api: 3.*
This package is auto-updated.
Last update: 2020-02-06 14:04:30 UTC
README
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.
Laravel Whatsapp
This is a Laravel wrapper for the whatsapp/chat-api package
Getting started
- Include the package in your application
- Register the service provider and aliases
- Create a listener
- Registration
- Sending a message
Include the package in your application
composer require lucasvdh/laravelwhatsapp:dev-master
Or add a requirement to your project's composer.json
"require": { "lucasvdh/laravelmacros": "dev-master" },
Register the service provider and aliases
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, // ... ],
Create a listener
<?php namespace App\Listeners; use Lucasvdh\LaravelWhatsapp\Abstracts\Listener as WhatsappListener class WhatsappEventListener extends WhatsappListener { // Check the Lucasvdh\LaravelWhatsapp\Interface\Listener for all events }
Registration
The registration flow.
Request a code
$phone_number = '31612345678' // Your phone number including country code $type = 'sms'; // $type = 'voice'; $result = Whatsapp::requestCode($phone_number, $type);
Verify the registration code
$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']
Connecting to the network
$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
Sending a message
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);
Receiving messages
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.