PHP code example of hsuan1117 / line-notify-channel

1. Go to this page and download the library: Download hsuan1117/line-notify-channel library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hsuan1117 / line-notify-channel example snippets


public function via($notifiable)
{
    return [LineNotifyChannel::class];
}

public function routeNotificationForLINENotify($notifiable)
{
    return $notifiable->line_notify_token;
}

public function toLINENotify(object $notifiable): LINENotifyMessage
{
    return new LINENotifyMessage('Hello World!');
}

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');
});


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'));
    }
}