-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbot.php
More file actions
74 lines (66 loc) · 2.42 KB
/
bot.php
File metadata and controls
74 lines (66 loc) · 2.42 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
<?php
$access_token = 'fLRrTmvRRN/JbtfxjbYstevAaoc6LcFj+jeL52oz4PGxxEdAVPtb+TYZBgqGVAJJlc3GO3jArweqbvgTt7FsSrbURmB1H8K211sgJ5+Zb/Tevxw/TifUMOpY1yCXSkoE+YoKsDzeY0zKfv69tkLGcgdB04t89/1O/w1cDnyilFU=';
// Get POST body content
$content = file_get_contents('php://input');
// Parse JSON
$events = json_decode($content, true);
// Validate parsed JSON data
if (!is_null($events['events'])) {
// Loop through each event
foreach ($events['events'] as $event) {
// Reply only when message sent is in 'text' format
if ($event['type'] == 'message' && $event['message']['type'] == 'text') {
// Get text sent
$text = $event['message']['text'].' - merry christmas';
// Get replyToken
$replyToken = $event['replyToken'];
// Build message to reply back
$messages = [
'type' => 'text',
'text' => $text
];
// 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";
// Check condition to leave Group
if ($event['message']['text'] == 'BoT Leave' && $event[source][type] != 'user') {
// Make a POST Request to Leave
// Check to leaveGroup or leaveRoom
switch ($event[source][type]) {
case "group":
$url = 'https://api.line.me/v2/bot/group/'.$event[source][groupId].'/leave';
break;
case "room":
$url = 'https://api.line.me/v2/bot/room/'.$event[source][roomId].'/leave';
break;
}
$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";
}
}
}
}
// Redeploy this code!
echo "OK";