WhatsApp Integration with Laravel using Twilio API | websolutioncode.com
WhatsApp Integration with Laravel using Twilio API | websolutioncode.com

WhatsApp Integration with Laravel using Twilio API

WhatsApp has become an integral part of communication for businesses. WhatsApp Integration with Laravel using Twilio API can streamline messaging processes. Twilio simplifies sending messages via WhatsApp, allowing developers to leverage its powerful API within Laravel.

Setting up Twilio and Laravel

Step 1: Sign up for Twilio and get API credentials

First, create a Twilio account if you haven’t already. Upon signing in, access the dashboard and note down the Account SID and Auth Token. Acquire a Twilio phone number for sending WhatsApp messages.

Step 2: Laravel Installation

If you haven’t set up a Laravel project, install Laravel using Composer:

Install Twilio PHP SDK

Use Composer to install the Twilio PHP SDK:

composer require twilio/sdk

Laravel Configuration

Configuration Files

Add your Twilio credentials in the .env file:

TWILIO_SID=your_twilio_account_sid
TWILIO_AUTH_TOKEN=your_twilio_auth_token
TWILIO_WHATSAPP_NUMBER=your_twilio_whatsapp_number

Twilio Configuration

Create a Twilio service provider to encapsulate the Twilio setup:

php artisan make:provider TwilioServiceProvider

Open the generated TwilioServiceProvider.php and add the following code:

use Illuminate\Support\ServiceProvider;
use Twilio\Rest\Client;

class TwilioServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->singleton('twilio', function ($app) {
            return new Client(
                config('services.twilio.sid'),
                config('services.twilio.token')
            );
        });
    }
}

Register TwilioServiceProvider

Include the Twilio service provider in config/app.php:

'providers' => [
    // Other Providers
    App\Providers\TwilioServiceProvider::class,
],

Sending WhatsApp Messages

Create a Controller

Generate a controller to handle WhatsApp message sending:

php artisan make:controller WhatsAppController

Implement the message sending logic in the WhatsAppController.php:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Twilio\Rest\Client;

class WhatsAppController extends Controller
{
    public function sendMessage(Request $request)
    {
        $twilio = app('twilio');

        $message = $twilio->messages
            ->create(
                "whatsapp:" . $request->input('to_number'),
                [
                    "from" => "whatsapp:" . config('services.twilio.whatsapp_number'),
                    "body" => $request->input('message')
                ]
            );

        return response()->json(['message_id' => $message->sid]);
    }
}

Routes Setup

Define the route to invoke the sendMessage method in routes/web.php:

use App\Http\Controllers\WhatsAppController;

Route::post('/send-whatsapp', [WhatsAppController::class, 'sendMessage']);

Usage

Now, you can send WhatsApp messages using an HTTP POST request to /send-whatsapp endpoint with parameters to_number and message.

Example using CURL:

curl -X POST http://websolutioncode.com/send-whatsapp -d "to_number=whatsapp:+1234567890" -d "message=Hello from Twilio!"

Conclusion

Integrating Twilio’s API with Laravel simplifies sending WhatsApp messages, empowering businesses to enhance their communication strategies. With a few setup steps and code implementations, leveraging WhatsApp as a messaging platform becomes feasible within the Laravel framework.

Check our tools website Word count
Check our tools website check More tutorial

Leave a Reply