PHP code example of linqelio / linqelio-laravel

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

    

linqelio / linqelio-laravel example snippets


$customer->sendMessage('Your order has shipped');

Linqelio::forKey($tenant->linqelio_key)->messages()->sendText($contactId, 'hello');

use Linqelio\Laravel\Facades\Linqelio;
use Linqelio\Laravel\Data\Enums\MessageType;

Linqelio::messages()->sendText($contactId, 'Your order has shipped');

Linqelio::messages()->sendText($contactId, 'Hi', channelId: $channelId);

Linqelio::messages()->sendText(
    contactId: $contactId,
    text: 'Your order has shipped',
    idempotencyKey: "order-{$order->id}-shipped",
);

use Linqelio\Laravel\Jobs\SendMessage;

SendMessage::dispatch($contactId, MessageType::Text, ['text' => 'Thanks!']);

$media = Linqelio::media()->uploadFile($request->file('invoice'));

Linqelio::messages()->sendMedia(
    contactId: $contactId,
    type: MessageType::Document,
    media: $media,
    caption: 'Your invoice',
);

return Linqelio::media()->fetch($messageId)->toResponse();

use Linqelio\Laravel\Events\MessageReceived;

class NotifyOperator
{
    public function handle(MessageReceived $event): void
    {
        $message = $event->message();   // fetched only if you ask

        Operator::notify($message?->text());
    }
}

Linqelio::webhooks()->register('https://your-app.test/linqelio/webhook',
    ['message.inbound', 'message.status']);

public function handle(MessageStatusChanged $event): void
{
    if ($event->hasFailed()) {
        // $event->reason says why: an unaddressable recipient, an unsupported
        // type. It will not be retried.
        Log::warning('send failed', ['id' => $event->messageId, 'why' => $event->reason]);
    }
}

$registered = Linqelio::webhooks()->register('https://your-app.test/linqelio/webhook',
    ['message.inbound']);

$registered->id();      // the subscription
$registered->secret;    // the signing key — SHOWN ONCE, see below

Linqelio::webhooks()->disable($id);   // stop delivering, keep the subscription
Linqelio::webhooks()->enable($id);
Linqelio::webhooks()->delete($id);    // unsubscribe for good

// The platform mints one and hands it back — the only time it is ever returned.
$registered = Linqelio::webhooks()->register($url, ['message.inbound']);
$registered->secret;   // put this in webhooks.secret before it goes out of scope

// Or sign with a key your deployment already holds. It goes to the platform's
// secret store; nothing reads it back.
Linqelio::webhooks()->register($url, ['message.inbound'],
    secret: config('linqelio.webhooks.secret'));

// Or point at one already in that store, if you administer it yourself.
Linqelio::webhooks()->register($url, ['message.inbound'],
    secretRef: 'secret://webhooks/your-app');

$result = Linqelio::contacts()->erase($contactId);

$result->toArray();  // ['contacts' => 1, 'identities' => 3, ...] — record this

LinqelioMessage::where('contact_id', $contactId)->delete();

use Linqelio\Laravel\Models\LinqelioMessage;

LinqelioMessage::query()
    ->forContact($contactId)
    ->inbound()
    ->where('text', 'like', "%invoice%")
    ->latest('occurred_at')
    ->get();

use Linqelio\Laravel\Concerns\HasLinqelioContact;

class Customer extends Model
{
    use HasLinqelioContact;   // needs a nullable linqelio_contact_id column
}

$customer->linkLinqelioContact(ChannelKind::WaWeb, phone: '+380501082555');
$customer->queueMessage('Thanks for your order');
$customer->linqelioMessages()->latest('occurred_at')->get();

use Linqelio\Laravel\Exceptions\{PolicyException, ChannelException, LinqelioException};

try {
    Linqelio::messages()->sendText($contactId, $text);
} catch (PolicyException $e) {
    // rate limited or over quota
    retryIn($e->retryAfter() ?? 60);
} catch (ChannelException $e) {
    // not connected, or the contact is not reachable there
    report($e->errorCode()->value);
} catch (LinqelioException $e) {
    report($e);
}

// AppServiceProvider
EmbedTokenController::resolveContactUsing(
    fn (Request $request) => $request->user()?->linqelio_contact_id
);

Http::fake(['*/contacts/*/messages' => Http::response(['id' => '01ABC', 'type' => 'text'], 202)]);
bash
composer vendor:publish --tag=linqelio-config
php artisan migrate
bash
php artisan linqelio:channels
bash
php artisan linqelio:backfill