PHP code example of spits-online / laravel-bird-api

1. Go to this page and download the library: Download spits-online/laravel-bird-api 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/ */

    

spits-online / laravel-bird-api example snippets


use Spits\Bird\Services\ContactService;

$birdContacts = app(new ContactService())->index(limit: 20, reverse: true);

use Spits\Bird\Services\ContactService;

$birdContact = app(new ContactService())->show('bird-contact-id-123');

use Spits\Bird\Models\Contact;
use Spits\Bird\Enums\IdentifierKey;
use Spits\Bird\Services\ContactService;

$contact = (new Contact())
    ->displayName('John Doe')
    ->phoneNumber('+12345678901')
    ->emailAddress('[email protected]');

$response = (new ContactService())->createOrUpdate($contact, IdentifierKey::PHONE_NUMBER);

use Spits\Bird\Services\ContactService;

$response = (new ContactService())->delete('contact-id-123');

if ($response === true) {
    // Successfully deleted the contact
    dd('Contact deleted successfully.');
} else {
    // Handle error
    dd($response);
}

use Spits\Bird\Models\Contact;

$contact = (new Contact())
    ->displayName('Jane Doe')
    ->phoneNumber('+98765432103')
    ->emailAddress('[email protected]')
    ->attribute('company', 'Acme Corp');

// The contact can then be passed to the `ContactService` for API interaction

use Illuminate\Notifications\Notification;use Spits\Bird\Channels\SMSChannel;use Spits\Bird\Messages\SMSMessage;

class OrderNotification extends Notification
{
    public function via(): array
    {
        return [ SMSChannel::class ];
    }

    public function toSMS($notifiable): SMSMessage
    {
        $contact = (new Contact())
            ->displayName('Jane Doe')
            ->phoneNumber('+98765432103')
            ->emailAddress('[email protected]')
            ->attribute('company', 'Acme Corp');
        
        return (new SMSMessage())
            ->text('Your order has been shipped!')
            ->toContact($contact);
    }
}


'whatsapp' => [
    'foo_template' => [
        'template_project_id' => `template_project_id`,
        'template_version' => `template_version`,
        'template_locale' => `template_locale`,
    ],
]



namespace App\Notifications;

use App\Support\MessageTemplate;
use Boilerplate\Notifications\BaseNotification;
use Carbon\Carbon;
use Spits\Bird\Channels\WhatsappChannel;
use Spits\Bird\Messages\WhatsappMessage;

class WhatsappNotification extends BaseNotification
{
    public function __construct()
    {
        $this->setChannels([
            WhatsappChannel::class
        ]);
    }

    public function toWhatsapp($notifiable): WhatsappMessage {

        $message = new WhatsappMessage(
            receiver: $notifiable->phone_number,
            template: new MessageTemplate(
                projectId: config('bird.templates.whatsapp.foo_template.template_project_id')
                version: config('bird.templates.whatsapp.foo_template.template_version'),
                locale: config('bird.templates.whatsapp.foo_template.template_locale')
                variables: [
                    'receiverFirstName' => $notifiable->first_name,
                    'senderFullName' => 'Foo bar',
                ]),
        );

        return $message;
    }
}



public function sendWhatsappMessage(NotificationRequest $request)
    {
        //Make sure you send the correct contact identifiers
        // Default is phonenumbers for the WhatsappChannel
        $users = User::all();
        try {
            Notification::send($users, new WhatsappNotification());
        } catch (Exception $exception) {
            Log::info($exception->getMessage());
        }
    }
}



namespace App\Support;

use Spits\Bird\Support\MessageTemplate;

class WhatsappOverrideTemplate extends MessageTemplate
{
    public function __construct(
        array $variables,
        ?string $projectId = null,
        ?string $version = null,
        ?string $locale = null,
    )
    {

        parent::__construct(
            projectId: $projectId ?? config('bird.templates.whatsapp.test_message.template_project_id'),
            version: $version ?? config('bird.templates.whatsapp.test_message.template_version'),
            locale: $locale ?? config('bird.templates.whatsapp.test_message.template_locale'),
            variables: $variables
        );
    }
}

bash
   php artisan vendor:publish --tag="bird-config"