-
Notifications
You must be signed in to change notification settings - Fork 6
/
bot.php
158 lines (136 loc) · 6.11 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
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
<?php
namespace LINE2Discord;
define("DEBUG", false);
require_once 'Settings.php';
require_once 'LineMessage.php';
require_once 'Logger.php';
use LINE2Discord\Settings as Settings;
use LINE2Discord\LineMessage as LineMessage;
new Bot;
class Bot
{
private $configure;
private $lineMessage;
private $logger;
public function __construct()
{
$this->logger = new Logger();
// 設定をロード
$this->configure = new Settings();
// jsonを受け取り
$encoded_json = file_get_contents('php://input');
// ハッシュチェック
$this->logger->log(0, "Check hash");
if(!$this->checkHash($encoded_json))
return;
// jsonをオブジェクトにする
$this->logger->log(0, "Convert json to object");
$decoded_json = json_decode($encoded_json)->{"events"};
$this->logger->log(0, \json_encode($decoded_json, JSON_UNESCAPED_UNICODE));
foreach($decoded_json as $json) {
$this->lineMessage = new LineMessage($this->configure, $this->logger);
$this->lineMessage->loadJson($json);
// 特定のグループからの送信のみに絞る
$this->logger->log(0, "Validation input");
if(!$this->validation())
return;
// 各処理へ振り分ける
$this->logger->log(0, "Distribute each process");
$this->distribute();
}
}
private function checkHash($json)
{
if(DEBUG) return true;
$hash = hash_hmac('sha256', $json, $this->configure->secret, true);
$sig = base64_encode($hash);
$header = \getallheaders();
$this->logger->log(0, \json_encode($header, JSON_UNESCAPED_UNICODE));
$compSig = $header['X-Line-Signature'];
$this->logger->log(0, "SIG: {$sig}");
$this->logger->log(0, "compSIG: {$compSig}");
$this->logger->log(0, "All: " . var_export(\getallheaders()));
if($sig != $compSig) {
$this->logger->log(2, "Wrong hash");
return false;
}
$this->logger->log(0, "Hash is correct");
return true;
}
private function validation()
{
if(DEBUG) return true;
if($this->lineMessage->sourceType == "group"
&& $this->lineMessage->roomId == $this->configure->groupId)
return true;
$this->logger->log(0, "Failed to validate");
return false;
}
private function distribute()
{
switch ($this->lineMessage->messageType) {
case 'image':
case 'video':
case 'audio':
case 'file':
$this->lineMessage->getFile();
case 'text':
case 'sticker':
$this->sendMessage();
break;
default:
$this->logger->log(2, "Something different message :" . $this->lineMessage->messageType);
break;
}
}
private function sendMessage()
{
$this->logger->log(0, "Send message");
$client = \curl_init($this->configure->discordUrl);
\curl_setopt($client, CURLOPT_POST, true);
\curl_setopt($client, CURLOPT_RETURNTRANSFER, true);
\curl_setopt($client, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
$name = (!empty($this->configure->botName)) ? array('username' => "{$this->lineMessage->userName} by {$this->configure->botName}") : array('username' => "{$this->lineMessage->userName}");
$thumbnail = (!empty($this->configure->botThumbnail)) ? array('avatar_url' => $this->configure->botThumbnail) : array();
$postfield = array('content' => 'Whoops, There is nothing.');
switch ($this->lineMessage->messageType) {
case 'text':
$postfield = array('content' => "{$this->lineMessage->text}");
break;
case 'image':
case 'video':
case 'audio':
case 'file':
// ファイルサイズチェック
if($this->lineMessage->isFileOverSize())
$postfield = array('content' => $this->configure->uploadURI . $this->lineMessage->fileName);
else{
$fileName = $this->configure->uploadLocation . $this->lineMessage->fileName;
$finfo = new \finfo(FILEINFO_MIME_TYPE);
$postfield = array('file' => new \CURLFILE($fileName, $finfo->file($fileName), $this->lineMessage->fileName));
}
break;
/*case 'sticker':
// TODO: 動くステッカーや音声つきステッカーは別URL
$sticker = \str_replace("{stickerID}", $this->lineMessage->stickerId, $this->configure->stickerURL);
$postfield = array('content' => $sticker);
break;*/
default:
$this->logger->log(4, "Unknown message type {$this->lineMessage->messageType}");
return;
break;
}
$field = \array_merge($name, $thumbnail, $postfield);
\curl_setopt($client, CURLOPT_POSTFIELDS, $field);
$this->logger->log(0, "Set webhook options");
$this->logger->log(0, $field['avatar_url']);
if(!\curl_exec($client)){
$errorNo = \curl_errno($client);
$error = \curl_error($client);
$info = \curl_getinfo($client);
if($info['http_code'] !== 204)
$this->logger->log(4, "Error sending to discord {$errorNo}({$info['http_code']}) {$error}");
}
}
}
?>