whchi / laravel-linebot-wrapper
A simple laravel wrapper for linecorp/line-bot-sdk
3.0.0
2021-07-07 07:53 UTC
Requires
- php: >=7
- linecorp/line-bot-sdk: ^7
Requires (Dev)
- mockery/mockery: 1.4.x-dev
- nunomaduro/collision: ^5.3
- orchestra/testbench: 7.0.x-dev
- squizlabs/php_codesniffer: *
README
A simple wrapper of linecorp/line-bot-sdk
Inspired by bottener.js
Installation
- composer install
composer require whchi/laravel-linebot-wrapper
- publish vendor
php artisan vendor:publish --provider="Whchi\LaravelLineBotWrapper\LINEBotServiceProvider"
- setup config
config/linebot.php
return [ 'channel_access_token' => 'find it in your LINE console' 'channel_secret' => 'find it in your LINE console' ];
- run migration: create a table named
line_bot_sessions
- 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)
Maximum size of $templateList
is 5, so as reply
$this->context->push(string $altText,array $templateList)
// 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)
Maximum size of carousel flex message is 10, more detail: official doc
$this->context->pushFlex(string $altText, array $flexTemplate)
- quickReply see $quickReply example at message samples
$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
- detect event
more detail see official doc
$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);
- get user profile
more detail see official doc
$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;