參考資料 ----
List of available LINE emojis
<?php
// 因為網站是寄放在虛擬主機商那, 所以要指定時區
date_default_timezone_set('Asia/Taipei');
$channelAccessToken = '{從 LINE 官方帳號後台取得的 token}';
// 客戶端傳來的訊息
$bodyMsg = file_get_contents('php://input');
// 寫 log, 檔案在目前目錄下的 log 目錄內
// 檔名為 log_西元年月日.log, 一天一個檔
$sLogfile = './log/log_'.date('Ymd').'.log';
$fp = fopen($sLogfile, "a+");
fwrite($fp , print_r(date('Y-m-d H:i:s').', Recive: '.$bodyMsg."\n", true));
fclose($fp);
$obj = json_decode($bodyMsg, true);
foreach ($obj['events'] as &$event)
{
$userId = $event['source']['userId'];
// 取得 使用者傳來的訊息
$msgUser = $event['message']['text'];
// 回覆含 emoji 的文字訊息
// $ 的位置就是 emoji 的位置, 作用類似格式化輸出
// 在本例, 2 個 $ 的位置分別為 0, 8
$msgReply = '$含 emoji$ 的文字訊息';
$payload = [ 'replyToken' => $event['replyToken'],
'messages' => [ // 一次最多可回覆 5 則訊息, 若超過 5 則, 程式就掛了, 不會有任何回應
['type' => 'text',
'text' => $msgReply,
'emojis' => [ [
"index"=> 0, // emoji 在文字串的位置, 一定要和 $msgReply 中的 $ 的位置一致, 不然整個訊息都不會回覆
"productId" => "5ac1bfd5040ab15980c9b435",
"emojiId" => "001"
],
[
"index" => 8,
"productId" => "5ac1bfd5040ab15980c9b435",
"emojiId" => "005"
]
]
],
[ 'type' => 'text',
'text' => '文字訊息 2'
],
[ 'type' => 'text',
'text' => '文字訊息 3'
],
[ 'type' => 'text',
'text' => '文字訊息 4'
],
[ 'type' => 'text',
'text' => '文字訊息 5'
]
]
];
// Send reply API
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '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_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json',
'Authorization: Bearer ' . $channelAccessToken
]);
$result = curl_exec($ch);
curl_close($ch);
}
?>

沒有留言:
張貼留言