forked from yatima12/IT-BOT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest2.php
97 lines (91 loc) · 3.19 KB
/
test2.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
<?php
$strAccessToken = "0i2v/wM+ufakKsO4rEPGJlBaRvRoQODlNlfrTyYbhx7dVdr9umhk8d4Ou0y/fQ6NlMEzKG2y98FkIgPOmWzQxXKTxmbdpwJQ786nmaTtMsK0PzEwR/+zmd/ipByxbxH2WOQDmACqgmyfeCwF1M4BFwdB04t89/1O/w1cDnyilFU=";
$content = file_get_contents('php://input');
$arrJson = json_decode($content, true);
$strUrl = "https://api.line.me/v2/bot/message/reply";
$_userId = $arrJson['events'][0]['source']['userId'];
$_msg = $arrJson['events'][0]['message']['text'];
$arrHeader = array();
$arrHeader[] = "Content-Type: application/json";
$arrHeader[] = "Authorization: Bearer {$strAccessToken}";
$filename = ''.$_userId.'.txt';
$_last = file(''.$_userId.'.txt');
$api_key="raGvU0tka_kLPSFwL7ObSQKwZGR-91G2";
$url = 'https://api.mlab.com/api/1/databases/byone/collections/linebot?apiKey='.$api_key.'';
$json = file_get_contents('https://api.mlab.com/api/1/databases/byone/collections/linebot?apiKey='.$api_key.'&q={"question":"'.$_msg.'"}');
$data = json_decode($json);
$isData=sizeof($data);
if (strpos($_msg, '@') !== false) {
$x_tra = str_replace("@","", $_msg);
$pieces = explode("&", $x_tra);
$_question=str_replace("","",$pieces[0]);
$_answer=str_replace("","",$pieces[1]);
//Post New Data
$newData = json_encode(
array(
'question' => $_question,
'value' => "1",
'answer'=> $_answer
)
);
$opts = array(
'http' => array(
'method' => "POST",
'header' => "Content-type: application/json",
'content' => $newData
)
);
$context = stream_context_create($opts);
$returnValue = file_get_contents($url,false,$context);
$arrPostData = array();
$arrPostData['replyToken'] = $arrJson['events'][0]['replyToken'];
$arrPostData['messages'][0]['type'] = "text";
$arrPostData['messages'][0]['text'] = 'Save';
}else{
if($isData >0){
foreach($data as $rec){/*
$arrPostData = array();
$arrPostData['replyToken'] = $arrJson['events'][0]['replyToken'];
$arrPostData['messages'][0]['type'] = "text";
$arrPostData['messages'][0]['text'] = $rec->answer;*/
}
}else{
$newData = json_encode(
array(
'value' => "1",
'question' => $_last,
'answer' => $_msg,
)
);
$opts = array(
'http' => array(
'method' => "POST",
'header' => "Content-type: application/json",
'content' => $newData
)
);
$context = stream_context_create($opts);
$returnValue = file_get_contents($url,false,$context);
}
}
$channel = curl_init();
curl_setopt($channel, CURLOPT_URL,$strUrl);
curl_setopt($channel, CURLOPT_HEADER, false);
curl_setopt($channel, CURLOPT_POST, true);
curl_setopt($channel, CURLOPT_HTTPHEADER, $arrHeader);
curl_setopt($channel, CURLOPT_POSTFIELDS, json_encode($arrPostData));
curl_setopt($channel, CURLOPT_RETURNTRANSFER,true);
curl_setopt($channel, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($channel);
curl_close ($channel);
sleep(0);
if (file_exists($filename)) {
$myfile = fopen(''.$_userId.'.txt', "w+") or die("Unable to open file!");
fwrite($myfile, $_msg);
fclose($myfile);
} else {
$myfile = fopen(''.$_userId.'.txt', "x+") or die("Unable to open file!");
fwrite($myfile, $_msg);
fclose($myfile);
}
?>