-
Notifications
You must be signed in to change notification settings - Fork 35
/
webhook_api.php
138 lines (100 loc) · 4.9 KB
/
webhook_api.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
<?php
$channelAccessToken = 'M2a6HOz5+QpBbctXqOzlOSSsv9t+s3OT4SJUyy1QzkbOoH9NioT1rmEY6SYm6PeIiZ7GY2N12odRGxWAHhuSFoePGOE/qLJKhrq98Nwfb6zVLgC7Np9YEFr6tBkDIlVENCDg3NPbb4t0e1tBGaet2QdB04t89/1O/w1cDnyilFU='; // Access Token ค่าที่เราสร้างขึ้น
$request = file_get_contents('php://input'); // Get request content
$request_json = json_decode($request, true); // Decode JSON request
foreach ($request_json['events'] as $event)
{
if ($event['type'] == 'message')
{
if($event['message']['type'] == 'text')
{
$text = $event['message']['text'];
$texts = explode(" ", $text);
if($text == "@บอท" || $texts[0] == "@บอท"){
if($texts[1] == "อ่านเลขมิเตอร์ไฟฟ้า"){
$reply_message = mySQL_selectAll('http://bot.kantit.com/json_select_users.php');
}else if($texts[1] == "อ่านเลขมิเตอร์น้ำ"){
$reply_message = mySQL_selectAll('http://bot.kantit.com/json_select_users.php?sid='.$texts[3]);
}else if($texts[1] == "ขอสรุปการเปรียบเทียบรการใช้พลังงาน"){
$reply_message = mySQL_selectFTP('http://bot.kantit.com/json_select_ftp.php?sid='.$texts[4]);
}else if($texts[1] == "เปิดน้ำทั้งหมด"){
$reply_message = mySQL_SET('http://bot.kantit.com/json_set.php?cmd=on');
}else if($texts[1] == "ปิดน้ำทั้งหมด"){
$reply_message = mySQL_SET('http://bot.kantit.com/json_set.php?cmd=off');
}else{
$reply_message .= "ฉันมีบริการให้คุณสั่งได้ ดังนี้...\n";
$reply_message .= "พิมพ์ว่า \"@บอท อ่านเลขมิเตอร์ไฟฟ้า\"\n";
$reply_message .= "พิมพ์ว่า \"@บอท อ่านเลขมิเตอร์น้ำ\"\n";
$reply_message .= "พิมพ์ว่า \"@บอท ขอสรุปการเปรียบเทียบรการใช้พลังงาน\"\r\n";
}
}
} else {
$reply_message = 'ฉันได้รับ "'.$event['message']['type'].'" ของคุณแล้ว!';
}
} else {
$reply_message = 'ฉันได้รับ Event "' . $event['type'] . '" ของคุณแล้ว!';
}
//if($reply_message == null || $reply_message == ""){ $reply_message = 'ขออภัยฉันไม่สามารถตอบกลับข้อความ "'. $text . '" ของคุณ!'; }
// reply message
$post_header = array('Content-Type: application/json', 'Authorization: Bearer ' . $channelAccessToken);
$data = ['replyToken' => $event['replyToken'], 'messages' => [['type' => 'text', 'text' => $reply_message]]];
$post_body = json_encode($data);
// reply method type-1 vs type-2
$send_result = reply_message_1('https://api.line.me/v2/bot/message/reply', $post_header, $post_body);
//$send_result = reply_message_2('https://api.line.me/v2/bot/message/reply', $post_header, $post_body);
}
function mySQL_selectAll($url)
{
$result = file_get_contents($url);
$result_json = json_decode($result, true); //var_dump($result_json);
$data = "ผลลัพธ์:\r\n";
foreach($result_json as $values) {
$data .= $values["user_stuid"] . " " . $values["user_firstname"] . " " . $values["user_lastname"] . "\r\n";
}
return $data;
}
function mySQL_selectFTP($url)
{
$result = file_get_contents($url);
$result_json = json_decode($result, true); //var_dump($result_json);
$data = "ผลลัพธ์:\r\n";
foreach($result_json as $values) {
$data .= $values["user_password"] . "\r\n";
}
return "รหัส FTP ของคุณคือ ".$data;
}
function mySQL_SET($url)
{
$result = file_get_contents($url);
$result_json = json_decode($result, true); //var_dump($result_json);
$data = "ผลลัพธ์:\r\n";
foreach($result_json as $values) {
$data .= $values["results"] . "\r\n";
}
return "cmd=".$data;
}
function reply_message_1($url, $post_header, $post_body)
{
$context = stream_context_create([
'http' => [
'method' => 'POST',
'header' => $post_header,
'content' => $post_body,
],
]);
$result = file_get_contents($url, false, $context);
return $result;
}
function reply_message_2($url, $post_header, $post_body)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $post_header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_body);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
?>