-
Notifications
You must be signed in to change notification settings - Fork 6
/
LineMessage.php
184 lines (170 loc) · 6.22 KB
/
LineMessage.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
namespace LINE2Discord;
require_once 'ReadOnlyTrait.php';
require_once 'Logger.php';
require_once 'MIME.php';
use LINE2Discord\ReadOnlyTrait as ReadOnlyTrait;
use LINE2Discord\Logger as Logger;
class LineMessage
{
use ReadOnlyTrait;
private $configure;
private $logger;
private $replyToken;
private $messageId;
private $sourceType;
private $roomId;
private $userId;
private $userName;
private $messageType;
private $text;
private $stickerId;
private $fileName;
private $fileSize;
/**
* __construct
*
* @param mixed $configure Settingsのインスタンス
* @param mixed $logger Loggerのインスタンス
*
* @return void
*/
public function __construct($configure, $logger)
{
$this->configure = $configure;
$this->logger = $logger;
}
/**
* JSONデータをロードします
*
* @param mixed $json デコードしたJSON
*
* @return void
*/
public function loadJson($json)
{
//if(DEBUG) return;
$this->replyToken = $json->{"replyToken"};
$this->messageId = $json->{"message"}->{"id"};
$this->sourceType = $json->{"source"}->{"type"};
$this->roomId = $this->sourceType == "user" ? $json->{"source"}->{"userId"} : $json->{"source"}->{"groupId"};
$this->userId = $json->{"source"}->{"userId"};
$this->messageType = $json->{"message"}->{"type"};
$this->fileName = ($this->messageType == "file") ? $json->{"message"}->{"fileName"} : null;
$this->fileSize = ($this->messageType == "file") ? $json->{"message"}->{"fileSize"} : null;
$this->loadText($json);
$this->loadStickerId($json);
$this->getUserName();
}
/**
* 存在する場合テキストをロードします
*
* @param mixed $json デコードしたJSON
*
* @return void
*/
private function loadText($json)
{
if($this->messageType == "text")
$this->text = $json->{"message"}->{"text"};
}
/**
* 存在する場合はステッカーをロードします
*
* @param mixed $json デコードしたJSON
*
* @return void
*/
private function loadStickerId($json)
{
if($this->messageType == "sticker")
$this->stickerId = $json->{"stickerId"};
}
/**
* ユーザー名をロードします
*
* @return void
*/
private function getUserName()
{
$this->logger->log(0, "Get user name");
$url = \str_replace("{userId}", $this->userId, \str_replace("{roomId}", $this->roomId, $this->configure->userNameURL));
$headers = array(
"Authorization: Bearer {$this->configure->token}",
);
$option = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 3,
];
$ch = curl_init($url);
\curl_setopt_array($ch, $option);
\curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$json = \curl_exec($ch);
$errno = \curl_errno($ch);
$err = \curl_error($ch);
$info = \curl_getinfo($ch);
if($info['http_code'] != 200 || DEBUG){
$this->logger->log(0, $url);
$this->logger->log((DEBUG) ? 0 : 4 ,"{$err}, {$errno}({$info['http_code']})");
$this->logger->log((DEBUG) ? 0 : 4 , "group: {$this->roomId}, user: {$this->userId}");
$this->logger->log(0, $json);
}
$jsonObj = json_decode($json);
$this->userName = ($info['http_code'] == 200) ? $jsonObj->{"displayName"} : "No name";
}
/**
* ファイルをダウンロードし、ファイル名をロードします
*
* @return void
*/
public function getFile()
{
$this->logger->log(0, "Get file");
// ファイル名を決定する
$fileName = '';
if($this->messageType == 'file')
// メッセージタイプがfileの場合は拡張子もfileNameに含んでいる
$fileName = $this->fileName;
else {
// 拡張子を決定する
$extension = '';
switch ($this->messageType) {
case 'image':
$extension = 'jpg';
break;
case 'audio':
$extension = 'm4a';
break;
case 'video':
$extension = 'mp4';
break;
default:
$this->logger->log(4, "Unknown message type {$this->messageType}");
return;
}
$fileName = $this->messageId . "." . $extension;
}
// ファイルを作成
$fp = fopen($this->configure->uploadLocation . $fileName, "w+");
$url = str_replace("{messageID}", $this->messageId, $this->configure->downloadURL);
$ch = \curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer {{$this->configure->token}}"));
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_exec($ch);
\curl_close($ch);
fclose($fp);
$this->logger->log(0, "Save file as {$fileName} on {$this->configure->uploadLocation}");
$this->fileName = $fileName;
}
/**
* ファイルサイズがDiscordの許容サイズか判定します
*
* @return void
*/
public function isFileOverSize()
{
return \filesize($this->configure->uploadLocation . $this->fileName) > $this->configure->maxFileSize;
}
}
?>