2017年4月22日 星期六

LINE訊息機器人實作(二) - 後端(Ubuntu, Apache,PHP)

因為必須要有SSL所以我申請免費的Let's Encrypt, 每申請一次可使用90天
至於如何申請, 真的有非常多的資訊, 我這就不多說了

首先先不管Line官網所提供的SDK, 用手動的先寫簡單的回應程式

callback_test.php

<?php
$accessToken = 'Channel Access Token'; #網頁產生的那一串亂碼

$json_string = file_get_contents('php://input');
$jsonObj = json_decode($json_string);

$type = $jsonObj->events[0]->type;
if($jsonObj->events[0]->type == 'message') {
  $type = $jsonObj->events[0]->message->type;
  $text = $jsonObj->events[0]->message->text;
}

if($type != "text" ) {
  exit;
}

$replyToken = $jsonObj->events[0]->replyToken;
$response_format_text = [
 [
     "type" => "text",
     "text" => $text
 ]
];

$post_data = [
        "replyToken" => $replyToken,
        "messages" => $response_format_text
        ];

$ch = curl_init("https://api.line.me/v2/bot/message/reply");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json; charser=UTF-8',
    'Authorization: Bearer ' . $accessToken
    ));
$result = curl_exec($ch);
curl_close($ch);

這樣理論上就完成了單純回應的程式


現在來簡單的玩一下, 單純回應以外的作法, 像是固定的文字做回應, 回應不止一個訊息, 可以回應5個訊息, 而且可以混形態, 像這就是回應一句話再一個位置
<?php
$accessToken = 'Channel Access Token'; #網頁產生的那一串亂碼\

$json_string = file_get_contents('php://input');
$jsonObj = json_decode($json_string);

$type = $jsonObj->events[0]->type;
if($jsonObj->events[0]->type == 'message') {
  $type = $jsonObj->events[0]->message->type;
  $text = $jsonObj->events[0]->message->text;
}

if($type != "text" ) {
  exit;
}

$replyToken = $jsonObj->events[0]->replyToken;

switch($text) {
  case "你在哪裡?" :
    $response_format_text = [
 [
     "type" => "text",
     "text" => "我在你心理!"
 ],
 [
    "type" => "location",
    "title" => "其實我在這裏唱歌",
    "address" => "104台北市中山區松江路199號",
    "latitude" => "25.056865",
    "longitude" => "121.533351"
 ]
];
  break;
  default:
     $response_format_text = [
      [
       "type" => "text",
       "text" => $text
     ]
    ];
  break;
}

$post_data = [
        "replyToken" => $replyToken,
        "messages" => $response_format_text
        ];

$ch = curl_init("https://api.line.me/v2/bot/message/reply");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json; charser=UTF-8',
    'Authorization: Bearer ' . $accessToken
    ));
$result = curl_exec($ch);
curl_close($ch);



再一個範例, 像是我只要說任何東西後面是不要錢, 系統就回應一個確認視窗
<?php
$accessToken = 'Channel Access Token'; #網頁產生的那一串亂碼\

$json_string = file_get_contents('php://input');
$jsonObj = json_decode($json_string);

$type = $jsonObj->events[0]->type;
if($jsonObj->events[0]->type == 'message') {
  $type = $jsonObj->events[0]->message->type;
  $text = $jsonObj->events[0]->message->text;
}

if($type != "text" ) {
  exit;
}

$replyToken = $jsonObj->events[0]->replyToken;

switch($text) {
  case "你在哪裡?" :
    $response_format_text = [
 [
     "type" => "text",
     "text" => "我在你心理!"
 ],
 [
    "type" => "location",
    "title" => "其實我在這裏唱歌",
    "address" => "104台北市中山區松江路199號",
    "latitude" => "25.056865",
    "longitude" => "121.533351"
 ]
];
  break;
  case (mb_substr($text, -3) == "不要錢"):
      $response_format_text = [[
    "type" => "template",
    "altText" => "真的假的?",
    "template" => [
        "type" => "confirm",
        "text" => "真的假的? $text ?",
        "actions" => [
            [
              "type" => "message",
              "label" => "真的",
              "text" => "人人叫我, 真心不騙小狼君!"
            ],
            [
              "type" => "messsage",
              "label" => "假的",
              "text" => "你眼睛業障重!"
            ]
        ]
    ]
  ]];
  break;
  default:
     $response_format_text = [
      [
       "type" => "text",
       "text" => $text
     ]
    ];
  break;
}

$post_data = [
        "replyToken" => $replyToken,
        "messages" => $response_format_text
        ];

$ch = curl_init("https://api.line.me/v2/bot/message/reply");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json; charser=UTF-8',
    'Authorization: Bearer ' . $accessToken
    ));
$result = curl_exec($ch);
curl_close($ch);


下篇就使用官方的line-bot-sdk-php

沒有留言:

張貼留言

LINE訊息機器人實作(二) - 後端(Ubuntu, Apache,PHP)

因為必須要有SSL所以我申請免費的Let's Encrypt, 每申請一次可使用90天 至於如何申請, 真的有非常多的資訊, 我這就不多說了 首先先不管Line官網所提供的SDK, 用手動的先寫簡單的回應程式 callback_test.php <?p...