`LINE Notify` 是一個服務,允許你透過 `LINE` 的 `API` 傳送通知。以下是如何在 `Laravel` 中實現這項服務的詳細步驟。
* 生成並重定向至 `OAuth2 URL` 地址
* 儲存與用戶相關的 `OAuth2` 存取權杖(Access Token)
* 在需要發送通知時,調用通知 `API`
* (如果有檢視連接狀態的頁面)通過連接狀態 `API` 顯示連接狀態
* (當連接服務需要取消通知時)調用通知撤銷 `API`
## 申請服務
前往[Line Notify](https://notify-bot.line.me/my/services/)申請服務
1. 新增服務

2. 取得 `Client ID` 以及 `Client Secret`

## 1. 設置 OAuth2 驗證
首先,你需要設置 `OAuth2` 驗證流程。這需要向 `LINE` 註冊你的應用程式,然後獲取一個客戶端 `ID` 和密鑰。接著你可以創建一個 `URL`,將用戶導向 `LINE` 的 `OAuth2` 授權端點,並帶上客戶端 `ID` 和其他必要參數。在 `Laravel` 中,這可能會像下面這樣:
```php=
public function redirectToLineNotify($redirectUri)
{
// Generate a random state value.
$state = Str::random(40);
// Save the state value in the session.
session()->put('state', $state);
// Generate the query string.
$parameters = http_build_query([
'response_type' => 'code',
'client_id' => 'your_line_client_id',
'redirect_uri' => $redirectUri,
'scope' => 'notify',
'state' => $state,
]);
// Redirect to the LINE OAuth endpoint.
return redirect()->away('https://notify-bot.line.me/oauth/authorize?' . $parameters);
}
```
## 2. 儲存 Access Token
當用戶通過 `LINE` 驗證後,`LINE` 會將他們重新導向回你的服務,並帶上一個授權碼。你可以使用這個授權碼向 `LINE` 的 `token` 端點請求一個 `access token`,並將其儲存在資料庫中。這可能會看起來像這樣:
```php=
public function handleLineNotifyCallback($state, $code, $redirectUri)
{
if (session()->get('state') !== $state) {
abort(403, 'Invalid state parameter. Possible CSRF attack.');
}
session()->forget('state');
$response = Http::asForm()->post('https://notify-bot.line.me/oauth/token', [
'grant_type' => 'authorization_code',
'code' => $code,
'redirect_uri' => $redirectUri,
'client_id' => 'your_line_client_id',
'client_secret' => 'your_line_client_secret',
]);
if ($response->successful()) {
$accessToken = $response->json()['access_token'];
return $accessToken;
}
return null;
}
public function lineNotifyCallback(Request $request)
{
$redirectUri = 'your_redriect_url'
$accessToken = $this->lineNotifyService->handleLineNotifyCallback($request->state, $request->code, $redirectUri);
$user = Auth::user();
if ($accessToken) {
$user->update([
'line_notify_token' => $accessToken,
]);
}
}
```
## 3. 發送通知
現在,當你需要向用戶發送通知時,你可以使用之前儲存的 `access token` 調用 `LINE` 的通知 `API`。這可能會看起來像這樣:
```php=
/**
* sends a message to a user on Line using the Line Notify API.
*/
public function sendLineNotification(User $user, $message)
{
$response = Http::withHeaders([
'Authorization' => 'Bearer ' . $user->line_notify_token, // The access token stored for the user
])->asForm()->post('https://notify-api.line.me/api/notify', [
'message' => $message,
]);
return $response->json();
}
```
## 4. 顯示狀態(如果有)
如果你的 `Laravel` 應用程式需要通過 `API` 來檢查並顯示 `LINE Notify` 的連接狀態,你可以這樣做
```php=
/**
* checks the connection status of the user's Line Notify token.
*/
public function checkLineConnectionStatus(User $user)
{
$response = Http::withHeaders([
'Authorization' => 'Bearer ' . $user->line_notify_token, // The access token stored for the user
])->get('https://notify-api.line.me/api/status');
return $response->json();
}
```
## 5. 撤銷通知
如果用戶希望取消通知,你可以調用 `LINE` 的撤銷 `API`,並使用之前儲存的 `access token`。這可能看起來像這樣:
```php=
/**
* revokes the user's Line Notify token.
*/
public function revokeLineNotification($user)
{
$response = Http::withHeaders([
'Authorization' => 'Bearer ' . $user->line_notify_token,
])->post('https://notify-api.line.me/api/revoke');
return $response->successful();
}
```
## 結論

`LINE Notify` 提供了一種便利的方式來發送 `LINE` 通知。在 `Laravel` 中整合 `LINE Notify` 的過程需要設置 `OAuth2` 驗證,儲存 `access token`,並調用相應的 `API` 進行通知或撤銷操作。希望這篇文章對你有所幫助!