elfsundae / laravel-bearychat
BearyChat 的 Laravel 集成,用于发送机器人消息。
Requires
- php: >=5.6.4
- elfsundae/bearychat: ^1.3.1
- illuminate/support: ~5.0|~6.0|~7.0|~8.0
Requires (Dev)
- mockery/mockery: ~0.9|~1.0
- phpunit/phpunit: ~5.7|~6.0|~7.0|~8.0|~9.0
Suggests
- laravel-notification-channels/bearychat: BearyChat notifications channel for Laravel 5.
README
为 BearyChat 提供 Laravel 集成,用于发送机器人消息。
此包兼容 Laravel 4/5/6/7/8 和 Lumen。
- 🇨🇳 中文文档
- Laravel 通知频道: BearyChatChannel
内容
安装
您可以使用 Composer 管理器安装此包
$ composer require elfsundae/laravel-bearychat
更新 composer 后,您可以根据以下步骤配置您的应用程序
Laravel 5/6/7/8
对于 Laravel 5.5+,服务提供程序将自动注册。
将服务提供程序添加到 config/app.php
中的 providers
数组
ElfSundae\BearyChat\Laravel\ServiceProvider::class,
注册外观
'BearyChat' => ElfSundae\BearyChat\Laravel\BearyChat::class,
然后发布配置文件
$ php artisan vendor:publish --tag=bearychat
接下来,通过编辑 config/bearychat.php
中的配置文件来配置您的 BearyChat 客户端。
Laravel 4
请安装版本 1.1.x
$ composer require elfsundae/laravel-bearychat:1.1.*
将服务提供程序添加到 config/app.php
中的 providers
数组
'ElfSundae\BearyChat\Laravel\ServiceProvider',
然后发布配置文件
$ php artisan config:publish elfsundae/laravel-bearychat
接下来,通过编辑 app/config/packages/elfsundae/laravel-bearychat/config.php
中的配置文件来配置您的 BearyChat 客户端。
Lumen
在 bootstrap/app.php
中注册服务提供程序
$app->register(ElfSundae\BearyChat\Laravel\ServiceProvider::class);
然后将此包中的配置文件复制到您的应用程序的 config/bearychat.php
$ cp vendor/elfsundae/laravel-bearychat/config/bearychat.php config/bearychat.php
现在您可以通过编辑 config/bearychat.php
来配置您的 BearyChat 客户端。
使用
基本使用
您可以使用 BearyChat
外观或 bearychat()
辅助函数获取 BearyChat Client
。
BearyChat::send('message'); bearychat()->sendTo('@elf', 'Hi!');
您可以通过 BearyChat
外观的 client
方法或通过将客户端名称传递给 bearychat()
函数来访问各种客户端。名称应与您 BearyChat 配置文件中列出的客户端之一相匹配。
BearyChat::client('dev')->send('foo'); bearychat('admin')->send('bar');
有关更高级的使用方法,请阅读 BearyChat PHP 包的文档。
异步消息
发送 BearyChat 消息实际上是通过同步 HTTP 请求 Incoming Webhook,因此会减慢您的应用程序执行速度。为了发送异步消息,您可以使用 Laravel 的出色队列系统来排队。
这里是Laravel 5.3的Queueable Job的一个示例
<?php namespace App\Jobs; use Illuminate\Bus\Queueable; use Illuminate\Queue\SerializesModels; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Contracts\Queue\ShouldQueue; use ElfSundae\BearyChat\Message; class SendBearyChat implements ShouldQueue { use InteractsWithQueue, Queueable, SerializesModels; /** * The BearyChat client. * * @var \ElfSundae\BearyChat\Client */ protected $client; /** * The Message instance to be sent. * * @var \ElfSundae\BearyChat\Message */ protected $message; /** * Create a new job instance. * * @param mixed $message A Message instance, or parameters which can be handled * by the `send` method of a Message instance. */ public function __construct($message = null) { if ($message instanceof Message) { $this->message = $message; } elseif (is_string($message)) { $this->text($message); if (func_num_args() > 1) { if (is_bool($markdown = func_get_arg(1))) { $this->markdown(func_get_arg(1)); if (func_num_args() > 2) { $this->notification(func_get_arg(2)); } } else { call_user_func_array([$this, 'add'], array_slice(func_get_args(), 1)); } } } } /** * Any unhandled methods will be sent to the Message instance. * * @param string $method * @param array $parameters * @return $this */ public function __call($method, $parameters) { $message = $this->message ?: new Message($this->client ?: bearychat()); $this->message = call_user_func_array([$message, $method], $parameters); return $this; } /** * Set the client with client name. * * @param string $name * @return $this */ public function client($name) { $this->client = bearychat($name); return $this; } /** * Execute the job. */ public function handle() { if ($this->client) { $this->client->sendMessage($this->message); } else { $this->message->send(); } } }
然后,您可以通过在任何包含DispatchesJobs
特质的对象上调用dispatch
方法或直接使用dispatch()
全局函数来调度SendBearyChat
作业
dispatch(new SendBearyChat('hello')); dispatch(new SendBearyChat('hello', true, 'notification')); dispatch(new SendBearyChat('hello', 'attachment content', 'attachment title', 'http://path/to/image', '#f00')); dispatch((new SendBearyChat)->client('server')->text('hello')->add('attachment')); dispatch(new SendBearyChat( bearychat('admin')->text('New order!')->add($order, $order->name, $order->image_url) ));
发送Laravel异常
BearyChat的一个常见用法是实时报告Laravel异常。只需覆盖异常处理程序的report
方法即可
/** * Report or log an exception. * * This is a great spot to send exceptions to Sentry, Bugsnag, etc. * * @param \Exception $e * @return void */ public function report(Exception $e) { parent::report($e); if (app()->environment('production') && $this->shouldReport($e)) { dispatch( (new SendBearyChat) ->client('server') ->text('New Exception!') ->notification('New Exception: '.get_class($e)) ->markdown(false) ->add(str_limit($e, 1300), get_class($e), null, '#a0a0a0') ); } }
创建输出响应
需要响应输出机器人?只需使用Message
实例创建JSON响应即可。
Route::post('webhook/bearychat', 'WebhookController@bearychat');
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use ElfSundae\BearyChat\Message; class WebhookController extends Controller { /** * The BearyChat Outgoing Robot. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\JsonResponse */ public function bearychat(Request $request) { $message = (new Message) ->text('Response for ' . $request->input('text')) ->add('attachment content'); return response()->json($message); } }
您可以将输出处理程序从Laravel的CSRF保护中排除。
自定义Guzzle
您可以通过在BearyChat
外观或app('bearychat')
上调用customHttpClient
方法来自定义BearyChat的HTTP客户端,即Guzzle。
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use GuzzleHttp\Client as HttpClient; use ElfSundae\BearyChat\Laravel\BearyChat; class AppServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { BearyChat::customHttpClient(function ($name) { if ($name == 'dev') { return new HttpClient([ 'connect_timeout' => 10, 'timeout' => 30, 'verify' => false ]); } }); } }
变更日志
有关最近更改的更多信息,请参阅CHANGELOG。
测试
$ composer test
许可证
BearyChat Laravel包在MIT许可证下提供。