การรับ Message

ในหน้า Facebook app settings หลังจาก setup webhook สำเร็จแล้ว ให้ทำการเลือก Fanpage ด้วย

จากนั้น ทำการเพิ่ม code เข้าไปในไฟล์ bot.php

<?php
ini_set("log_errors", 1);
ini_set("error_log", "/tmp/php.log");

$challange = $_REQUEST['hub_challenge'];

$input = json_decode(file_get_contents('php://input'), true);
$sender = $input['entry'][0]['messaging'][0]['sender']['id'];
$message = $input['entry'][0]['messaging'][0]['message']['text'];

error_log($challange);
error_log($sender);
error_log($message);

echo $challange;
?>

ทำการส่งข้อความทาง Message ของ Fanpage

เปิดดูไฟล์ log จะเห็น user id ของผู้ส่ง และ ข้อความที่ ส่งมา

นอกจากนี้ การใช้ ngrok ก็สามารถช่วยดู request/response ได้เช่นกัน ใช้ web browser เปิดเข้าไปที่ http://localhost:4040

ถ้าเป็น input message ประเภท Text นี่คือข้อมูลที่เราสนใจ

results matching ""

    No results matching ""