Send WhatsApp Messages In Laravel can be accomplished by incorporating a third-party platform like Twilio, which offers APIs for sending WhatsApp messages.
How To Send WhatsApp Messages In Laravel
Twilio provides a flexible API that allows developers to incorporate voice, messaging, and video capabilities into applications. With support for multiple programming languages, Twilio’s API enables easy integration of features like sending SMS, making voice calls, and implementing two-factor authentication. This empowers businesses to improve customer engagement and communication, offering a reliable solution for creating innovative and interactive applications across various industries.
For Send WhatsApp Messages In Laravel we will use third-party package “twilio/sdk” to send message on end user. This package provide API to send Media and message on user number programaticaly.
We will use following step to send whatsApp message in Laravel with twilio/sdk
Step for Send WhatsApp Messages In Laravel API Integration
- Step 1: Install Laravel
- Step 2: Install twilio/sdk Package
- Step 3: Set up a Twilio Account
- Step 4: Configure Environment Variables
- Step 5: Create Route
- Step 6: Create Controller
- Step 7: Create Blade File
- Run Laravel App
let’s follow bellow steps:
Step -1 : Install Laravel
This step is not required if you have already create a laravel project. Other wise execute the below command:
composer create-project laravel/laravel example-app
Step 2: Install twilio/sdk Package
First, you need to install the Twilio SDK for PHP using Composer. Run the following command in your Laravel project directory:
composer require twilio/sdk
Step 3: Set up a Twilio Account
1. Initially, you must create and input a phone number. Subsequently, you will be able to obtain the account SID, Token, and Number.
Create Account from here: www.twilio.com.
2. Begin by creating and adding a phone number. Afterward, you will have access to the account SID, Token, and Number.
Step 4: Configure Environment Variables
Add your Twilio credentials to your .env
file:
TWILIO_SID=your_twilio_sid
TWILIO_AUTH_TOKEN=your_twilio_auth_token
TWILIO_WHATSAPP_NUMBER=your_twilio_whatsapp_number
Step 5: Create Route
Define a route for sending messages in routes/web.php
routes/web.php
// routes/web.php
use App\Http\Controllers\WhatsAppController;
Route::post('/send-whatsapp', [WhatsAppController::class, 'send']);
Step 6: Create Controller
In this stage, we are going to establish WhatsAppController and develop the send SMS functionality. Therefore, we should include a new route in the web.php file as shown below:
app/Http/Controllers/WhatsAppController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Twilio\Rest\Client;
use Exception;
class WhatsAppController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
return view('whatsapp');
}
/**
* Write code on Method
*
* @return response()
*/
public function send(Request $request)
{
$twilioSid = env('TWILIO_SID');
$twilioToken = env('TWILIO_AUTH_TOKEN');
$twilioWhatsAppNumber = env('TWILIO_WHATSAPP_NUMBER');
$recipientNumber = $request->phone;
$message = $request->message;
try {
$twilio = new Client($twilioSid, $twilioToken);
$twilio->messages->create(
$recipientNumber,
[
"from" => "whatsapp:+". $twilioWhatsAppNumber,
"body" => $message,
]
);
return back()->with(['success' => 'WhatsApp message sent successfully!']);
} catch (Exception $e) {
return back()->with(['error' => $e->getMessage()]);
}
}
}
Step-7 : Create Blade file
Here, we will create “whatsapp.blade.php” file with following code:
resources/views/whatsapp.blade.php
<form method="POST" action="{{ url('send-whatsapp') }}">
{{ csrf_field() }}
@if ($message = Session::get('success'))
<div class="alert alert-success alert-block">
<strong>{{ $message }}</strong>
</div>
@endif
@if ($message = Session::get('error'))
<div class="alert alert-danger alert-block">
<strong>{{ $message }}</strong>
</div>
@endif
<div class="mb-3">
<label class="form-label" for="inputName">Phone:</label>
<input
type="text"
name="phone"
id="inputName"
class="form-control @error('phone') is-invalid @enderror"
placeholder="Phone Number">
@error('phone')
<span class="text-danger">{{ $message }}</span>
@enderror
</div>
<div class="mb-3">
<label class="form-label" for="inputName">Message:</label>
<textarea
name="message"
id="inputName"
class="form-control @error('message') is-invalid @enderror"
placeholder="Enter Message"></textarea>
@error('message')
<span class="text-danger">{{ $message }}</span>
@enderror
</div>
<div class="mb-3">
<button class="btn btn-success btn-submit">Send Message</button>
</div>
</form>
Step-8 Run Laravel Application
Output
Thanks for reading