> ## Documentation Index
> Fetch the complete documentation index at: https://kawax.biz/llms.txt
> Use this file to discover all available pages before exploring further.

# Webhook / Bot - LINE SDK for Laravel

> Berichten versturen en ontvangen met de LINE Messaging API via de webhookcontroller en de Bot-facade.

## Webhook

Het pakket biedt een webhookroute en -controller.

```mermaid theme={null}
flowchart TD
    A["Gebruiker stuurt een bericht"] --> B["LINE verstuurt via webhook"]
    B --> C["Ontvangen op de line.webhook-route"]
    C --> D["Validatie via de ValidateSignature-middleware"]
    D --> E["WebhookController"]
    E --> F["WebhookEventDispatcher dispatcht<br>Laravel-events"]
    F --> G["Antwoorden via een eventlistener"]
```

### Webhook-URL

De standaard webhook-URL is als volgt.

```
https://example.com/line/webhook
```

Je kunt het pad wijzigen in `.env`.

```dotenv theme={null}
LINE_BOT_WEBHOOK_PATH=webhook
```

### Integratie met het Laravel-eventsysteem

Wanneer een webhookevent wordt ontvangen, wordt er een Laravel-event gedispatcht. Event discovery is standaard ingeschakeld.

<Info>
  Voer in productie `php artisan event:cache` uit.
</Info>

### Standaardlistener publiceren

```shell theme={null}
php artisan vendor:publish --tag=line-listeners
```

Er wordt een `MessageListener` gegenereerd in `app/Listeners/Line/`.

```php theme={null}
namespace App\Listeners\Line;

use LINE\Clients\MessagingApi\ApiException;
use LINE\Webhook\Model\MessageEvent;
use LINE\Webhook\Model\StickerMessageContent;
use LINE\Webhook\Model\TextMessageContent;
use Revolution\Line\Facades\Bot;

class MessageListener
{
    protected string $token;

    public function handle(MessageEvent $event): void
    {
        $message = $event->getMessage();
        $this->token = $event->getReplyToken();

        match ($message::class) {
            TextMessageContent::class => $this->text($message),
            StickerMessageContent::class => $this->sticker($message),
        };
    }

    protected function text(TextMessageContent $message): void
    {
        Bot::reply($this->token)->text($message->getText());
    }

    protected function sticker(StickerMessageContent $message): void
    {
        Bot::reply($this->token)->sticker(
            $message->getPackageId(),
            $message->getStickerId()
        );
    }
}
```

## Bot-facade

`Revolution\Line\Facades\Bot` delegeert naar alle methods van de `MessagingApiApi`-klasse van de officiële SDK.

```php theme={null}
use Revolution\Line\Facades\Bot;

Bot::replyMessage();
Bot::pushMessage();
```

### De reply-method

Met `Bot::reply()` kun je een replytoken meegeven en een bericht beantwoorden.

<Info>
  Pushberichten via `Bot::pushMessage()` hebben per tariefplan een limiet op het aantal berichten. Antwoorden op berichten van gebruikers (`Bot::reply()`) kennen daarentegen geen limiet en zijn gratis te gebruiken.
</Info>

```php theme={null}
use Revolution\Line\Facades\Bot;

// Tekst beantwoorden
Bot::reply($token)->text('text');

// Meerdere teksten beantwoorden met een andere afzendernaam
Bot::reply($token)->withSender('alt-name')->text('text1', 'text2');

// Een sticker beantwoorden
Bot::reply($token)->sticker(package: 1, sticker: 1);
```

## Aanpassen

### Bot-macro's

`Bot` implementeert `Macroable`, dus je kunt willekeurige methods toevoegen.

Registreer ze in `AppServiceProvider@boot`.

```php theme={null}
use Revolution\Line\Facades\Bot;

public function boot(): void
{
    Bot::macro('foo', function () {
        return $this->bot()->...;
    });
}
```

```php theme={null}
$foo = Bot::foo();
```

### De MessagingApiApi-instantie vervangen

`Bot::bot()` geeft de `MessagingApiApi`-instantie terug. Met `Bot::botUsing()` kun je de instantie vervangen.

```php theme={null}
$bot = new MyBot();

Bot::botUsing($bot);
```

Een callable wordt ook geaccepteerd.

```php theme={null}
Bot::botUsing(function () {
    return new MyBot();
});
```

### De WebhookHandler vervangen

Als je je eigen webhookverwerking wilt schrijven zonder het Laravel-eventsysteem, implementeer dan de `WebhookHandler`-interface en vervang de handler.

Maak `app/Actions/LineWebhook.php` aan.

```php theme={null}
<?php

namespace App\Actions;

use Illuminate\Http\Request;
use LINE\Webhook\Model\MessageEvent;
use Revolution\Line\Contracts\WebhookHandler;
use Revolution\Line\Facades\Bot;

class LineWebhook implements WebhookHandler
{
    public function __invoke(Request $request): mixed
    {
        Bot::parseEvent($request)->each(function ($event) {
            if ($event instanceof MessageEvent) {
                //
            }
        });

        return response('OK');
    }
}
```

Registreer de klasse in `AppServiceProvider@register`.

```php theme={null}
use App\Actions\LineWebhook;
use Revolution\Line\Contracts\WebhookHandler;

public function register(): void
{
    $this->app->scoped(WebhookHandler::class, LineWebhook::class);
}
```

### Middleware van de standaardroute

Standaard is de `throttle`-middleware ingeschakeld. Je kunt dit wijzigen in `.env`.

```dotenv theme={null}
# Uitschakelen
LINE_BOT_WEBHOOK_MIDDLEWARE=null

# De throttle-instellingen wijzigen
LINE_BOT_WEBHOOK_MIDDLEWARE=throttle:120,1
```

### Http::line()

Het pakket breidt de `Http`-klasse uit, dus je kunt ook rechtstreeks API-requests versturen zonder de `Bot`-facade.

```php theme={null}
use Illuminate\Support\Facades\Http;

$response = Http::line()->post('/v2/bot/channel/webhook/test', [
    'endpoint' => '',
]);
```

<Info>
  Zie de [GitHub-repository](https://github.com/invokable/laravel-line-sdk) voor de meest recente informatie.
</Info>


## Related topics

- [LINE SDK for Laravel](/nl/packages/laravel-line-sdk/index.md)
- [Socialite (LINE Login) - LINE SDK for Laravel](/nl/packages/laravel-line-sdk/socialite.md)
- [Notificatiekanaal - LINE SDK for Laravel](/nl/packages/laravel-line-sdk/notification.md)
- [Laravel Notification for Discord(Webhook)](/nl/packages/laravel-notification-discord-webhook.md)
- [我的包](/zh-CN/packages/index.md)
