Skip to content

Webhook API

Romain Monteil edited this page Feb 10, 2018 · 3 revisions

Webhook API

To be able to send messages to an user, you need to configure a webhook in your Facebook application. To do that, follow the instructions here.

Once your webhook is configured, you need to validate the callback URL

if (!$messenger->webhook()->isValidToken()) {
    // Display an error
}

$challenge = $messenger->webhook()->challenge();

header("HTTP/1.1 200 OK");
echo $challenge;

Then you will be able to handle callbacks from messenger

if (!$messenger->webhook()->isValidCallback()) {
    header("HTTP/1.1 400 Invalid Request");
}

$events = $messenger->webhook()->getCallbackEvents();
foreach ($events as $event) {
    if ($event instanceof MessageEvent) {
        $message = $event->getMessage();
        
        echo $message->getMessageId();
        echo $message->getSequence();
        
        if ($message->hasText()) {
            echo $message->getText();
        }
        
        if ($message->hasQuickReply()) {
            echo $message->getQuickReply();
        }
        
        if ($message->hasAttachments()) {
            print_r($message->getAttachments());
        }
    }
    
    if ($event instanceof PostbackEvent) {
        $postback = $event->getPostback();
        
        echo $postback->getPayload();
    }
}

header("HTTP/1.1 200 OK")
Clone this wiki locally