PHP code example of ycs77 / laravel-line-bot

1. Go to this page and download the library: Download ycs77/laravel-line-bot 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/ */

    

ycs77 / laravel-line-bot example snippets


class VerifyCsrfToken extends Middleware
{
    protected $except = [
        'webhook',
    ];
}

$app->configure('app');
$app->configure('linebot');

/*
|--------------------------------------------------------------------------
| Register Service Providers
|--------------------------------------------------------------------------
|
| ...
|
*/

$app->register(Ycs77\LaravelLineBot\LineBotLumenServiceProvider::class);



use Ycs77\LaravelLineBot\Facades\LineBot;

LineBot::on()->text('嗨', function () {
    LineBot::text('你好')->reply();
});

LineBot::on()->fallback(function () {
    LineBot::text('我不大了解您的意思...')->reply();
});


LineBot::on()->text('我叫{name}', function ($name) {
    LineBot::text("你好{$name}")->reply();
});

LineBot::on()->fallback(function () {
    LineBot::text('我不大了解您的意思...')->reply();
});

LineBot::on()->image(function () {
    LineBot::text('圖片')->reply();
});

LineBot::on()->follow(function () {
    LineBot::text('加為好友')->reply();
});

LineBot::on()->text('嗨', function () {
    LineBot::text('你好')->reply();
});

use Ycs77\LaravelLineBot\ActionBuilder;
use Ycs77\LaravelLineBot\Message\TemplateBuilder;

LineBot::on()->text('選單', function () {
    LineBot::template('這是我的選單', function (TemplateBuilder $template) {
        $template->button('選單', '請選擇', 'https://example.test/image/path...', function (ActionBuilder $action) {
            $action->message('購買');
            $action->message('加到購物車');
            $action->url('瀏覽網站', 'https://developers.line.biz/zh-hant/');
        });
    })->reply();
});

LineBot::on()->text('購買', function () {
    LineBot::text('購買成功!')->reply();
});

LineBot::on()->text('加到購物車', function () {
    LineBot::text('加到購物車成功!')->reply();
});

use Ycs77\LaravelLineBot\QuickReplyBuilder;

LineBot::on()->text('哈囉', function () {
    LineBot::text('請問有什麼能為您服務?')
        ->quickReply(function (QuickReplyBuilder $action) {
            $action->message('商品清單');
            $action->message('聯絡資訊');
        })
        ->reply();
});

LineBot::on()->text('商品清單', function () {
    LineBot::text('商品清單...')->reply();
});

LineBot::on()->text('聯絡資訊', function () {
    LineBot::text('聯絡資訊...')->reply();
});

$action->message('購買');
$action->message('加到購物車');
$action->url('瀏覽網站', 'https://example.test/');

LineBot::on()->text('profile', function () {
    $profile = LineBot::profile();
    LineBot::text("你好 {$profile->name()}")->reply();
});

'user' => [
    'enabled' => true,
    'model' => App\User::class,
    'id' => 'line_user_id',
],

Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('line_user_id')->unique();
    $table->timestamps();
});



namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Ycs77\LaravelLineBot\CanStoreLineBotUser;
use Ycs77\LaravelLineBot\Contracts\User as UserContract;

class User extends Authenticatable implements UserContract
{
    use Notifiable, CanStoreLineBotUser;

    protected $fillable = [
        'name', 'line_user_id',
    ];
}


LineBot::on()->text('user', function () {
    $user = LineBot::user();
    LineBot::text("你好:{$user->name}")->reply();
});

'rich_menus' => [
    'rich_menu_1' => [
        'size' => [
            'width' => 2500,
            'height' => 1686,
        ],
        'selected' => false,
        'name' => '我的 Rich Menu',
        'chatBarText' => '開啟選單',
        'areas' => [
            [
                'bounds' => [
                    'x' => 0,
                    'y' => 0,
                    'width' => 2500,
                    'height' => 1686,
                ],
                'action' => [
                    'type' => 'message',
                    'label' => 'Rich Menu',
                    'text' => 'Rich Menu',
                ],
            ],
        ],
    ],
],

LineBot::on()->text('LINE SDK', function () {
    $token = LineBot::getEvent()->getReplyToken();
    LineBot::base()->replyText($token, '回傳文字...');
});
bash
php artisan vendor:publish --provider=Ycs77\\LaravelLineBot\\LineBotServiceProvider
bash
php artisan linebot:install

php artisan linebot:richmenu:create rich_menu_1 "public/image.jpg"

php artisan linebot:richmenu:list

php artisan linebot:richmenu:clear richmenuid-sdg24sd56gf...