-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcallback.php
More file actions
167 lines (132 loc) · 4.93 KB
/
Copy pathcallback.php
File metadata and controls
167 lines (132 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/* 輸入申請的Line Developers 資料 */
$channel_id = "{Channel ID}";
$channel_secret = "{Channel Secret}";
$channel_access_token = "{Channel Access Token}";
$myURL = "https://Your Domain/update/"
// 當有人發送訊息給bot時 我們會收到的json
// {
// "events":
// [
// {
// "replyToken": "nHuyWiB7yP5Zw52FIkcQobQuGDXCTA",
// "type": "message",
// "timestamp": 1462629479859,
// "source": {
// "type": "user",
// "userId": "U206d25c2ea6bd87c17655609a1c37cb8"
// },
// "message": {
// "id": "325708",
// "type": "text",
// "text": "Hello, world"
// }
// }
// ]
// }
// 將收到的資料整理至變數
$receive = json_decode(file_get_contents("php://input"));
// 讀取收到的訊息內容
$text = $receive->events[0]->message->text;
// 讀取訊息來源的類型 [user, group, room]
$type = $receive->events[0]->source->type;
// 由於新版的Messaging Api可以讓Bot帳號加入多人聊天和群組當中
// 所以在這裡先判斷訊息的來源
if ($type == "room")
{
// 多人聊天 讀取房間id
$from = $receive->events[0]->source->roomId;
}
else if ($type == "group")
{
// 群組 讀取群組id
$from = $receive->events[0]->source->groupId;
}
else
{
// 一對一聊天 讀取使用者id
$from = $receive->events[0]->source->userId;
}
// 讀取訊息的型態 [Text, Image, Video, Audio, Location, Sticker]
$content_type = $receive->events[0]->message->type;
// 準備Post回Line伺服器的資料
$header = ["Content-Type: application/json", "Authorization: Bearer {" . $channel_access_token . "}"];
// 回覆訊息
reply($content_type, $text);
function reply($content_type, $message) {
global $header, $from, $receive;
$url = "https://api.line.me/v2/bot/message/push";
$data = ["to" => $from, "messages" => array(["type" => "text", "text" => $message])];
switch($content_type) {
case "text" :
$content_type = "文字訊息";
$data = ["to" => $from, "messages" => array(["type" => "text", "text" => $message])];
break;
case "image" :
$content_type = "圖片訊息";
$message = getObjContent("jpeg"); // 讀取圖片內容
$data = ["to" => $from, "messages" => array(["type" => "image", "originalContentUrl" => $message, "previewImageUrl" => $message])];
break;
case "video" :
$content_type = "影片訊息";
$message = getObjContent("mp4"); // 讀取影片內容
$data = ["to" => $from, "messages" => array(["type" => "video", "originalContentUrl" => $message, "previewImageUrl" => $message])];
break;
case "audio" :
$content_type = "語音訊息";
$message = getObjContent("mp3"); // 讀取聲音內容
$data = ["to" => $from, "messages" => array(["type" => "audio", "originalContentUrl" => $message[0], "duration" => $message[1]])];
break;
case "location" :
$content_type = "位置訊息";
$title = $receive->events[0]->message->title;
$address = $receive->events[0]->message->address;
$latitude = $receive->events[0]->message->latitude;
$longitude = $receive->events[0]->message->longitude;
$data = ["to" => $from, "messages" => array(["type" => "location", "title" => $title, "address" => $address, "latitude" => $latitude, "longitude" => $longitude])];
break;
case "sticker" :
$content_type = "貼圖訊息";
$packageId = $receive->events[0]->message->packageId;
$stickerId = $receive->events[0]->message->stickerId;
$data = ["to" => $from, "messages" => array(["type" => "sticker", "packageId" => $packageId, "stickerId" => $stickerId])];
break;
default:
$content_type = "未知訊息";
break;
}
$context = stream_context_create(array(
"http" => array("method" => "POST", "header" => implode(PHP_EOL, $header), "content" => json_encode($data), "ignore_errors" => true)
));
file_get_contents($url, false, $context);
}
function getObjContent($filenameExtension){
global $channel_access_token, $receive;
$objID = $receive->events[0]->message->id;
$url = 'https://api.line.me/v2/bot/message/'.$objID.'/content';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer {' . $channel_access_token . '}',
));
$json_content = curl_exec($ch);
curl_close($ch);
if (!$json_content) {
return false;
}
$fileURL = './update/'.$objID.'.'.$filenameExtension;
$fp = fopen($fileURL, 'w');
fwrite($fp, $json_content);
fclose($fp);
if ($filenameExtension=="mp3"){
require_once("../getID3/getid3/getid3.php");
$getID3 = new getID3;
$fileData = $getID3->analyze($fileURL);
//$audioInfo = var_dump($fileData);
$playSec = floor($fileData["playtime_seconds"]);
$re = array($myURL.$objID.'.'.$filenameExtension, $playSec*1000);
return $re;
}
return $myURL.$objID.'.'.$filenameExtension;
}
?>