richdynamix / chatbase-api
Chatbase API 的 PHP SDK 包装器(非官方)
Requires
- php: ^7.1
- guzzlehttp/guzzle: ^6.3
- illuminate/contracts: ~5.5.0|~5.6.0|~5.7.0|~5.8.0
- illuminate/support: ~5.5.0|~5.6.0|~5.7.0|~5.8.0
- nesbot/carbon: ^1.22
Requires (Dev)
- phpmd/phpmd: ^2.6
- phpunit/phpunit: ^7.0
- squizlabs/php_codesniffer: *
This package is auto-updated.
Last update: 2024-08-29 00:48:59 UTC
README
Chatbase 是由 Google 提供的聊天机器人分析服务。
这是 PHP 的非官方 Chatbase API。它主要针对 Laravel 框架,但也可以在支持 Composer 的任何独立项目中使用。
安装
此软件包可以通过 Composer 安装。
composer require richdynamix/chatbase-api
无框架使用
// API KEY and default platform must be set $apiKey = '12345-12345-12345'; $platform = 'messenger';
// Instantiate a new Chatbase client with the dependency of Guzzle $client = new \Richdynamix\Chatbase\Service\ChatbaseClient(new \GuzzleHttp\Client());
// Instantiate a new FieldManager with the dependency of the API KEY and default platform $fieldsManager = new \Richdynamix\Chatbase\Entities\FieldsManager($apiKey, $platform);
// Instantiate a new GenericMessage with the dependency of the Chatbase client and the FieldsManager $chatbase = new \Richdynamix\Chatbase\Service\GenericMessage($client, $fieldsManager);
// send a user message to chatbase $send = $chatbase->userMessage()->with(['user_id => '12345', 'message' => 'hello'])->send();
Laravel 使用
在 Laravel >5.5 中,该软件包将自动注册服务提供者。在 Laravel 5.4 中,您必须安装此服务提供者。
// config/app.php 'providers' => [ ... Richdynamix\Chatbase\ChatbaseServiceProvider::class, ... ];
在 Laravel >5.5 中,该软件包将自动注册外观。在 Laravel 5.4 中,您必须手动安装外观。
// config/app.php 'aliases' => [ ... 'Chatbase' => Richdynamix\Chatbase\Facades\Chatbase::class, ... ];
您可以使用以下命令发布此软件包的配置文件
php artisan vendor:publish --provider="Richdynamix\Chatbase\ChatbaseServiceProvider"
以下配置文件将发布到 config/chatbase.php
return [ /* * The Chatbase API key */ 'api_key' => env('CHATBASE_API_KEY'), 'platform' => env('CHATBASE_DEFAULT_PLATFORM', 'messenger'), ];
目前仅设置了通用消息 API
IoC 容器
IoC 容器将自动为您解析 GenericMessage
依赖项。您可以通过多种方式从 IoC 容器中获取 GenericMessage
实例。
// Directly from the IoC $chatbase = app(Richdynamix\Chatbase\Contracts\GenericMessage::class); // From a constructor class FooClass { public function __construct(Richdynamix\Chatbase\Contracts\GenericMessage $chatbase) { // . . . } } // From a method class BarClass { public function barMethod(Richdynamix\Chatbase\Contracts\GenericMessage $chatbase) { // . . . } }
或者您可以直接使用 Chatbase
外观
Chatbase::userMessage()->with($data)->send();
Chatbase::notHandledUserMessage()->with($data)->send();
Chatbase::botMessage()->with($data)->send();
可以使用 with()
方法设置的字段(键)。(作为数组传递)
使用方法
所有方法都按以下顺序接受相同的参数 -
$userId, $message, $intent, $version, $customSessionID
向 Chatbase 发送用户消息
$send = $chatbase->userMessage()->with(['user_id => '12345', 'message' => 'hello'])->send(); // With Facade $send = Chatbase::userMessage()->with(['user_id => '12345', 'message' => 'hello'])->send(); // With helper setters $send = Chatbase::userMessage()->withUserId('12345')->withMessage('hello')->send(); dd($send);
示例响应 -
{#618 ▼ +"message_id": "2981752682" +"status": 200 }
向不同平台发送用户消息到 Chatbase
$send = $chatbase->userMessage()->with([ 'user_id => '12345', 'message' => 'hello', 'platform' => 'slack' ])->send(); // With Facade $send = Chatbase::userMessage()->with([ 'user_id => '12345', 'message' => 'hello', 'platform' => 'slack' ])->send(); // With helper setters $send = Chatbase::userMessage() ->setPlatform('slack') ->withUserId('12345') ->withMessage('hello') ->send(); dd($send);
示例响应 -
{#618 ▼ +"message_id": "2981752682" +"status": 200 }
在记录版本的同时向 Chatbase 发送用户消息
$send = $chatbase->userMessage()->with([ 'user_id => '12345', 'message' => 'hello', 'version' => '1.2.1' ])->send(); // With Facade $send = Chatbase::userMessage()->with([ 'user_id => '12345', 'message' => 'hello', 'version' => '1.2.1' ])->send(); // With helper setters $send = Chatbase::userMessage() ->withUserId('12345') ->withMessage('hello') ->withVersion('1.2.1') ->send(); dd($send);
示例响应 -
{#618 ▼ +"message_id": "2981752682" +"status": 200 }
带有意图向 Chatbase 发送用户消息
$send = $chatbase->userMessage()->with([ 'user_id => '12345', 'message' => 'hello', 'intent' => 'hotel-booking' ])->send(); // With Facade $send = Chatbase::userMessage()->with([ 'user_id => '12345', 'message' => 'hello', 'intent' => 'hotel-booking' ])->send(); // With helper setters $send = Chatbase::userMessage() ->withUserId('12345') ->withMessage('hello') ->withIntent('hotel-booking') ->send(); dd($send);
示例响应 -
{#618 ▼ +"message_id": "2981752682" +"status": 200 }
发送未由机器人处理的失败用户消息
$send = $chatbase->notHandledUserMessage() ->with([ 'user_id => '12345', 'message' => 'hello', ]) ->send(); // With Facade $send = Chatbase::notHandledUserMessage() ->with([ 'user_id => '12345', 'message' => 'hello', ]) ->send(); // With helper setters $send = Chatbase::userMessage() ->withUserId('12345') ->withMessage('hello') ->withIntent('hotel-booking') ->send(); dd($send);
示例响应 -
{#618 ▼ +"message_id": "29817235682" +"status": 200 }
发送发送回用户的机器人消息
$send = $chatbase->botMessage()->with(['user_id => '12345','message' => 'hello'])->send(); // With facade $send = Chatbase::botMessage()->with(['user_id => '12345','message' => 'hello'])->send(); // With helper setters $send = Chatbase::botMessage()->withUserId('12345')->withMessage('hello')->send(); dd($send);
响应 -
{#618 ▼ +"message_id": "29347235682" +"status": 200 }
与多个机器人协作
有时您可能希望将您的机器人活动推送到不同的 Chatbase 账户。也许您在一个应用程序中运行了多个机器人。您可以在每个方法调用中轻松设置每个机器人的 API 密钥
。
$send = $chatbase->userMessage()->with([ 'api_key' => 'my-chatbase-api-key', 'user_id => '12345', 'message' => 'hello' ])->send(); // With facade $send = Chatbase::userMessage()->with([ 'api_key' => 'my-chatbase-api-key', 'user_id => '12345', 'message' => 'hello' ])->send(); // With helper setters $send = Chatbase::botMessage()->setApiKey('my-chatbase-api-key')->withUserId('12345')->withMessage('hello')->send(); dd($send);
示例响应 -
{#618 ▼ +"message_id": "2981752682" +"status": 200 }
*请注意:发送给 Chatbase 的无效字段可能会导致成功的条目,但您将收到 400 错误,并且会抛出 WrongDataSet
异常。这通常发生在您为机器人消息设置类似 intent
的字段时。
变更日志
请参阅 CHANGELOG 了解最近更改的详细信息。
贡献
请参阅 CONTRIBUTING 了解详细信息。
安全性
如果您发现任何与安全相关的问题,请通过电子邮件 steven@richdynamix.com 而不是使用问题跟踪器。
致谢
许可
MIT 许可证(MIT)。请参阅 许可文件 了解更多信息。