PHP code example of darkclow4 / laravel-waha-messages

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

    

darkclow4 / laravel-waha-messages example snippets


use LaravelWaha\WahaMessages\Facades\Waha;

// Send a text message
Waha::messages()->sendText('[email protected]', 'Hello from Laravel!');

Waha::messages()->sendText('[email protected]', 'Hello World!');

// With a specific session
Waha::messages()->sendText('[email protected]', 'Hello!', 'my-session');

Waha::messages()->sendImage(
    chatId: '[email protected]',
    url: 'https://example.com/photo.jpg',
    caption: 'Check out this image!',
);

Waha::messages()->sendVideo(
    chatId: '[email protected]',
    url: 'https://example.com/video.mp4',
    caption: 'Watch this video!',
);

Waha::messages()->sendFile(
    chatId: '[email protected]',
    url: 'https://example.com/report.pdf',
    filename: 'monthly-report.pdf',
);

Waha::messages()->sendLocation(
    chatId: '[email protected]',
    latitude: -6.200000,
    longitude: 106.816666,
    title: 'Jakarta',
);

Waha::messages()->sendContact(
    chatId: '[email protected]',
    contact: [
        'fullName' => 'John Doe',
        'phoneNumber' => '+1111111111111',
    ],
);

Waha::messages()->sendSeen('[email protected]');

// Start typing
Waha::messages()->startTyping('[email protected]');

// Stop typing
Waha::messages()->stopTyping('[email protected]');

// Basic usage
Waha::humanLike()->sendText('[email protected]', 'Hello!');

// With a specific session
Waha::humanLike()->sendText('[email protected]', 'Hello!', 'my-session');

Waha::humanLike()
    ->msPerChar(100)     // 100ms per character (default: 300)
    ->minDelay(1000)     // Minimum 1 second delay (default: 2000ms)
    ->maxDelay(15000)    // Maximum 15 second delay (default: 10000ms)
    ->sendText('[email protected]', 'This message will take longer to "type"...');

// List all sessions
$sessions = Waha::sessions()->list();

// Create a new session
Waha::sessions()->create(['name' => 'my-session']);

// Start / Stop / Logout
Waha::sessions()->start('my-session');
Waha::sessions()->stop('my-session');
Waha::sessions()->logout('my-session');

// Get session details
$session = Waha::sessions()->get('my-session');

// Get QR code for authentication
$qr = Waha::sessions()->qr('my-session');

// Request authentication code
Waha::sessions()->requestCode('my-session', [
    'phoneNumber' => '1111111111111',
]);

// Update a session
Waha::sessions()->update('my-session', ['webhooks' => [...]]);

// Delete a session
Waha::sessions()->delete('my-session');

// Get chats overview
$chats = Waha::chats()->overview();

// Get messages from a specific chat
$messages = Waha::chats()->messages('[email protected]', limit: 50);

// Delete a chat
Waha::chats()->deleteChat('[email protected]');

// Use a specific session
$chats = Waha::chats()->overview('my-session');

// List all groups
$groups = Waha::groups()->list();

// Create a group
Waha::groups()->create('Project Team', [
    '[email protected]',
    '[email protected]',
]);

// Get group details
$group = Waha::groups()->get('[email protected]');

// Add participants
Waha::groups()->addParticipants('[email protected]', [
    '[email protected]',
]);

// Remove participants
Waha::groups()->removeParticipants('[email protected]', [
    '[email protected]',
]);

// List all contacts
$contacts = Waha::contacts()->list();

// Get a specific contact
$contact = Waha::contacts()->get('[email protected]');

// Check if a phone number exists on WhatsApp
$result = Waha::contacts()->checkExists('1111111111111');
// Returns: ['numberExists' => true, 'chatId' => '[email protected]']

// Get profile picture URL
$picture = Waha::contacts()->profilePicture('[email protected]');

use LaravelWaha\WahaMessages\WahaClient;

class NotificationService
{
    public function __construct(
        private WahaClient $waha,
    ) {}

    public function sendAlert(string $phone, string $message): void
    {
        $this->waha->messages()->sendText("{$phone}@c.us", $message);
    }
}

use LaravelWaha\WahaMessages\Exceptions\WahaException;
use LaravelWaha\WahaMessages\Exceptions\WahaAuthenticationException;
use LaravelWaha\WahaMessages\Exceptions\WahaNotFoundException;

try {
    Waha::sessions()->get('non-existent');
} catch (WahaNotFoundException $e) {
    // Session not found (404)
    $e->getStatusCode();    // 404
    $e->getResponseBody();  // ['message' => '...']
} catch (WahaAuthenticationException $e) {
    // Invalid API key (401)
} catch (WahaException $e) {
    // Any other API error
}
bash
php artisan vendor:publish --tag=waha-config