lucasvdh/laravelwhatsapp

This package is abandoned and no longer maintained. No replacement package was suggested.

A Laravel wrapper for 'whatsapp/chat-api'

dev-master 2018-02-17 12:56 UTC

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

  1. Include the package in your application
  2. Register the service provider and aliases
  3. Create a listener
  4. Registration
  5. 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.

Setting up a background worker

Configure Supervisor

Events to listen for