การส่ง Message ประเภทอื่นๆ

นอกจากข้อความ เราสามารถส่งข้อมูลประเภทอื่นๆได้ ดังนี้

  • รูปภาพ
  • เสียง
  • วีดีโอ
  • ไฟล์อื่นๆ (ที่ Faceboook อนุญาต)

ทดลองส่งไฟล์รูปภาพ

อันดับแรก ควรเข้าไปดูคำแนะนำที่เอกสารของ Facebook https://developers.facebook.com/docs/messenger-platform/send-messages/\#content\_types

ทำการเพิ่ม 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);

$access_token = "EAAEF6Rg2mWEBAAnqU0FhYYlb3s0a7kiVuKMUja9umtC50i25bTZAOBBVvDf9BX0hixiqYryCMVLVnhZBZCkJZBl5AZAQTMvAoIZCj5W268DOurt2FLIGb2GL2fGWMwY6RtN7nuFe3K13B9tXxNPfonHvDZCGyTldpbFvdipzJD3QQZDZD";

if(!empty($input['entry'][0]['messaging'][0]['message']))
{
    if( $message == "หวัดดีหมอ" )
    {
        reply($access_token, $sender, "สวัสดี อยากให้ข้าช่วยอะไรล่ะ");
    }
    if( $message == "เกรี้ยวกราด")
    {
        reply_image($access_token, $sender, "https://vignette.wikia.nocookie.net/psi/images/a/ac/Angry-hugh-jackman.jpg/revision/latest?cb=20151113213317");
    }
    else
    {
        reply($access_token, $sender, "เจ้าพูดอะไร ไม่รู้ความ");
    }

}

if(!empty($input['entry'][0]['messaging'][0]['postback'])){
    reply_echo($access_token, $sender, "Let's start");
}

echo $challange;


function reply($access_token, $sender, $message)
{
    //API Url
    $url = 'https://graph.facebook.com/v2.6/me/messages?'.http_build_query(array(
        'access_token' => $access_token));

    //Initiate cURL.
    $ch = curl_init($url);

    //The JSON data.
    $jsonData = array (
        'recipient' => 
        array (
          'id' => $sender,
        ),
        'message' => 
        array (
          'text' => $message,
        ),
      );
    error_log($jsonData);

    //Encode the array into JSON.
    $jsonDataEncoded = json_encode($jsonData);

    //Tell cURL that we want to send a POST request.
    curl_setopt($ch, CURLOPT_POST, 1);

    //Attach our encoded JSON string to the POST fields.
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);

    //Set the content type to application/json
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

    //Execute the request
    $result = curl_exec($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $error = curl_error($ch);

    error_log($jsonDataEncoded);
    error_log($url);
    error_log($result);
    error_log($httpcode);
    error_log($error);
}

function reply_image($access_token, $sender, $image_url)
{
    //API Url
    $url = 'https://graph.facebook.com/v2.6/me/messages?'.http_build_query(array(
        'access_token' => $access_token));

    //Initiate cURL.
    $ch = curl_init($url);

    //The JSON data.
    $jsonData = array (
        'recipient' => 
        array (
          'id' => $sender,
        ),
        'message' => 
        array (
            'attachment' => 
            array (
                'type' => 'image',
                'payload' => 
                    array (
                        'url' => 'https://vignette.wikia.nocookie.net/psi/images/a/ac/Angry-hugh-jackman.jpg/revision/latest?cb=20151113213317',
                        'is_reusable' => true,
                    ),
            ),
        )
      );
    error_log($jsonData);

    //Encode the array into JSON.
    $jsonDataEncoded = json_encode($jsonData);

    //Tell cURL that we want to send a POST request.
    curl_setopt($ch, CURLOPT_POST, 1);

    //Attach our encoded JSON string to the POST fields.
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);

    //Set the content type to application/json
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

    //Execute the request
    $result = curl_exec($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $error = curl_error($ch);

    error_log($jsonDataEncoded);
    error_log($url);
    error_log($result);
    error_log($httpcode);
    error_log($error);
}
?>

results matching ""

    No results matching ""