whchi/laravel-linebot-wrapper 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

whchi/laravel-linebot-wrapper

Composer 安装命令:

composer require whchi/laravel-linebot-wrapper

包简介

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

README 文档

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;

whchi/laravel-linebot-wrapper 适用场景与选型建议

whchi/laravel-linebot-wrapper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 157 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 04 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「wrapper」 「line」 「laravel」 「chatbot」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 whchi/laravel-linebot-wrapper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 whchi/laravel-linebot-wrapper 我们能提供哪些服务?
定制开发 / 二次开发

基于 whchi/laravel-linebot-wrapper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 157
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 6
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-04-21