forked from jackrtd/LINE-BOT-PHP-Starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdhammachakdev.php
170 lines (123 loc) · 6.3 KB
/
dhammachakdev.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
define("LINE_MESSAGING_API_CHANNEL_SECRET", 'cf0ce886078d72cb4dac84067938cd1a');
define("LINE_MESSAGING_API_CHANNEL_TOKEN", 'koGlCXsvgSe+hGyZLr+5ggRf9+hy+YUFjzuZIy19/dk2T7yJLkrSB/I+R4Qjipaym4/QlQI20kGjzag554KDZc596JWaSgp5juQALxAZChLyfFiZcJxZsbU/8iRcytkDq02Q6gkxKLCyPB7g9lnIvAdB04t89/1O/w1cDnyilFU=');
require __DIR__."/vendor/autoload.php";
$httpClient = new \LINE\LINEBot\HTTPClient\CurlHTTPClient(LINE_MESSAGING_API_CHANNEL_TOKEN);
$bot = new \LINE\LINEBot($httpClient, ['channelSecret' => LINE_MESSAGING_API_CHANNEL_SECRET]);
$signature = $_SERVER["HTTP_".\LINE\LINEBot\Constant\HTTPHeader::LINE_SIGNATURE];
$body = file_get_contents("php://input");
try {
$events = $bot->parseEventRequest($body, $signature);
} catch (Exception $e) {
var_dump($e);
}
foreach ($events as $event) {
$reply_token = $event->getReplyToken();
$text_received = $event->getText();
$userId = $event->getUserId();
$type = $event->getType();
$timestamp = $event->getTimestamp();
// เก็บ Profile ของ user
$getProfileResponse = $bot->getProfile($userId);
if ($getProfileResponse->isSucceeded()) {
$profile = $getProfileResponse->getJSONDecodedBody();
$displayName = $profile['displayName'];
$pictureUrl = $profile['pictureUrl'];
$statusMessage = $profile['statusMessage'];
}
// สร้าง object ข้อความตอบกลับ
$messages = new \LINE\LINEBot\MessageBuilder\MultiMessageBuilder();
// ที่อยู่ของ server เรา
$url_vstar = "https://www.vstarproject.com/project/linebot/dhammachak/api.php";
// ส่ง data ไปตามนี้
$data_vstar = [
'line_id'=> $userId,
'text_type'=> $type,
'text_received'=> $text_received,
'timestamp'=> $timestamp,
'displayName'=> $displayName,
'pictureUrl'=> $pictureUrl,
'statusMessage'=> $statusMessage
];
$post_vstar = json_encode($data_vstar);
// ส่งข้อมูลไปด้วย curl
$headers_vstar = ['Content-Type: application/json'];
$ch_vstar = curl_init($url_vstar);
curl_setopt($ch_vstar, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch_vstar, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_vstar, CURLOPT_POSTFIELDS, $post_vstar);
curl_setopt($ch_vstar, CURLOPT_HTTPHEADER, $headers_vstar);
curl_setopt($ch_vstar, CURLOPT_FOLLOWLOCATION, 1);
$result_vstar = curl_exec($ch_vstar);
curl_close($ch_vstar);
// รับข้อความตอบกลับ
$res = json_decode($result_vstar,true);
// ข้อความตอบกลับ
$message_vstar = $res['message'];
//ส่งข้อความ
$_msg = new \LINE\LINEBot\MessageBuilder\TextMessageBuilder($message_vstar );
$messages->add($_msg);
/*
$firebase = Firebase::fromServiceAccount(__DIR__.'/puri-contact-firebase-adminsdk-l04g2-fa656ae233.json');
$database = $firebase->getDatabase();
$reference = $database->getReference('dhammachak/contacts');
$data = $reference->getValue();
foreach($data as $value){
if($userId==$value['line_id']){$registed = true;}
}
if(!$registed){
$reference->push([
'line_id' => $userId,
'pictureUrl' => $pictureUrl,
'displayName' => $displayName
]);
}
$round_received = int($text_received);
$round_limit = 500;
if(is_int($round_received) && $round_received > 0 && $round_received <= $round_limit){
$chants = $database->getReference('dhammachak/chants/'.$userId);
$chants->push([
'line_id' => $userId,
'pictureUrl' => $pictureUrl,
'displayName' => $displayName,
'round' => $round_received,
'line_timestamp' => $timestamp
]);
$chants_data = $chants->getValue();
$sum_round = 0;
foreach($chants_data as $value){
$sum_round += int($value['round']);
}
$_msg = new \LINE\LINEBot\MessageBuilder\TextMessageBuilder("ขออนุโมทนาบุญกับการส่งยอดนะคะ คุณ".$displayName."\n\nยอดที่คุณส่งล่าสุด คือ ".$round_received." จบ \n\n**บันทึกเรียบร้อยค่ะ** \n\n(ยอดรวมทั้งหมด ".number_format($sum_round)." จบ)");
$messages->add($_msg);
}elseif(is_int($round_received) && $round_received > $round_limit){
$_msg = new \LINE\LINEBot\MessageBuilder\TextMessageBuilder("ท่านสามารถส่งยอดได้ ไม่เกินครั้งละ $round_limit จบ นะคะ \n\n กรุณาส่งใหม่อีกครั้งค่ะ คุณ".$displayName);
$messages->add($_msg);
}elseif($text_received == "ยอดรวม"){
$round_ref = $database->getReference('dhammachak/chants/'.$userId);
$round_data = $round_ref->getValue();
$sum_round = 0;
foreach($round_data as $value){
$sum_round += int($value['round']);
}
$_msg = new \LINE\LINEBot\MessageBuilder\TextMessageBuilder("ยอดรวมทั้งหมดที่ส่งมาแล้วของคุณ ".$displayName." คือ \n\n ".number_format($sum_round)." จบ \n\nขอกราบอนุโมทนาบุญด้วยนะคะ");
$messages->add($_msg);
}elseif($text_received == "ยกเลิก"){
$database->getReference('dhammachak/chants/'.$userId)->orderByChild('line_timestamp')->limitToLast('1')->getSnapshot();
$round_data = $last_round_ref->getValue();
$last_round = $round_data[0]['round'];
$_msg = new \LINE\LINEBot\MessageBuilder\TextMessageBuilder("ลบยอดล่าสุดของท่าน คือ \n\n ".number_format($last_round)." จบ \n\n เรียบร้อยค่ะ");
$messages->add($_msg);
}else{
$_msg = new \LINE\LINEBot\MessageBuilder\TextMessageBuilder("กรุณาส่งเฉพาะตัวเลข เพื่อบันทึกยอดสวด\n\nหรือถ้าจะดูยอดทั้งหมดที่ส่งไปแล้ว \n\nให้พิมพ์ว่า \"ยอดรวม\" นะคะ คุณ".$displayName);
$messages->add($_msg);
}
*/
// เผื่อมี error feedback
$response = $bot->replyMessage($reply_token, $messages);
echo $response->getHTTPStatus() . ' ' . $response->getRawBody();
}
echo "OK";
/// ฟังก์ชัน เปลี่ยนข้อความเป็นตัวเลข
function int($s){return(int)preg_replace('/[^\-\d]*(\-?\d*).*/','$1',$s);}
?>