---
# System prepended metadata

title: Laravel-Linebot
tags: [Laravel]

---

# Laravel-Linebot
###### tags: `Laravel`


# 1. 開啟你的VSCODE專案
[DahisC](@7S-28sLnTNeRmfI4wFzcPg)提供的乾淨檔案👇

https://github.com/DahisC/laravel7-linebot

```
phpartisan serve
```
結果如下
php artisan serve
Laravel development server started: http://127.0.0.1:8000
# 2. 下載使用ngrok

[ngrok下載位置](https://ngrok.com/)
* ## 當免費仔下載他，解壓縮到桌面後開啟

![](https://i.imgur.com/hNwr3JY.png)
* ## 在最後輸入連結網址
![](https://i.imgur.com/vDc3T5u.png)

你可以在此頁面最後寫上 
```
ngrok http 8000
```
或者
```
ngrok http http://127.0.0.1:8000/
```
後ENTER執行
* ## 取得https://網址

複製https://...ngrok.io 網址
等等加到後面的賴機器人

![](https://i.imgur.com/ycL9Y8y.png)

[如果想了解ngrok可以看我](https://5xruby.tw/posts/easy-ngrok-by-nginx-ssh-tunnel)

# 3. 建立Line機器人
* ## 登入[line busniess](https://tw.linebiz.com/login/)
![](https://i.imgur.com/Hnrfmua.png)

* ## 建立機器人
    ![](https://i.imgur.com/7ZiI2lg.png)
    * ### 填寫基本資料>確認
        ![](https://i.imgur.com/lHcRcMt.png)
        ![](https://i.imgur.com/yLw0dXI.png)
    * ### 
        ![](https://i.imgur.com/NSy6F6n.png)
    * ###
        ![](https://i.imgur.com/nNUIXjz.png)
* ## 建立完成
    ![](https://i.imgur.com/U8I8xEi.png)
# 4. 設定line機器人
* ## 回應模式設定
![](https://i.imgur.com/nhqRhBq.png) 
* ## 若無WEBHOOK則，點選啟用Messaging API
    * 點選 啟用Messaging API
        ![](https://i.imgur.com/jrcwH0y.png)
    * 建立服務提供者
        ![](https://i.imgur.com/XmPXQV9.png)
    * 點選確定略過隱私權政策及服務條款
        ![](https://i.imgur.com/sOi5ykf.png)
    * 點選確認啟用Messaging API
        ![](https://i.imgur.com/yuywlO0.png)
* ## 填入Webhook網址
    就是剛剛複製的網址https://...ngrok.io
    後綴 .ngrok.io/api/line
    
    ![](https://i.imgur.com/t4aot7j.png)
* ## 點選 由LINE Developers進行其他設定
    ![](https://i.imgur.com/Boza516.png)
* ## 點選右上角頭像
    ![](https://i.imgur.com/uQG2ErA.png)
* ## 點選 LINE Developers Console
    ![](https://i.imgur.com/tuujW0K.png)
* ## 點選稍早創建的服務提供者名稱
    ![](https://i.imgur.com/VlA98Lm.png)
* ## 點選機器人
    ![](https://i.imgur.com/bhS6R8f.png)  
* ## 點選Message API
    ![](https://i.imgur.com/2zmUW4A.png)
* ## 驗證webhook (Verify)
    ![](https://i.imgur.com/hbEd3Dw.png)
* ## 點選啟用 Use Webhook 
    ![](https://i.imgur.com/4vQJi7y.png)
* ## 取得Channel access token
    複製他
    ![](https://i.imgur.com/5WojFNj.png)

# 5. VScode建置

* ## api.php 設定路由

```php=
<?php

use App\Http\Controllers\LineController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

Route::post('/line','LineController@index');
```

* ## create LineController

```
php artisan make:controller LineController
```

* ## LineController程式碼

```php=
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class LineController extends Controller
{
    //
    public function index(Request $request)
        error_log(json_encode('1'));
        return response('ok','200');
}    
``` 
* ## 修改.env

```
LINE_CHANNEL_ASCCES_TOKEN=放剛剛複製的TOKEN
```

重新再跑一次php artisan serve~

* ## 驗證webhook (Verify)
    會出現"成功"
    
    ![](https://i.imgur.com/hbEd3Dw.png)

# 7. 發送訊息至LINE機器人
![](https://i.imgur.com/g5L0URg.png)


# 參考資料:
![](https://i.imgur.com/ls1WWB0.png)

https://www.oxxostudio.tw/articles/201804/line-bot-apps-script.html
