10/29/2019

Line Bot : Line แบบ Push สามารถทำเขียน php แล้วเรียกใช้ผ่าน Crontab ก็ได้ Run จาก หน้า php

Line Bot : Line แบบ Push สามารถทำเขียน php แล้วเรียกใช้ผ่าน Crontab ก็ได้ Run จาก หน้า php
ไม่ต้องส่ง reply จาก Line
Test Run ผ่านหน้า Web
หรือ ผ่าน Terminal command php
  1. <?php
  2.  
  3. $ACCESS_TOKEN = "XXX";
  4.  
  5.    $content = file_get_contents('php://input');
  6.    $arrayJson = json_decode($content, true);
  7.    $arrayHeader = array();
  8.    $arrayHeader[] = "Content-Type: application/json";
  9.    $arrayHeader[] = "Authorization: Bearer {$ACCESS_TOKEN}";
  10.    //รับข้อความจากผู้ใช้
  11. //   $message = $arrayJson['events'][0]['message']['text'];
  12.    //รับ id ของผู้ใช้
  13. //   $id = $arrayJson['events'][0]['source']['userId'];
  14. //   #ตัวอย่าง Message Type "Text + Sticker"
  15. //   if($message == "สวัสดี"){
  16.  
  17.       $arrayPostData['to'] = 'UID XXXXX'; //Test Suwit
  18.  
  19.       $arrayPostData['messages'][0]['type'] = "text";
  20.       $arrayPostData['messages'][0]['text'] = "สวัสดีจ้าาา";
  21.  
  22.       $arrayPostData['messages'][1]['type'] = "sticker";
  23.       $arrayPostData['messages'][1]['packageId'] = "2";
  24.       $arrayPostData['messages'][1]['stickerId'] = "34";
  25.       pushMsg($arrayHeader, $arrayPostData);
  26. //   }
  27.  
  28.    function pushMsg($arrayHeader,$arrayPostData){
  29.       $strUrl = "https://api.line.me/v2/bot/message/push";
  30.       $ch = curl_init();
  31.       curl_setopt($ch, CURLOPT_URL,$strUrl);
  32.       curl_setopt($ch, CURLOPT_HEADER, false);
  33.       curl_setopt($ch, CURLOPT_POST, true);
  34.       curl_setopt($ch, CURLOPT_HTTPHEADER, $arrayHeader);
  35.       curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($arrayPostData));
  36.       curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
  37.       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  38.       $result = curl_exec($ch);
  39.       curl_close ($ch);
  40.    }
  41. ?>

No comments:

Post a Comment