PHP code example of omniai / sdk

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

    

omniai / sdk example snippets




mniAI\Client;

// Initialize client
$client = new Client('YOUR_API_KEY');

// Send a message
$message = $client->messages->send([
    'phone' => '+5511999999999',
    'channel' => 'whatsapp',
    'type' => 'text',
    'content' => 'Olá! Como posso ajudar?'
]);

echo "Message sent: {$message['id']}\n";

// Text message
$message = $client->messages->send([
    'phone' => '+5511999999999',
    'channel' => 'whatsapp',
    'type' => 'text',
    'content' => 'Hello from OmniAI!'
]);

// Media message
$mediaMessage = $client->messages->send([
    'phone' => '+5511999999999',
    'channel' => 'whatsapp',
    'type' => 'image',
    'content' => 'Check this image',
    'mediaUrl' => 'https://example.com/image.jpg'
]);

// Bulk send
$result = $client->messages->sendBulk([
    'messages' => [
        [
            'phone' => '+5511999999999',
            'channel' => 'whatsapp',
            'type' => 'text',
            'content' => 'Message 1'
        ],
        [
            'phone' => '+5511888888888',
            'channel' => 'whatsapp',
            'type' => 'text',
            'content' => 'Message 2'
        ]
    ]
]);

// Create contact
$contact = $client->contacts->create([
    'name' => 'João Silva',
    'phone' => '+5511999999999',
    'email' => '[email protected]',
    'tags' => ['customer', 'vip']
]);

// List contacts
$contacts = $client->contacts->list([
    'page' => 1,
    'limit' => 50,
    'search' => 'João'
]);

// Update contact
$updated = $client->contacts->update('contact-123', [
    'tags' => ['customer', 'vip', 'premium']
]);

$campaign = $client->campaigns->create([
    'name' => 'Black Friday 2024',
    'channel' => 'whatsapp',
    'messageTemplate' => 'Aproveite 50% OFF em todos os produtos!',
    'targetAudience' => [
        'tags' => ['customer'],
        'segment' => 'active'
    ],
    'scheduledAt' => '2024-11-29T09:00:00Z'
]);

// Start campaign
$client->campaigns->start($campaign['id']);

// Chat completion
$response = $client->ai->chatCompletion([
    'messages' => [
        ['role' => 'user', 'content' => 'Preciso de ajuda com meu pedido']
    ]
]);

// Sentiment analysis
$sentiment = $client->ai->analyzeSentiment([
    'text' => 'Estou muito satisfeito com o atendimento!'
]);
echo $sentiment['sentiment']; // 'positive'

// Intent detection
$intent = $client->ai->extractIntent([
    'text' => 'Quero cancelar minha assinatura'
]);
echo $intent['intent']; // 'cancel_subscription'

// Generate response
$suggestion = $client->ai->generateResponse([
    'context' => 'Cliente perguntou sobre prazo de entrega',
    'userMessage' => 'Quando chega meu pedido?',
    'tone' => 'professional'
]);

// Get overview
$analytics = $client->analytics->getOverview([
    'startDate' => '2024-01-01',
    'endDate' => '2024-01-31',
    'channel' => 'whatsapp'
]);

// Export report
$export = $client->analytics->exportReport([
    'reportType' => 'messages',
    'startDate' => '2024-01-01',
    'endDate' => '2024-01-31',
    'format' => 'pdf'
]);
echo $export['downloadUrl'];

use OmniAI\Client;
use OmniAI\Exceptions\AuthenticationException;
use OmniAI\Exceptions\ValidationException;
use OmniAI\Exceptions\RateLimitException;

$client = new Client('YOUR_API_KEY');

try {
    $message = $client->messages->send([
        'phone' => '+5511999999999',
        'channel' => 'whatsapp',
        'type' => 'text',
        'content' => 'Hello!'
    ]);
} catch (AuthenticationException $e) {
    echo 'Invalid API key';
} catch (ValidationException $e) {
    echo "Validation error: {$e->getMessage()}";
} catch (RateLimitException $e) {
    echo 'Rate limit exceeded, please wait';
}

// Custom configuration
$client = new Client(
    'YOUR_API_KEY',
    'https://api.omniaiassist.com/v1'
);