3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[PHP]LINE bot開発での簡易デバッグ

Last updated at Posted at 2020-09-17

#今回の題
今回は完全に個人的なメモです。
LINEbotのsdkなどに関する説明等はいたしません。

#どうやるのか
受け取ったリクエストの内容をerror_logでログファイルに書き出し、ブラウザ上で確認できるようにする。

error_log(print_r($event, true) . "\n", 3, '書き込むログファイルへのパス');

以下は全体像。

<?php
require('vendor/autoload.php');

use LINE\LINEBot\Constant\HTTPHeader;
use LINE\LINEBot\HTTPClient\CurlHTTPClient;
use LINE\LINEBot;

$http_client = new CurlHTTPClient(チャネルアクセストークン);
$bot = new LINEBot($http_client, ['channelSecret' => チャネルシークレット]);
$signature = $_SERVER['HTTP_' . HTTPHeader::LINE_SIGNATURE];
$request_body = file_get_contents('php://input');
$events = $bot->parseEventRequest($request_body, $signature);
$event = $events[0];

error_log(print_r($event, true) . "\n", 3, '書き込むログファイルへのパス');

このログファイルをブラウザで表示させると、以下のように表示される。

LINE\LINEBot\Event\MessageEvent\TextMessage Object
(
    [emojis:LINE\LINEBot\Event\MessageEvent\TextMessage:private] => 
    [message:protected] => Array
        (
            [type] => text
            [id] => XXXXXXXXX
            [text] => テスト
        )

    [event:protected] => Array
        (
            [type] => message
            [replyToken] => XXXXXXXXX
            [source] => Array
                (
                    [userId] => XXXXXXXXX
                    [type] => user
                )

            [timestamp] => 1600355946638
            [mode] => active
            [message] => Array
                (
                    [type] => text
                    [id] => XXXXXXXXX
                    [text] => テスト
                )

        )

)

以上!

#一言
息抜きにLINEbotを作成中です。
もっといい簡易的なデバッグ方法があればコメントにお願いいたします🙇‍♀️

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?