PHP code example of crenspire / laravel-whatsapp
1. Go to this page and download the library: Download crenspire/laravel-whatsapp library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
crenspire / laravel-whatsapp example snippets
use Crenspire\Whatsapp\Facades\Whatsapp;
// Send a text message
$response = Whatsapp::sendTextMessage('1234567890', 'Hello from Laravel!');
// Send a media message
$response = Whatsapp::sendMediaMessage('1234567890', 'media_id_123', 'image', 'Check this out!');
// Send a template message
$response = Whatsapp::sendTemplateMessage('1234567890', 'hello_world', ['John', 'Doe']);
use Crenspire\Whatsapp\WhatsappService;
$whatsapp = app(WhatsappService::class);
$response = $whatsapp->sendTextMessage('1234567890', 'Hello World!');
Whatsapp::sendTextMessage('1234567890', 'Hello World!');
// Send image with caption
Whatsapp::sendMediaMessage('1234567890', 'media_id_123', 'image', 'Check this out!');
// Send video
Whatsapp::sendMediaMessage('1234567890', 'media_id_456', 'video', 'Watch this video');
// Send document
Whatsapp::sendMediaMessage('1234567890', 'media_id_789', 'document', 'Important document');
// Send template with parameters
Whatsapp::sendTemplateMessage(
'1234567890',
'hello_world',
['John', 'Doe'],
'en_US'
);
$buttons = [
['id' => 'btn1', 'title' => 'Option 1'],
['id' => 'btn2', 'title' => 'Option 2'],
['id' => 'btn3', 'title' => 'Option 3']
];
Whatsapp::sendButtonMessage(
'1234567890',
'Choose an option:',
$buttons,
'Header Text',
'Footer Text'
);
$sections = [
[
'title' => 'Category 1',
'rows' => [
[
'id' => 'row1',
'title' => 'Option 1',
'description' => 'Description for option 1'
],
[
'id' => 'row2',
'title' => 'Option 2',
'description' => 'Description for option 2'
]
]
]
];
Whatsapp::sendListMessage(
'1234567890',
'Choose from the list:',
'View Options',
$sections,
'Header Text',
'Footer Text'
);
$response = Whatsapp::uploadMedia('/path/to/image.jpg', 'image');
$mediaId = $response['id'];
// Use the media ID to send the image
Whatsapp::sendMediaMessage('1234567890', $mediaId, 'image', 'Uploaded image');
$filePath = Whatsapp::downloadMedia('media_id_123');
// File is saved to storage/app/whatsapp-media/media_id_123.jpg
// In config/whatsapp.php
'tenants' => [
'company1' => [
'phone_number_id' => 'phone_number_1',
'access_token' => 'access_token_1'
],
'company2' => [
'phone_number_id' => 'phone_number_2',
'access_token' => 'access_token_2'
]
]
// Send message using specific tenant
Whatsapp::sendTextMessage('1234567890', 'Hello!', 'company1');
// In your EventServiceProvider
protected $listen = [
\Crenspire\Whatsapp\Events\MessageSent::class => [
// Your listener
],
\Crenspire\Whatsapp\Events\MessageDelivered::class => [
// Your listener
],
\Crenspire\Whatsapp\Events\MessageRead::class => [
// Your listener
],
\Crenspire\Whatsapp\Events\MessageReceived::class => [
// Your listener
],
\Crenspire\Whatsapp\Events\MessageFailed::class => [
// Your listener
],
];
namespace App\Listeners;
use Crenspire\Whatsapp\Events\MessageReceived;
use Illuminate\Contracts\Queue\ShouldQueue;
class HandleIncomingMessage implements ShouldQueue
{
public function handle(MessageReceived $event)
{
// Handle incoming message
$messageId = $event->messageId;
$from = $event->from;
$message = $event->message;
$timestamp = $event->timestamp;
// Your logic here
}
}
use Crenspire\Whatsapp\Exceptions\WhatsappException;
try {
Whatsapp::sendTextMessage('1234567890', 'Hello!');
} catch (WhatsappException $e) {
// Handle WhatsApp API errors
logger('WhatsApp error: ' . $e->getMessage());
}
// Configure rate limit in config/whatsapp.php
'rate_limit' => 30, // messages per minute
// In config/whatsapp.php
'debug' => true,
// Using custom headers with individual requests
$customHeaders = [
'X-Request-ID' => 'req_12345',
'X-Client-Version' => '2.0.0',
'X-Custom-Auth' => 'custom_token'
];
Whatsapp::sendTextMessage('+1234567890', 'Hello!', null, $customHeaders);
// Using custom headers with media upload
Whatsapp::uploadMedia('/path/to/file.jpg', 'image', null, $customHeaders);
// Using custom headers with media download
$filePath = Whatsapp::downloadMedia('media_id_123', null, $customHeaders);
// config/whatsapp.php
'tenants' => [
'tenant1' => [
'phone_number_id' => '123456789',
'access_token' => 'token1',
'headers' => [
'X-Tenant-ID' => 'tenant1',
'X-API-Version' => 'v2.0',
'X-Custom-Header' => 'tenant1-value'
]
],
'tenant2' => [
'phone_number_id' => '987654321',
'access_token' => 'token2',
'headers' => [
'X-Tenant-ID' => 'tenant2',
'X-API-Version' => 'v1.5'
]
]
]
// config/whatsapp.php
'default_headers' => [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'User-Agent' => 'Laravel-WhatsApp-Package/1.0.0',
'X-Client-Name' => 'MyApp',
'X-Client-Version' => '1.0.0'
],
// Template builder
$template = Whatsapp::template('welcome_template', 'en_US')
->header([['type' => 'text', 'text' => 'Welcome!']])
->body([['type' => 'text', 'text' => 'Hello {{1}}!']])
->footer([['type' => 'text', 'text' => 'Thank you']])
->build();
// Message builder
$message = Whatsapp::message()::text('Hello with preview!', true);
// Get business profile
$profile = Whatsapp::getBusinessProfile();
// Update business profile
Whatsapp::updateBusinessProfile([
'messaging_product' => 'whatsapp',
'about' => 'Updated business description'
]);
// Get media information
$info = Whatsapp::getMediaInfo('media_id_123');
// Delete media
Whatsapp::deleteMedia('media_id_123');
bash
php artisan vendor:publish --tag=config --provider="Crenspire\\Whatsapp\\WhatsappServiceProvider"