PHP code example of silasrm / chat-api

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

    

silasrm / chat-api example snippets


// config/services.php
...
'chatapi' => [
     // Like: https://euXXXX.chat-api.com/instanceYYYYY/
    'url' => env('CHATAPI_URL'),
    'token' => env('CHATAPI_TOKEN'),
],
...

use Illuminate\Notifications\Notification;
use Silasrm\ChatApi\ChatApiMessage;
use Silasrm\ChatApi\ChatApiChannel;

class OrderCreated extends Notification
{
    public function via($notifiable): array
    {
        return [
            ChatApiChannel::class,
        ];
    }

    public function toRocketChat($notifiable): ChatApiMessage
    {
        return ChatApiMessage::create('John Doe create a new order with value US$ 50.0')
            ->to('NNNNNNNNNNNNNN'); // Phone number with country code
    }
}

public function routeNotificationForChatApi(): string
{
    return $this->phone;
}

public function toChatApi($notifiable)
{
    return ChatApiMessage::create('Test message')
        ->to('NNNNNNNNNNNNNN') // Phone number with country code
        ->attachments([
            // url (for remote) or path (for local), file
            ChatApiAttachment::create()->url('test'),
            ChatApiAttachment::create(['url' => 'test']),
            new ChatApiAttachment(['url' => 'test']),
            ['url' => 'test']
        ]);
}

[
    [
        'caption' => 'Caption of file',
        'filename' => 'payment.xlsx',
    ]
];   

public function toChatApi($notifiable)
{
    return ChatApiMessage::create('Test message')
        ->to('NNNNNNNNNNNNNN') // Phone number with country code
        ->links([
            ChatApiLink::create()
                ->url('https://wikimedia.org/nature')
                ->title('All about Nature')
                ->previewImage('https://upload.wikimedia.org/wikipedia/ru/3/33/NatureCover2001.jpg'),
            ChatApiLink::create([
                'url' => 'https://wikimedia.org/nature',
                'title' => 'All about Nature',
                'previewImage' => 'https://upload.wikimedia.org/wikipedia/ru/3/33/NatureCover2001.jpg',
            ]),
            new ChatApiLink([
                'url' => 'https://wikimedia.org/nature',
                'title' => 'All about Nature',
                'previewImage' => 'https://upload.wikimedia.org/wikipedia/ru/3/33/NatureCover2001.jpg',
            ]),
            [
                'url' => 'https://wikimedia.org/nature',
                'title' => 'All about Nature',
                'previewImage' => 'https://upload.wikimedia.org/wikipedia/ru/3/33/NatureCover2001.jpg',
            ]
        ]);
}

[
    [
        'title' => 'All about Nature',
        'previewImage' => 'https://upload.wikimedia.org/wikipedia/ru/3/33/NatureCover2001.jpg',
        'description' => 'See that!',
    ]
];