How to integrate WhatsApp Business API with Laravel step by step
Integrating WhatsApp Business API with Laravel: A Step-by-Step Guide
In this article, we will explore the process of integrating the WhatsApp Business API with Laravel, a popular PHP web framework. We will cover the necessary steps, including setting up the WhatsApp Business API, creating a Laravel project, and implementing webhooks to receive messages from WhatsApp.
Prerequisites
To follow along with this guide, you will need the following:
- Laravel installed on your local machine
- A WhatsApp Business API account
- A verified business phone number
- A reliable internet connection
Step 1: Set up the WhatsApp Business API
To get started, you need to set up a WhatsApp Business API account. Follow these steps:
- Go to the WhatsApp Business API website and sign up for an account.
- Verify your business phone number by following the instructions provided.
- Once your phone number is verified, you will receive a WhatsApp Business API token. This token will be used to authenticate your API requests.
Step 2: Create a Laravel Project
Next, create a new Laravel project using the following command:
composer create-project --prefer-dist laravel/laravel whatsapp-api
Once the project is created, navigate to the project directory and install the required dependencies using the following command:
composer install
Step 3: Configure the WhatsApp Business API in Laravel
To integrate the WhatsApp Business API with Laravel, you need to create a new service provider. Create a new file called `WhatsAppServiceProvider.php` in the `app/Providers` directory:
// app/Providers/WhatsAppServiceProvider.php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use WhatsApp\WhatsApp;
class WhatsAppServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind('whatsapp', function ($app) {
return new WhatsApp($app['config']['whatsapp.token']);
});
}
}
Next, add the following code to the `config/app.php` file to register the service provider:
// config/app.php
'providers' => [
// ...
App\Providers\WhatsAppServiceProvider::class,
],
Step 4: Implement Webhooks to Receive Messages from WhatsApp
Webhooks are a way for WhatsApp to notify your application when a message is received. To implement webhooks, you need to create a new route in Laravel. Create a new file called `web.php` in the `routes` directory:
// routes/web.php
Route::post('/webhook', 'WhatsAppController@handleWebhook');
Next, create a new controller called `WhatsAppController.php` in the `app/Http/Controllers` directory:
// app/Http/Controllers/WhatsAppController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use WhatsApp\WhatsApp;
class WhatsAppController extends Controller
{
public function handleWebhook(Request $request)
{
$whatsapp = app('whatsapp');
$message = $whatsapp->getMessageFromWebhook($request->all());
// Process the message here
return response()->json(['message' => 'Webhook received successfully']);
}
}
Step 5: Test the Integration
To test the integration, send a message to your WhatsApp Business API phone number. The message should trigger the webhook, and your application should receive the message.
Conclusion
In this article, we explored the process of integrating the WhatsApp Business API with Laravel. We covered the necessary steps, including setting up the WhatsApp Business API, creating a Laravel project, and implementing webhooks to receive messages from WhatsApp. With this integration, you can build applications that interact with WhatsApp users and provide a seamless experience.
References
For more information on the WhatsApp Business API, please refer to the official documentation: