PHP code example of irwan-runtuwene / driver-whatsapp

1. Go to this page and download the library: Download irwan-runtuwene/driver-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/ */

    

irwan-runtuwene / driver-whatsapp example snippets


$config = [
    'whatsapp' => [
        'api_version' => 'v21.0',
        'phone_number_id' => 'your_phone_number_id',
        'access_token' => 'your_access_token',
        'verify_token' => 'your_verify_token',
        'throw_http_exceptions' => true,
    ]
];

$botman = BotManFactory::create($config);

// This may not work consistently
$messageId = $message->getPayload()->get('id');

// Reliable for Cloud API
$payload = $message->getPayload();
$messageId = $payload->get('messages')[0]['id'] ?? null;

// Or from the event directly
$messageId = $message->getPayload()->get('entry')[0]['changes'][0]['value']['messages'][0]['id'] ?? null;

// Media URLs were directly accessible
$mediaUrl = $message->getPayload()->get('image')['url'];

// Media  $message->getPayload()->get('media');
if ($media) {
    $driver = $bot->getDriver();
    
    // Step 1: Get media URL
    $mediaInfo = $driver->getMediaUrl($media['id']);
    
    // Step 2: Download media
    $driver->downloadMedia($mediaInfo['url'], $savePath);
}

use BotMan\Drivers\Whatsapp\Extensions\ButtonTemplate;
use BotMan\Drivers\Whatsapp\Extensions\ElementButton;

$template = ButtonTemplate::create('Choose an option:')
    ->addButton(ElementButton::create('option_1', 'Option 1'))
    ->addButton(ElementButton::create('option_2', 'Option 2'));

$bot->reply($template);

use BotMan\Drivers\Whatsapp\Extensions\ListTemplate;
use BotMan\Drivers\Whatsapp\Extensions\ListSection;
use BotMan\Drivers\Whatsapp\Extensions\ListRow;

$section = ListSection::create('Products')
    ->addRow(ListRow::create('prod_1', 'Product 1', 'Description'));

$template = ListTemplate::create('Our Products', 'View Catalog')
    ->addSection($section);

$bot->reply($template);

// Limited or no template support

use BotMan\Drivers\Whatsapp\Extensions\TemplateMessage;

$template = TemplateMessage::create('order_confirmation', 'en_US')
    ->addBodyParameters(['John Doe', 'ORD-12345', '$99.99']);

$bot->reply($template);

$botman->hears('hello', function ($bot) {
    $bot->reply('Hello! How can I help you?');
});

use BotMan\Drivers\Whatsapp\Extensions\ButtonTemplate;
use BotMan\Drivers\Whatsapp\Extensions\ElementButton;

$botman->hears('menu', function ($bot) {
    $template = ButtonTemplate::create('Choose an option:')
        ->addButton(ElementButton::create('option_1', 'Option 1'))
        ->addButton(ElementButton::create('option_2', 'Option 2'))
        ->addButton(ElementButton::create('option_3', 'Option 3'));
    
    $bot->reply($template);
});

use BotMan\Drivers\Whatsapp\Extensions\ListTemplate;
use BotMan\Drivers\Whatsapp\Extensions\ListSection;
use BotMan\Drivers\Whatsapp\Extensions\ListRow;

$botman->hears('catalog', function ($bot) {
    $section = ListSection::create('Products')
        ->addRow(ListRow::create('prod_1', 'Product 1', 'Description 1'))
        ->addRow(ListRow::create('prod_2', 'Product 2', 'Description 2'));
    
    $template = ListTemplate::create('Our Products', 'View Catalog')
        ->addSection($section);
    
    $bot->reply($template);
});

use BotMan\Drivers\Whatsapp\Extensions\TemplateMessage;

$botman->hears('order confirmation', function ($bot) {
    $template = TemplateMessage::create('order_confirmation', 'en_US')
        ->addBodyParameters(['John Doe', 'ORD-12345', '$99.99']);
    
    $bot->reply($template);
});

use BotMan\Drivers\Whatsapp\Extensions\MediaTemplate;

// Send image
$botman->hears('send image', function ($bot) {
    $media = MediaTemplate::create(MediaTemplate::TYPE_IMAGE)
        ->link('https://example.com/image.jpg')
        ->caption('Check this out!');
    
    $bot->reply($media);
});

// Send document
$botman->hears('send pdf', function ($bot) {
    $media = MediaTemplate::create(MediaTemplate::TYPE_DOCUMENT)
        ->link('https://example.com/document.pdf')
        ->filename('Document.pdf')
        ->caption('Here is the document');
    
    $bot->reply($media);
});

use BotMan\Drivers\Whatsapp\Extensions\LocationMessage;

$botman->hears('location', function ($bot) {
    $location = LocationMessage::create(37.7749, -122.4194)
        ->name('San Francisco Office')
        ->address('123 Market St, San Francisco, CA');
    
    $bot->reply($location);
});

use BotMan\Drivers\Whatsapp\Extensions\ReactionMessage;

$botman->receivedMessage(function ($bot, $message) {
    // React to the message with a thumbs up
    $messageId = $message->getPayload()->get('messages')[0]['id'];
    $reaction = ReactionMessage::create($messageId, '👍');
    
    $bot->reply($reaction);
});

use BotMan\Drivers\Whatsapp\Extensions\FlowTemplate;

// Send a flow message
$botman->hears('start survey', function ($bot) {
    $flow = FlowTemplate::create(
        'your_flow_id',           // Flow ID from WhatsApp Manager
        'Start Survey',            // Button text
        'Please complete our customer satisfaction survey'
    )
    ->header('Customer Survey')
    ->footer('Takes only 2 minutes')
    ->navigate('welcome_screen', ['customer_id' => '12345'])
    ->mode('published');  // or 'draft' for testing
    
    $bot->reply($flow);
});

// Handle flow responses
$botman->receivedMessage(function ($bot, $message) {
    $payload = $message->getPayload();
    
    if ($flowResponse = $payload->get('flow_response')) {
        $responseData = json_decode($flowResponse['response_json'], true);
        
        // Process the flow response data
        $bot->reply('Thank you for completing the survey!');
        
        // Access flow data
        $name = $flowResponse['name'];
        $data = $responseData;  // Contains user's form submissions
    }
});

// Data exchange flow
$botman->hears('update profile', function ($bot) {
    $flow = FlowTemplate::create(
        'profile_flow_id',
        'Update Profile',
        'Update your account information'
    )
    ->dataExchange([
        'current_name' => 'John Doe',
        'current_email' => '[email protected]',
    ])
    ->mode('published');
    
    $bot->reply($flow);
});

$botman->hears('quote me', function ($bot, $message) {
    $messageId = $message->getPayload()->get('messages')[0]['id'];
    
    // Use the driver to set reply context
    $driver = $bot->getDriver();
    $driver->replyTo($messageId);
    
    $bot->reply('This is a reply to your message!');
});

$botman->receivedMessage(function ($bot, $message) {
    $payload = $message->getPayload();
    
    if ($media = $payload->get('media')) {
        $driver = $bot->getDriver();
        
        // Get media URL
        $mediaInfo = $driver->getMediaUrl($media['id']);
        
        // Download media
        $savePath = storage_path("app/whatsapp/media/{$media['id']}");
        $driver->downloadMedia($mediaInfo['url'], $savePath);
        
        $bot->reply('Media downloaded successfully!');
    }
});

$botman->receivedMessage(function ($bot, $message) {
    $messageId = $message->getPayload()->get('messages')[0]['id'];
    $driver = $bot->getDriver();
    $driver->markAsRead($messageId);
});