forked from jackrtd/LINE-BOT-PHP-Starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathibs_news_push_msg_user.php
103 lines (80 loc) · 3.4 KB
/
ibs_news_push_msg_user.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
<?php
header('Access-Control-Allow-Origin: *');
define("LINE_MESSAGING_API_CHANNEL_SECRET", '3e4001657a33f71bb310ffba52370d8e');
define("LINE_MESSAGING_API_CHANNEL_TOKEN", '2Kv6ENeGC9MlIuiCLnePFEfbvkntSmNgCXhhel73PHUZVRoJIkcorESN4CcusUDzS+whtgnSRimkzU/fkFQb7b8v+4t0FLrHqUDHhRjJohCqcIm7sJYxrC9vxpUxBzXfpXeo+y7BhalZIS/OFhx14wdB04t89/1O/w1cDnyilFU=');
require __DIR__."/vendor/autoload.php";
$MessageType = $_REQUEST['msg_type'];
$UserId = $_REQUEST['UserId'];
$text_send = $_REQUEST['text_send'];
$thumbnail = $_REQUEST['thumbnail'];
if(!$text_send){
exit();
}
// user id ของ puri = 'U02a2cb394330d90571a21b09f2c230ea'
$httpClient = new \LINE\LINEBot\HTTPClient\CurlHTTPClient(LINE_MESSAGING_API_CHANNEL_TOKEN);
$bot = new \LINE\LINEBot($httpClient, ['channelSecret' => LINE_MESSAGING_API_CHANNEL_SECRET]);
// connect กับ firebase
$firebase = Firebase::fromServiceAccount(__DIR__.'/puri-contact-firebase-adminsdk-l04g2-fa656ae233.json');
$database = $firebase->getDatabase();
// สร้าง Object ข้อความตอบกลับ
$messages = new \LINE\LINEBot\MessageBuilder\MultiMessageBuilder();
if($MessageType == 'text'){
// ข้อความ
$_msg = new \LINE\LINEBot\MessageBuilder\TextMessageBuilder($text_send);
$messages->add($_msg);
}elseif($MessageType == 'image'){
// รูป
$imageMessageBuilder = new \LINE\LINEBot\MessageBuilder\ImageMessageBuilder($text_send,$thumbnail);
$messages->add($imageMessageBuilder);
}elseif($MessageType == 'video'){
// Video
$VideoMessageBuilder = new \LINE\LINEBot\MessageBuilder\VideoMessageBuilder($text_send,$thumbnail);
$messages->add($VideoMessageBuilder);
}
$response = $bot->pushMessage($UserId, $messages);
date_default_timezone_set('Asia/Bangkok');
$current_time = date("Y/m/d H:i:s");
$date = new DateTime();
$timestamp = $date->getTimestamp();
// เก็บข้อมูลที่เต้าส่งมา Push to Firebase
$chat_history = $database->getReference('ibs/line/chat_all');
$chat_history->push([
'UserId' => $UserId,
'MessageType' => $MessageType,
'Text' => $text_send,
'Thumbnail' => $thumbnail,
'current_time' => $current_time,
'timestamp' => $timestamp,
'DisplayName' => 'admin',
'Admin' => 1,
]);
// check ดูว่ามีรายชื่อ line id นี้ ใน firebase หรือยัง
$ref_user = $database->getReference('ibs/line/contact/user');
$data = $ref_user->getValue();
foreach($data as $key => $value){
if($UserId==$value['UserId']){
$row_key = $key;
}
}
// เก็บข้อมูลที่เต้าส่งมา Push to Firebase
$chat_history_user = $database->getReference("ibs/line/contact/user/{$row_key}/ChatHistory");
$chat_history_user->push([
'UserId' => $UserId,
'MessageType' => $MessageType,
'Text' => $text_send,
'Thumbnail' => $thumbnail,
'current_time' => $current_time,
'timestamp' => $timestamp,
'DisplayName' => 'admin',
'Admin' => 1,
]);
// ส่งข้อความมาที่เครื่อง samsung taba ด้วย
//$response = $bot->pushMessage('Ua2bdf85b0466beeb8c8af8fbccfba5df', $textMessageBuilder);
// โอ๊ต ID U12fd0233ec75ef62153d1ee3b31f1037
$jsonresponse = [
"success"=>true,
"feedback"=>"ส่งข้อความเรียบร้อย",
"bot_response"=>$response
];
echo json_encode($jsonresponse);
?>