forked from jackrtd/LINE-BOT-PHP-Starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.php
64 lines (51 loc) · 2.21 KB
/
bot.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
<?php
$access_token = 'q0fc4/QpHGEtYHB3qgJLR05WlPsQQY1x730gAnMOakIAw4MxxmNmi8QGyCfbnkL5Ont/yg6FCD62fmsf3uYkxuRZFsJ9P/p79/g8UDzPYhZqY+53Zsw/pHZd35MnStYZTvZ99LcFvhXUvzo11MR0AwdB04t89/1O/w1cDnyilFU=';
// Get POST body content
$content = file_get_contents('php://input');
// Parse JSON
$events = json_decode($content,true);
// Validate parsed JSON date_add
if(!is_null($events['events'])){
foreach($events['events'] as $event){
if($event['type']=='message' && $event['message']['type']=='text'){
// Get text sent
$text = $event['message']['text'];
$url_ibs = "http://www.ibsone.com/project/linebot/puribot/api/translate.php";
$data_ibs = [
'text'=> $text
];
$post_ibs = json_encode($data_ibs);
$headers_ibs = ['Content-Type: application/json'];
$ch_ibs = curl_init($url_ibs);
curl_setopt($ch_ibs, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch_ibs, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_ibs, CURLOPT_POSTFIELDS, $post_ibs);
curl_setopt($ch_ibs, CURLOPT_HTTPHEADER, $headers_ibs);
curl_setopt($ch_ibs, CURLOPT_FOLLOWLOCATION, 1);
$result_ibs = curl_exec($ch_ibs);
curl_close($ch_ibs);
$res = json_decode($result_ibs,true);
$messages = $res['messages'];
// Get reply token
$replyToken = $event['replyToken'];
$url = 'https://api.line.me/v2/bot/message/reply';
$data = [
'replyToken'=> $replyToken,
'messages'=>$messages
];
$post = json_encode($data);
$headers = ['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 "OK";
?>