PHP code example of asmaa-gamal / laravel-meta-messaging

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

    

asmaa-gamal / laravel-meta-messaging example snippets


Meta::facebook()->to($psid)->text('Your order shipped!')->send();

use AsmaaGamal\MetaMessaging\Facades\Meta;

// Text
Meta::facebook()->to($psid)->text('Hello!')->send();

// Media — a URL, a local file, or a reusable attachment ID; the source is inferred
Meta::facebook()->to($psid)->image('https://example.com/photo.jpg')->send();
Meta::facebook()->to($psid)->video('/local/path/clip.mp4')->send();
Meta::facebook()->to($psid)->audio('1234567890')->send();
Meta::facebook()->to($psid)->file('https://example.com/invoice.pdf')->send();

// Reply to a specific message, and react to one
Meta::facebook()->to($psid)->text('On it')->replyTo($mid)->send();
Meta::facebook()->to($psid)->react($mid, '❤');
Meta::facebook()->to($psid)->unreact($mid);

// Comments
Meta::facebook()->privateReply($commentId, 'Sent you the details in DM');
Meta::facebook()->replyToComment($commentId, 'Thanks for the kind words!');

// Instagram — same API, narrower feature set
Meta::instagram()->to($igsid)->text('Thanks for reaching out!')->send();

use AsmaaGamal\MetaMessaging\Messages\Templates\{GenericTemplate, Card};
use AsmaaGamal\MetaMessaging\Messages\Buttons\{UrlButton, PostbackButton};

Meta::facebook()->to($psid)->template(
    GenericTemplate::make()
        ->card(
            Card::make('Classic T-Shirt')
                ->subtitle('Soft cotton, $19')
                ->image('https://example.com/shirt.png')
                ->button(UrlButton::make('View', 'https://example.com/shirt'))
                ->button(PostbackButton::make('Buy now', 'BUY_SHIRT'))
        )
)->send();

use AsmaaGamal\MetaMessaging\Messages\QuickReply;

Meta::facebook()->to($psid)
    ->text('How can we help?')
    ->quickReplies([
        QuickReply::text('Track my order', 'TRACK'),
        QuickReply::text('Talk to a human', 'AGENT'),
        QuickReply::email(),
    ])
    ->send();

config(['meta-messaging.version' => 'v25.0']);           // default for everything

Meta::facebook()->usingVersion('v23.0')->to($psid)->text('Hi')->send();

'accounts' => [
    'facebook' => [
        'default' => ['page_id' => '...', 'token' => '...'],
        'legacy'  => ['page_id' => '...', 'token' => '...', 'version' => 'v21.0'],
    ],
],

Meta::facebook('second_page')->to($psid)->text('Hi')->send();

// Multi-tenant: resolve the token at runtime
Meta::facebook()->usingToken($tenant->page_token, $tenant->page_id)
    ->to($psid)->text('Hi')->send();

use AsmaaGamal\MetaMessaging\Exceptions\{
    MessagingWindowExpiredException,
    RecipientUnavailableException,
    PrivateReplyNotAllowedException,
    MetaMessagingException,
};

try {
    Meta::facebook()->to($psid)->text('Hello')->send();
} catch (MessagingWindowExpiredException $e) {
    // Outside 24 hours — queue it for the next time they message you
} catch (RecipientUnavailableException $e) {
    // Permanent for this person — stop retrying, mark them unreachable
} catch (MetaMessagingException $e) {
    report($e);
}

$response = Meta::facebook()->to($psid)->text('Hello')->sendSafely();

if ($response->failed()) {
    $error = $response->error();

    $error->key;          // 'window_expired' — stable, safe to switch on
    $error->hint;         // plain-English explanation of the fix
    $error->code;         // 10
    $error->subcode;      // 2018278
    $error->traceId;      // Meta's fbtrace_id, for support tickets
    $error->isRetryable(); // false
    $error->toArray();    // everything, ready to log
}

Meta::facebook()->to($psid)->text('Hello')->queue();

use AsmaaGamal\MetaMessaging\Events\{MetaMessageSent, MetaMessageFailed};

use AsmaaGamal\MetaMessaging\Facades\Meta;
use AsmaaGamal\MetaMessaging\Transport\MetaRequest;

it('greets the customer', function () {
    Meta::fake();

    (new WelcomeFlow)->run($psid);

    Meta::assertSent(fn (MetaRequest $r) => $r->payload['message']['text'] === 'Welcome!');
});

Meta::fake()->respondWithError(551, "This person isn't available right now.");

expect(fn () => Meta::facebook()->to($psid)->text('Hi')->send())
    ->toThrow(RecipientUnavailableException::class);
bash
php artisan vendor:publish --tag=meta-messaging-config