-
Notifications
You must be signed in to change notification settings - Fork 6
/
LINENotifyController.php
146 lines (130 loc) · 5.1 KB
/
LINENotifyController.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
<?php
namespace MOLiBot\Http\Controllers;
use Illuminate\Http\Request;
use MOLiBot\LINE_Notify_User;
use SoapBox\Formatter\Formatter;
use \GuzzleHttp\Client as GuzzleHttpClient;
use \GuzzleHttp\Exception\TransferException as GuzzleHttpTransferException;
class LINENotifyController extends Controller
{
private $redirect_uri;
private $client_id;
private $client_secret;
public function __construct()
{
$this->redirect_uri = \Config::get('line.line_notify_redirect_uri');
$this->client_id = \Config::get('line.line_notify_client_id');
$this->client_secret = \Config::get('line.line_notify_client_secret');
}
public static function sendMsg($access_token, $msg)
{
$client = new GuzzleHttpClient();
try {
$response = $client->request('POST', 'https://notify-api.line.me/api/notify', [
'headers' => [
'Authorization' => 'Bearer ' . $access_token,
],
'form_params' => [
'message' => $msg,
],
'timeout' => 10
]);
} catch (GuzzleHttpTransferException $e) {
$status = $e->getCode();
if ($status == 400) {
throw new \Exception('400 - Unauthorized request');
} elseif ($status == 401) {
throw new \Exception('401 - Invalid access token');
} elseif ($status == 500) {
throw new \Exception('500 - Failure due to server error');
} else {
throw new \Exception('Processed over time or stopped');
}
}
return $response;
}
public function getStatus($access_token)
{
$client = new GuzzleHttpClient();
try {
$response = $client->request('GET', 'https://notify-api.line.me/api/status', [
'headers' => [
'Authorization' => 'Bearer ' . $access_token,
],
'timeout' => 10
]);
$response = $response->getBody()->getContents();
$formatter = Formatter::make($response, Formatter::JSON);
$json = $formatter->toArray();
return $json;
} catch (GuzzleHttpTransferException $e) {
return $e;
}
}
public function auth(Request $request)
{
$code = $request->query('code', false);
$state = $request->query('state', false);
if ($code) {
$client = new GuzzleHttpClient();
// get access_token
try {
$response = $client->request('POST', 'https://notify-bot.line.me/oauth/token', [
'headers' => [
'User-Agent' => 'MOLi Bot',
'cache-control' => 'no-cache'
],
'form_params' => [
'grant_type' => 'authorization_code',
'code' => $code,
'redirect_uri' => $this->redirect_uri,
'client_id' => $this->client_id,
'client_secret' => $this->client_secret
],
'timeout' => 10
]);
$response = $response->getBody()->getContents();
$formatter = Formatter::make($response, Formatter::JSON);
$json = $formatter->toArray();
$access_token = $json['access_token'];
$success = true;
LINE_Notify_User::create([
'access_token' => $access_token
]);
} catch (GuzzleHttpTransferException $e) {
$status = $e->getCode();
if ($status == 400) {
$error = '400 - Unauthorized request';
return view('LINE/notify_auth', compact('error'));
} else {
$error = 'Other - Processed over time or stopped';
return view('LINE/notify_auth', compact('error'));
}
}
// send a welcome message
try {
$msg = "\n歡迎使用暨大通知,此服務由 MOLi 實驗室維護\n如有疑問可至粉專或群組詢問\nhttps://moli.rocks";
$this->sendMsg($access_token, $msg);
} catch (\Exception $e) {
return $e->getCode();
}
// get status
try {
$json = $this->getStatus($access_token);
LINE_Notify_User::where('access_token', $access_token)
->update([
'targetType' => $json['targetType'],
'target' => $json['target']
]);
} catch (\Exception $e) {
return $e->getCode();
}
return view('LINE/notify_auth', compact('success'));
} else {
// 歡迎畫面
$client_id = $this->client_id;
$redirect_uri = $this->redirect_uri;
return view('LINE/notify_auth', compact('client_id', 'redirect_uri'));
}
}
}