forked from best001001/LABTESS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-i.php
More file actions
133 lines (119 loc) · 5.4 KB
/
Copy pathapi-i.php
File metadata and controls
133 lines (119 loc) · 5.4 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
<?php
$access_token = "/1H2Ob/D5/sXjHapP8eNz9WdUVcXnZcFhJcX9BLul3HK9bums95y9oJM315Js+v5BnPLSPHR8DzXLw/ME2cTWm6RDF1aCNyMiUpwvUec4WyPuAc71zWMl+Qy1iIaiHLTEE5TgVO7eZ+reDwt1dIt1QdB04t89/1O/w1cDnyilFU=";
$userId ='U09daf41b2443adfdfcbdfc3b3858f3eb';
// Call LINE API
$content = file_get_contents('php://input');
$events = json_decode($content, true);
// Call Database Mysql
//$database = file_get_contents('http://35.198.246.124/dbtestbme/jsonline.php');
//$datas = json_decode($database, true);
// Call Function GetName
function CallLineGetName($access_token,$userId){
$url = 'https://api.line.me/v2/bot/profile/'.$userId;
$headers = array('Authorization: Bearer ' .$access_token);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$result = CallLineGetName($access_token,$userId);
$json = json_decode($result,TRUE);
if(!is_null($json['displayName'])){
foreach ($json as $type => $value){
if($type == 'displayName'){
$Name = $value; // send reply text name gg
}
}
}
// Call Database Mysql
if (!is_null($datas['id'])) {
foreach ($datas as $type => $value) {
if($type == 'id'){
$id = $value;
}elseif($type == 'temperature'){
$Temperature = $value;
}elseif($type == 'pressure') {
$Pressure = $value;
}elseif($type == 'approx') {
$Approx = $value;
}elseif($type == 'humidity') {
$Humidity = $value;
}elseif($type == 'datetime') {
$datetime = $value;
}
}
}
// Call Events LINE API
if (!is_null($events['events'])) {
foreach ($events['events'] as $event) {
if ($event['type'] == 'message' && $event['message']['type'] == 'text'){
$text = $event['message']['text'];
if ($text == "สวัสดี"
or $text == "สวัสดีครับ"
or $text == "open"
or $text == "hi"
or $text == "Hi"
){
$text = $text." คุณ ".$Name."\n สามารถเลือกเมนูได้เลยครับ ";
}elseif($text == "1"){
$text = "Temperature(อุณหภูมิ)ตอนนี้จะอยู่ที่ ".$Temperature." (°C) ครับ";
}elseif ($text == "2"){
$text = "Pressure(ความดัน)ตอนนี้จะอยู่ที่ ".$Pressure." (°Pa) ครับ";
}elseif ($text == "3"){
$text = " Approx. Altitude(ความสูงจากระดับน้ำทะเล)ตอนนี้จะอยู่ที่ ".$Approx." (°) ครับ";
}elseif ($text == "4"){
$text = "Humidity(ความชื้น)ตอนนี้จะอยู่ที่ ".$Humidity." (°) ครับ";
}elseif ($text == "5"){
$text = "วันที่/เวลา :".$datetime;
}else if($text == "6"){
$text = "อุณหภูมิ ตอนนี้จะอยู่ที่ ".$Temperature." (°C) ครับ \n
- ความดันอยู่ที่ ".$Pressure." (°F) ครับ\n
- ความสูงจากระดับน้ำทะเล ตอนนี้จะอยู่ที่ ".$Approx." (°C) ครับ\n
- ความชื้น ตอนนี้จะอยู่ที่ ".$Humidity." (°F) ครับ\n
- อับเดจล่าสุดเมื่อวันที่ ".$datetime." ครับ";
}else if($text == "ใครสร้าง Mark I"){
$text ="Mark I ถูกสร้างโดย mr.mach ครับ";
}else{
$text = "ขอเวลาเรียนรู้ก่อนนะครับ :)";
}
$replyToken = $event['replyToken'];
$messages = [
'type' => 'text',
'text' => $text
];
}elseif ($event['type'] == 'message' && $event['message']['type'] == 'sticker'){
$id = $event['message']['id'];
$stickerId = $event['message']['stickerId'];
$packageId = $event['message']['packageId'];
$replyToken = $event['replyToken'];
$messages = [
'type' => 'sticker',
'id' => $id,
'stickerId' => $stickerId,
'packageId' => $packageId
];
}
// Make a POST Request to Messaging API to reply to sender
$url = 'https://api.line.me/v2/bot/message/reply';
$data = [
'replyToken' => $replyToken,
'messages' => [$messages],
];
$post = json_encode($data);
$headers = array('Content-Type: application/json', 'Authorization: Bearer ' . $access_token);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
curl_close($ch);
echo $result . "\r\n";
}
}
echo "sucess full";
?>