LINE公式アカウント作成&Webhook設定
LINE Developersを表示。
「コンソールにログイン」をクリック。
![LINE developers](https://halfpower.work/wp-content/uploads/2023/08/1-1-1024x431.png)
「LINEアカウントでログイン」をクリック。
![LINEログイン](https://halfpower.work/wp-content/uploads/2023/08/2-2.png)
プロバイダーの「作成」をクリック。
![LINE プロバイダー作成ボタン](https://halfpower.work/wp-content/uploads/2023/08/3-2.png)
プロバイダー名を入力して「作成」をクリック。
![LINE 新規プロバイダー作成](https://halfpower.work/wp-content/uploads/2023/08/4-1.png)
「Messaging API」をクリック。
![LINE Messaging API チャネル設定](https://halfpower.work/wp-content/uploads/2023/08/5-1024x573.png)
必須項目を入力し「作成」をクリック。
![LINE 新規チャネル作成1](https://halfpower.work/wp-content/uploads/2023/08/6-1024x529.png)
![LINE 新規チャネル作成2](https://halfpower.work/wp-content/uploads/2023/08/7-1024x600.png)
![LINE 新規チャネル作成3](https://halfpower.work/wp-content/uploads/2023/08/8-1024x409.png)
「OK」をクリック。
![LINE 新規チャネル作成確認](https://halfpower.work/wp-content/uploads/2023/08/9.png)
「同意する」をクリック。
![LINE 情報利用に関する同意について](https://halfpower.work/wp-content/uploads/2023/08/10.png)
「同意する」をクリック。
![LINE ZHDグループへの情報提供に関する個別規約への同意について](https://halfpower.work/wp-content/uploads/2023/08/11.png)
「LINE Official Account Manager」をクリック。
![LINE Messaging API チャネル基本設定](https://halfpower.work/wp-content/uploads/2023/08/12.png)
別タブで開いた「LINE Official Account Manager」の左メニューの「応答設定」をクリック。
![LINE Official Account Managerメニュー](https://halfpower.work/wp-content/uploads/2023/08/13.png)
「Webhook」をON、「応答メッセージ」をOFFにする。
![LINE Official Account Manager 応答設定](https://halfpower.work/wp-content/uploads/2023/08/14-1.png)
LINE Developersタブに戻る。
「Messaging API設定」をクリック。
![](https://halfpower.work/wp-content/uploads/2023/08/15.png)
「Webhook URL」にPOST先URLを入力して「更新」をクリック。
![LINE Messaging API Webhook設定](https://halfpower.work/wp-content/uploads/2023/08/16.png)
メッセージ受信時はもちろん、友だち登録、解除時にも通知される。
Laravel側
use Illuminate\Http\Response;
class xxxxController extends Controller
{
public function yyyy(Request $request)
{
$values = $request->all();
// 何らかの処理
return response(null, Response::HTTP_OK);
}
}
Laravel側注意点
1.GETではなくPOSTで通知されるので、POSTのルートを設定しましょう。
2.Webhook通知はCSRFを無効にする必要があるので
\app\Http\Middleware\VerifyCsrfToken.phpの$exceptに通知されるURLを記述しましょう。
class VerifyCsrfToken extends Middleware
{
/**
* The URIs that should be excluded from CSRF verification.
*
* @var array
*/
protected $except = [
'/xxxx/yyyy'
];
}
例)通知される内容
友だち登録
array (
'destination' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'events' =>
array (
0 =>
array (
'type' => 'follow',
'webhookEventId' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'deliveryContext' =>
array (
'isRedelivery' => false,
),
'timestamp' => 1692499283591,
'source' =>
array (
'type' => 'user',
'userId' => 'xxxxxxxxxxxxxxxxxxxxxxxx',
),
'replyToken' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'mode' => 'active',
),
),
)
友達削除
array (
'destination' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'events' =>
array (
0 =>
array (
'type' => 'unfollow',
'webhookEventId' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'deliveryContext' =>
array (
'isRedelivery' => false,
),
'timestamp' => 1692499173688,
'source' =>
array (
'type' => 'user',
'userId' => 'xxxxxxxxxxxxxxxxxxxxxxxx',
),
'mode' => 'active',
),
),
)
メッセージ受信通知
array (
'destination' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'events' =>
array (
0 =>
array (
'type' => 'message',
'message' =>
array (
'type' => 'text',
'id' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'text' => '〇〇〇〇〇〇〇〇',
),
'webhookEventId' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'deliveryContext' =>
array (
'isRedelivery' => false,
),
'timestamp' => 1692495509410,
'source' =>
array (
'type' => 'user',
'userId' => 'xxxxxxxxxxxxxxxxxxxxxxxx',
),
'replyToken' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'mode' => 'active',
),
),
)
1件のコメント