PHPackages                             yamakadi/laravel-line-webhooks - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. yamakadi/laravel-line-webhooks

ActiveLibrary[API Development](/categories/api)

yamakadi/laravel-line-webhooks
==============================

Handle Line webhooks in a Laravel application

2.0.1(8y ago)5234MITPHPPHP ^7.0

Since Oct 23Pushed 8y ago1 watchersCompare

[ Source](https://github.com/yamakadi/laravel-line-webhooks)[ Packagist](https://packagist.org/packages/yamakadi/laravel-line-webhooks)[ Docs](https://github.com/yamakadi/laravel-line-webhooks)[ RSS](/packages/yamakadi-laravel-line-webhooks/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (4)Versions (4)Used By (0)

Handle Line Webhooks in a Laravel application
=============================================

[](#handle-line-webhooks-in-a-laravel-application)

[![Latest Version on Packagist](https://camo.githubusercontent.com/5aac966e64f7c1827eef8490134d82d676fb6a6b94817effdb7b2594c3939773/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f79616d616b6164692f6c61726176656c2d6c696e652d776562686f6f6b732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/yamakadi/laravel-line-webhooks)[![Build Status](https://camo.githubusercontent.com/af4271b7115da7768916d9054e63adcb8c693615a780cf38d1a1d348d4e940dc/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f79616d616b6164692f6c61726176656c2d6c696e652d776562686f6f6b732f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/yamakadi/laravel-line-webhooks)[![StyleCI](https://camo.githubusercontent.com/7b5e421e1240ba97723c38f5345d2ba85da49534286349b63f0a8a8ada4e0d64/68747470733a2f2f7374796c6563692e696f2f7265706f732f3130353932303137392f736869656c643f6272616e63683d6d6173746572)](https://styleci.io/repos/105920179)[![SensioLabsInsight](https://camo.githubusercontent.com/b2afe762f05deac7a32baaa7005976268855e5cf50c3d2f0f6f02fa067b783ef/68747470733a2f2f696d672e736869656c64732e696f2f73656e73696f6c6162732f692f61303237623130332d373732632d346462632d613261342d6136636363303765313237662e7376673f7374796c653d666c61742d737175617265)](https://insight.sensiolabs.com/projects/a027b103-772c-4dbc-a2a4-a6ccc07e127f)[![Quality Score](https://camo.githubusercontent.com/328a0dc08ccf37145cf0a0b019ba652d5ab7f5a9f34dc9dd957a7878f77256e6/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f79616d616b6164692f6c61726176656c2d6c696e652d776562686f6f6b732e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/yamakadi/laravel-line-webhooks)[![Total Downloads](https://camo.githubusercontent.com/ad2fac1d6af13857e82a1fc09d33687879a69944671c350d8889a16de09c60e4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f79616d616b6164692f6c61726176656c2d6c696e652d776562686f6f6b732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/yamakadi/laravel-line-webhooks)

[Line](https://developers.line.me/en/docs/) can notify your application of events using webhooks. This package can help you handle those webhooks. Out of the box it will verify the Line signature of all incoming requests. All valid calls will be logged to the database. You can easily define jobs or events that should be dispatched when specific events hit your app.

This package will not handle what should be done after the webhook request has been validated and the right job or event is called. You should still code up any work (eg. regarding messages) yourself.

Before using this package we highly recommend reading [the entire documentation on the messaging api over at Line](https://developers.line.me/en/docs/messaging-api/overview/).

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require yamakadi/laravel-line-webhooks
```

The service provider will automatically register itself.

You must publish the config file with:

```
php artisan vendor:publish --provider="Yamakadi\LineWebhooks\LineWebhooksServiceProvider" --tag="config"
```

This is the contents of the config file that will be published at `config/line-webhooks.php`:

```
return [
    /*
     * You need to define your channel secret and access token in your environment variables
     */
    'channel_id' => env('LINEBOT_CHANNEL_ID'),
    'channel_secret' => env('LINEBOT_CHANNEL_SECRET'),
    'channel_access_token' => env('LINEBOT_CHANNEL_ACCESS_TOKEN'),

    /*
     * You can define the job that should be run when a certain webhook hits your application
     * here. The key is the name of the Line event in snake_case.
     *
     * You can find a list of Line webhook types here:
     * https://developers.line.me/en/docs/messaging-api/reference/#webhook-event-objects
     */
    'jobs' => [
        // 'message' => \App\Jobs\LineWebhooks\HandleIncomingMessage::class,
        // 'beacon' => \App\Jobs\LineWebhooks\HandleBeaconSignal::class,
    ],

    /*
     * The classname of the model to be used. The class should equal or extend
     * Yamakadi\LineWebhooks\LineWebhookCall.
     */
    'model' => Yamakadi\LineWebhooks\LineWebhookCall::class,
];
```

Next, you must publish the migration with:

```
php artisan vendor:publish --provider="Yamakadi\LineWebhooks\LineWebhooksServiceProvider" --tag="migrations"
```

After the migration has been published you can create the `line_webhook_calls` table by running the migrations:

```
php artisan migrate
```

Finally, take care of the routing: At [the Line dashboard](https://developers.line.me/console/) you must configure at what url Line webhooks should hit your app. In the routes file of your app you must pass that route to `Route::lineWebhooks`:

```
Route::lineWebhooks('webhook-route-configured-at-the-line-dashboard');
```

Behind the scenes this will register a `POST` route to a controller provided by this package. Because Line has no way of getting a csrf-token, you must add that route to the `except` array of the `VerifyCsrfToken` middleware:

```
protected $except = [
    'webhook-route-configured-at-the-line-dashboard',
];
```

Usage
-----

[](#usage)

Line will send out webhooks for several event types. You can find the [full list of events types](https://developers.line.me/en/docs/messaging-api/reference/#webhook-event-objects) in the Line documentation.

Line will sign all requests hitting the webhook url of your app. This package will automatically verify if the signature is valid. If it is not, the request was probably not sent by Line.

Unless something goes terribly wrong, this package will always respond with a `200` to webhook requests. Sending a `200` will prevent Line from resending the same event over and over again. All webhook requests with a valid signature will be logged in the `line_webhook_calls` table. The table has a `payload` column where the entire payload of the incoming webhook is saved.

If the signature is not valid, the request will not be logged in the `line_webhook_calls` table but a `Yamakadi\LineWebhooks\WebhookFailed` exception will be thrown. If something goes wrong during the webhook request the thrown exception will be saved in the `exception` column. In that case the controller will send a `500` instead of `200`.

There are two ways this package enables you to handle webhook requests: you can opt to queue a job or listen to the events the package will fire.

### Handling webhook requests using jobs

[](#handling-webhook-requests-using-jobs)

If you want to do something when a specific event type comes in you can define a job that does the work. Here's an example of such a job:

```
