whchi/laravel-linebot-wrapper

A simple laravel wrapper for linecorp/line-bot-sdk

3.0.0 2021-07-07 07:53 UTC

This package is auto-updated.

Last update: 2024-11-07 14:51:25 UTC


README

A simple wrapper of linecorp/line-bot-sdk

Inspired by bottener.js

Installation

  1. composer install
composer require whchi/laravel-linebot-wrapper
  1. publish vendor
php artisan vendor:publish --provider="Whchi\LaravelLineBotWrapper\LINEBotServiceProvider"
  1. setup config config/linebot.php
return [
    'channel_access_token' => 'find it in your LINE console'
    'channel_secret' => 'find it in your LINE console'
];
  1. run migration: create a table named line_bot_sessions
  2. start use, see sample in app sample

remember to remove VerifyCsrfToken for line route

Usage

sdk functions

$this->context->sdk('replyText', [$event['replyToken'], 'hello']);

Initialize

In your webhook entry point

public function __construct()
{
    $context = app('LINEBotContext');
}
...
public function entryPoint(Request $request)
{
    $lineMsg = $request->all();
    $events = collect($lineMsg['events']);
    $events->map(function($event) {
        $this->context->setContext($event);

        ... do whatever you want...
    });
}

Remember bot session

Use event queue is recommended, see app samples

// after setContext
event(new SaveLineBotSessionEvent($event));

State handling

Need Redis running in your enviorment

  • init state
$this->context->initState([
    'status' => 1,
    'isAuth' => ['departA' => true, 'departB' => false]
    ]);
// must execute after setContext
$this->context->buildState();
  • set state
$this->context->setState(['status' => 2]);
  • get state
$this->context->getState('isAuth.departA');
  • reset state
$this->context->resetState();

Messages handling

see message samples for $template

push API

Make sure your account have PUSH_API permission

set userId before use push function

$this->context->setPushTo(string $userId|$groupId|$roomId);
  • button
$this->context->pushButtonTemplate(string $altText,array $template)
  • confirm
$this->context->pushConfirmTemplate(string $altText,array $template)
  • carousel
$this->context->pushCarouselTemplate(string $altText,array $template)
  • image carousel
$this->context->pushImageCarouselTemplate(string $altText, array $template)
  • audio
$this->context->pushAudio(array $template)
  • video
$this->context->pushVideo(array $template)
  • image
$this->context->pushImage(array $template)
  • sticker
$this->context->pushSticker(array $template)
  • location
$this->context->pushLocation(array $template)
  • text
$this->context->pushText(array $template)
  • multiple messages

Maximum size of $templateList is 5, so as reply

$this->context->push(string $altText,array $templateList)
  • multiple users

// max 150 user id
// @see https://developers.line.biz/en/reference/messaging-api/#send-multicast-message
$this->context->pushMulticast(array $memberIdList, string $altText, array $templateList)
  • flex

Maximum size of carousel flex message is 10, more detail: official doc

$this->context->pushFlex(string $altText, array $flexTemplate)
$this->context->pushButtonTemplate(string $altText, $template + $quickReply);

reply API

Change pushXXXX to replyXXXX without setting userId, that is.

Leave API

$this->context->leave();

other usage

$this->context->isXXXXEvent();
// example
$this->context->isMessageEvent();
$this->context->isPostbackEvent();
...
  • detect message event type
$this->context->isXXXXMessage();
// example
$this->context->isTextMessage();
$this->context->isImageMessage();
...
  • get message event message
$this->content->getMessagePayload();
  • get postback data
$this->context->getPostbackPayload();
/**
 * $key = 'datetime' | 'date' | 'time'
 */
$this->context->getDateTimePostbackPayload(string $key);
$this->context->getUserProfile();
  • get streaming data(audio / video / image)
$this->context->getMessageStreamData();
  • accessable attributes
$this->context->rawEvent;
$this->context->eventType;
$this->context->messageEventType;
$this->context->useId;
$this->context->botSessionId;