https://github.com/mesak/laravel-linebot
https://github.com/mesak/laravel-linebot
Last synced: 7 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/mesak/laravel-linebot
- Owner: mesak
- Created: 2023-01-29T09:27:02.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-02-15T18:45:47.000Z (over 3 years ago)
- Last Synced: 2025-03-26T16:24:45.761Z (over 1 year ago)
- Language: PHP
- Size: 19.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Laravel LINEBOT
採用 laravel response 的方式來回應 LINEBOT 的訊息
## 安裝
```bash
composer require mesak/laravel-linebot
```
## 設定檔替換
將設定檔複製到專案中
```bash
php artisan vendor:publish --tag=mesak-linebot.config --force
```
修改 `config/linebot.php` 中的設定
```php
'listener' => 'App\Listeners\LineBotListener',
```
## 使用
利用標準檔案 `App\Listeners\LineBotListener` 來建立 LINEBOT 的 Listener
```bash
php artisan vendor:publish --tag=mesak-linebot.listener
```
`LineBotListener.php`
LINEBOT 預設的事件:
- onMessage
- onUnsend
- onFollow
- onUnfollow
- onJoin
- onLeave
- onPostback
- onVideoPlayComplete
- onBeacon
- onAccountLink
- onMemberJoined
- onMemberLeft
- onThings
## 設定
在 `.env` 中加入
```env
LINE_CLIENT_ID=xxxxxxxx
LINE_CLIENT_SECRET=xxxxxxxx
```
在 route 中加入,對應 line webhook 的 post 事件
```php
use Illuminate\Http\Request;
Route::post('/line', function (Request $request) {
return \Facades\Mesak\LineBot\Contracts\Bot::handle($request);
});
```
### Event 事件
套件利用處理 Request 把內容塞入 `Mesak\LineBot\Events\MessageEvent` 中,利用 BotEventSubscribe 發起對話事件,最後回傳 `Mesak\LineBot\Actions\BaseAction` 的事件來處理需要的回應
### 擴充
如果需要擴充 LINEBOT 的功能,可以在 `App\Providers\AppServiceProvider` 中的 register 加入 `\Mesak\LineBot\Contracts\Bot::class` 合約綁定
```php
$this->app->singleton(\Mesak\LineBot\Contracts\Bot::class, function ($app) {
return tap(new \App\Services\EntityBot(config('linebot') , $app['events']) ,function($bot){
$bot->boot();
});
});
```
自訂 `EntityBot` 類別
```php