Please see this repo for instructions on how to submit a channel proposal.
This package makes it easy to send notifications using LINENotify with Laravel 8+.
To use this package, you must have a LINENotify account.
You can install the package via composer:
composer require hsuan1117/line-notify-channel
Optionally include a few steps how users can set up the service.
Add your LINE Notify client ID and client secret to your .env
file:
LINE_NOTIFY_CLIENT_ID=your-client-id
LINE_NOTIFY_CLIENT_SECRET=your-client-secret
Add to via() method in your notification class:
public function via($notifiable)
{
return [LineNotifyChannel::class];
}
Add routeNotificationForLINENotify() method in your notification class:
public function routeNotificationForLINENotify($notifiable)
{
return $notifiable->line_notify_token;
}
Add to toLINENotify() method in your notification class:
public function toLINENotify(object $notifiable): LINENotifyMessage
{
return new LINENotifyMessage('Hello World!');
}
Here is an example, use it at your own scenario
- Create a route for the callback URL
Route::prefix('line-notify')->group(function () {
Route::post('gen', [LineNotifyController::class, 'generateLinkToken'])->name('line-notify.callback')->middleware('auth:sanctum');
Route::post('link', [LineNotifyController::class, 'link'])->name('line-notify.callback');
});
- Create a controller for the callback URL
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\URL;
class LineNotifyController extends Controller
{
public function generateLinkToken() {
$token = Str::random(40);
auth()->user()->update([
'link_token' => $token,
]);
return $token;
}
public function link(Request $request)
{
$code = $request->get('code');
$state = $request->get('state');
$user = User::where('link_token', $state)->firstOrFail();
$response = Http::asForm()->post('https://notify-bot.line.me/oauth/token', [
'grant_type' => 'authorization_code',
'code' => $code,
'redirect_uri' => config('app.url') . '/line-notify/link',
'client_id' => config('services.line-notify.client_id'),
'client_secret' => config('services.line-notify.client_secret'),
]);
$token = $response->json()['access_token'];
$user->update([
'line_notify_token' => $token,
]);
return redirect()->to(config('app.frontend_url'));
}
}
- Usage: put the following code in your frontend
const CLIENT_ID = "xxxxxxxxxx";
const BACKEND_URL = `https://your-backend-url.com`;
const LINK_URL = `${BACKEND_URL}/line-notify/link`
const GEN_URL = `${BACKEND_URL}/line-notify/gen`
// 1. Generate link token
const linkToken = await axios.post(GEN_URL, {}, {
headers: {
'Authorization': `Bearer ${token}`,
}
}).then(res => res.data);
// 2. Redirect to LINE Notify authorization page
window.location.href = `https://notify-bot.line.me/oauth/authorize?response_type=code&client_id=${CLIENT_ID}&redirect_uri=${encodeURIComponent(LINK_URL)}&scope=notify&state=${linkToken}&response_mode=form_post`;
// 3. Then the code will automatically redirect to the callback URL
Please see CHANGELOG for more information what has changed recently.
Not implemented yet. Welcome to contribute.
If you discover any security related issues, please email hsuan@hsuan.app instead of using the issue tracker.
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.