PHP code example of denlopes / waha-laravel-sdk

1. Go to this page and download the library: Download denlopes/waha-laravel-sdk 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/ */

    

denlopes / waha-laravel-sdk example snippets


use Waha;

$chat = Waha::chat('[email protected]');

$message = $chat->sendMessage('Hello from Laravel');
$message->id(); // WhatsApp message id

use DenLopes\Waha\Data\Input\RemoteFile;

$chat->sendImage(new RemoteFile('image/jpeg', 'https://example.com/pic.jpg'));

$chat->sendMessage('Thanks for reaching out!', replyTo: '[email protected]_ABC');

$conversation = $chat->conversation();

$conversation->send('Hi there');
$conversation->reply('Good question', $incomingMessageId);

use DenLopes\Waha\Services\SessionService;

$sessions = app(SessionService::class)->listSessions();

use DenLopes\Waha\Client;

public function __construct(private Client $waha) {}

$chat = $this->waha->chat('[email protected]', 'sales');

use DenLopes\Waha\Data\Input\RemoteFile;
use Waha;

$chat = Waha::chat('[email protected]', 'sales'); // session name or Session object

$message = $chat->sendMessage('Hello from Laravel');

// Every send* returns a Message, so message actions chain directly.
$chat->sendImage(new RemoteFile(mimetype: 'image/jpeg', url: 'https://example.com/pic.jpg'))
    ->react('🔥');

$chat->message($message->id());      // lazy handle, no I/O until get()
$chat->find($message->id());         // eager fetch
$chat->getMessages(limit: 50);       // list, as Message objects

$existing = $chat->message($message->id());

$existing
    ->markRead()
    ->pin()
    ->update('Updated text')
    ->delete();

use Waha;

$conversation = Waha::conversation('[email protected]');

// markRead → thinking delay → startTyping → random typing delay → stopTyping → sendText
$message = $conversation->send('Hello from Laravel');

// Reply to an inbound message with the same flow.
$reply = $conversation->reply('Thanks for reaching out!', $incomingMessageId);

$conversation = $chat->conversation();

$conversation
    ->markRead()
    ->startTyping()
    ->wait(800)
    ->stopTyping();

$conversation->reset(); // clear pacing state, e.g. when a human takes over

use DenLopes\Waha\Enums\ContactStage;
use DenLopes\Waha\Support\Pacing;

Pacing::fromConfig(); // reads waha.conversations
Pacing::off();        // no humanization, no pacing (useful in tests)

$conversation->withStage(ContactStage::Warm)->send('hi');

use DenLopes\Waha\Session;

$session = Session::from('default');
$session = Session::default(); // uses waha.default_session

$session->value(); // string
(string) $session; // string

use DenLopes\Waha\Services\MessagingService;

$messaging = app(MessagingService::class);

$message = $messaging->sendText(
    chatId: '[email protected]',
    text: 'Hello!',
);

use DenLopes\Waha\Data\Input\ApiKeyRequest;
use DenLopes\Waha\Data\SessionActions;

$request = new ApiKeyRequest(
    isAdmin: false,
    session: 'default',
    isActive: true,
    actions: new SessionActions(
        read: true,
        send: true,
        control: false,
        setting: false,
        app: false,
        delete: false,
    ),
);

$request->toArray();

// config/waha.php
'default_host' => env('WAHA_DEFAULT_HOST', 'primary'),

'hosts' => [
    'primary' => [
        'base_url'        => env('WAHA_PRIMARY_URL'),
        'api_key'         => env('WAHA_PRIMARY_API_KEY'),
        'api_key_header'  => env('WAHA_API_KEY_HEADER', 'X-Api-Key'),
        'default_session' => env('WAHA_PRIMARY_DEFAULT_SESSION', 'default'),
        'mode'            => env('WAHA_PRIMARY_MODE', 'admin_fallback'),
        'session_keys'    => [],
    ],
    'secondary' => [
        'base_url' => env('WAHA_SECONDARY_URL'),
        'api_key'  => env('WAHA_SECONDARY_API_KEY'),
    ],
],

use DenLopes\Waha\Contracts\PinStore;

$pins = app(PinStore::class);

$pins->pin('company-123', 'company-host');
$pins->getHostForSession('company-123'); // 'company-host'
$pins->forget('company-123');

// config/waha.php
'webhooks' => [
    'handlers' => [
        'message.any' => \App\Waha\Handlers\MessageHandler::class,
        'message.*'   => \App\Waha\Handlers\AnyMessageHandler::class,
    ],
],

try {
    $messaging->sendText('[email protected]', 'Hello');
} catch (\DenLopes\Waha\Exceptions\RateLimitException $e) {
    // back off and retry later
} catch (\DenLopes\Waha\Exceptions\WahaException $e) {
    report($e);
}

$debug = app(\DenLopes\Waha\Debug\DebugStore::class);
$debug->last();     // last masked request/response
$debug->lastCurl(); // last request as a copy-pasteable curl command
bash
php artisan vendor:publish --tag="waha-config"
php artisan vendor:publish --tag="waha-migrations"