-
Notifications
You must be signed in to change notification settings - Fork 26
/
lineBot.php
156 lines (139 loc) · 3.7 KB
/
lineBot.php
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
<?php
// CREATE BY NONTACHAI KORNINAI
// 01 OCTOBER 2016
//
// UPDATE BY NONTACHAI KORNINAI
// 25 MARCH 2018
require_once __DIR__ . '/setting.php';
class Linebot {
private $channelAccessToken;
private $channelSecret;
private $webhookResponse;
private $webhookEventObject;
private $apiReply;
private $apiPush;
public function __construct(){
$this->channelAccessToken = Setting::getChannelAccessToken();
$this->channelSecret = Setting::getChannelSecret();
$this->apiReply = Setting::getApiReply();
$this->apiPush = Setting::getApiPush();
$this->webhookResponse = file_get_contents('php://input');
$this->webhookEventObject = json_decode($this->webhookResponse);
}
private function httpPost($api,$body){
$ch = curl_init($api);
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($body));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charser=UTF-8',
'Authorization: Bearer '.$this->channelAccessToken));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
public function reply($text){
$api = $this->apiReply;
$webhook = $this->webhookEventObject;
$replyToken = $webhook->{"events"}[0]->{"replyToken"};
$body["replyToken"] = $replyToken;
$body["messages"][0] = array(
"type" => "text",
"text"=>$text
);
$result = $this->httpPost($api,$body);
return $result;
}
public function push($body){
$api = $this->apiPush;
$result = $this->httpPost($api, $body);
return $result;
}
public function pushText($to, $text){
$body = array(
'to' => $to,
'messages' => [
array(
'type' => 'text',
'text' => $text
)
]
);
$this->push($body);
}
public function pushImage($to, $imageUrl, $previewImageUrl = false){
$body = array(
'to' => $to,
'messages' => [
array(
'type' => 'image',
'originalContentUrl' => $imageUrl,
'previewImageUrl' => $previewImageUrl ? $previewImageUrl : $imageUrl
)
]
);
$this->push($body);
}
public function pushVideo($to, $videoUrl, $previewImageUrl){
$body = array(
'to' => $to,
'messages' => [
array(
'type' => 'video',
'originalContentUrl' => $videoUrl,
'previewImageUrl' => $previewImageUrl
)
]
);
$this->push($body);
}
public function pushAudio($to, $audioUrl, $duration){
$body = array(
'to' => $to,
'messages' => [
array(
'type' => 'audio',
'originalContentUrl' => $audioUrl,
'duration' => $duration
)
]
);
$this->push($body);
}
public function pushLocation($to, $title, $address, $latitude, $longitude){
$body = array(
'to' => $to,
'messages' => [
array(
'type' => 'location',
'title' => $title,
'address' => $address,
'latitude' => $latitude,
'longitude' => $longitude
)
]
);
$this->push($body);
}
public function getMessageText(){
$webhook = $this->webhookEventObject;
$messageText = $webhook->{"events"}[0]->{"message"}->{"text"};
return $messageText;
}
public function postbackEvent(){
$webhook = $this->webhookEventObject;
$postback = $webhook->{"events"}[0]->{"postback"}->{"data"};
return $postback;
}
public function getUserId(){
$webhook = $this->webhookEventObject;
$userId = $webhook->{"events"}[0]->{"source"}->{"userId"};
return $userId;
}
public function getGroupId(){
$webhook = $this->webhookEventObject;
$groupId = $webhook->{"events"}[0]->{"source"}->{"groupId"};
return $groupId;
}
}