PHP code example of craftivelabs / ci4-syncraft

1. Go to this page and download the library: Download craftivelabs/ci4-syncraft 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/ */

    

craftivelabs / ci4-syncraft example snippets




use CraftiveLabs\Syncraft\SyncraftClient;

$client = new SyncraftClient('YOUR_API_KEY');

// Connect via QR
$qr = $client->connectQr();
echo $qr->qrCode; // data:image/png;base64,...

// Connect via phone pairing code
$code = $client->connectPhone('6281234567890');
echo $code->pairingCode; // ABCD-1234

// Disconnect (temporary)
$client->disconnect();

// Logout (permanent)
$client->logout();

// Get connection status
$status = $client->getStatus();
$status->isConnected; // bool
$status->status;      // "connected"
$status->deviceId;    // UUID
$status->jid;         // "[email protected]"

// Text message
$result = $client->sendText('6281234567890', 'Hello from Syncraft!');
$result->messageId; // "3EB08F9BB46BF22511D17C"
$result->status;    // "sent"

// Media message (file upload)
$result = $client->sendMedia(
    '6281234567890',
    'image',
    'Check this out!',
    '/path/to/photo.jpg'
);

// Media message with custom filename
$result = $client->sendMedia(
    '6281234567890',
    'document',
    'Invoice #1001',
    '/path/to/invoice.pdf',
    'invoice-1001.pdf' // optional custom filename
);

// Group text
$result = $client->sendGroupText('[email protected]', 'Hello group!');

// Group media (file upload)
$result = $client->sendGroupMedia(
    '[email protected]',
    'image',
    'Check this photo',
    '/path/to/photo.jpg'
);

// Group media with custom filename
$result = $client->sendGroupMedia(
    '[email protected]',
    'document',
    'Report Q2',
    '/path/to/report.pdf',
    'quarterly-report-q2.pdf'
);

// Channel text
$result = $client->sendChannelText('12036302394829302@newsletter', 'New update!');

// Channel media
$result = $client->sendChannelMedia(
    '12036302394829302@newsletter',
    'image',
    'New banner',
    '/path/to/banner.jpg'
);

// Create broadcast
$broadcast = $client->createBroadcast('Promo July', 'text', 'Hello!', [
    ['phone_number' => '62812345678'],
    ['phone_number' => '62898765432', 'message' => 'Custom message for Budi!'],
]);
$broadcast->id;     // 42
$broadcast->status; // "pending"

// Get broadcast status
$detail = $client->getBroadcast(42);
$detail->sentCount;   // 2
$detail->failedCount; // 0

// Cancel broadcast
$client->cancelBroadcast(42);

// Get joined groups
$groups = $client->getGroups();
foreach ($groups->groups as $group) {
    echo $group->name; // "Developer Syncraft"
    echo $group->participants; // 21
}

// Get channels
$channels = $client->getChannels();

// Check WhatsApp number
$check = $client->checkNumber('6281234567890');
$check->isRegistered; // true

// Bulk check numbers
$results = $client->checkNumbers(['6281234567890', '11234567890']);



use CraftiveLabs\Syncraft\DTO\WebhookEvent;
use CraftiveLabs\Syncraft\Webhook\Handler;
use CraftiveLabs\Syncraft\Webhook\WebhookTrait;

class MessageHandler implements Handler
{
    use WebhookTrait;

    public function handle(array $payload): void
    {
        $event = $this->parseEvent($payload);

        if ($event->isMessageReceived()) {
            $msg = $event->toMessageData();
            log_message('info', "Message from {$msg->from}: {$msg->content}");
        }

        if ($event->isDeviceDisconnected()) {
            $device = $event->toDeviceData();
            log_message('error', "Device {$event->deviceId} disconnected");
        }
    }
}