iansltx/dialogflow-bridge
Composer 安装命令:
composer require iansltx/dialogflow-bridge
包简介
A set of middlewares + helpers for responding to Dialogflow web hooks
README 文档
README
A middleware + helper objects for responding to Dialogflow webhooks.
Requirements + Installation
To take full advantage of this library, you'll need:
- A PSR-11 (formerly Container Interop) compatible container to pull dependencies from.
- A framework that either uses double-pass middlewares (e.g. Slim 3) or the proposed PSR-15 middleware (e.g. Zend Expressive 2.0+), using PSR-7 requests and responses, to use the middleware component.
To install, use Composer:
composer require iansltx/dialogflow-bridge
Getting Started
The easiest way to integrate this library is by adding it as a route middleware to a supported (micro)framework. For example, with Slim 3:
<?php use iansltx\DialogflowBridge\{Middleware, Router, Question, Answer}; // require Composer autoloader here $app = new Slim\App(); $app->post('/hook', new Middleware\DoublePass(Router::buildFromClosureArray([ 'hello' => function(Question $question, Answer $answer) : Answer { return $answer->withSpeechAndText("Hello {$question->getParam('name', 'world')}!"); } ]))); $app->run();
To see your new app in action, point Dialogflow's web hook configuration at /hook
on wherever you're hosting your app, create an intent with (or without) a name
parameter, set the action name on that intent to hello, check the box to use
the Web Hook for fulfillment, then call your new intent.
Now, let's break down what just happened:
- Dialogflow called our web hook, which Slim routed to the
DoublePassmiddleware. - That middleware handed the request off to the
Router, which marshaledQuestionandAnswerclasses. - the
Routersaw that theQuestionhad an action ofhello, which matched one of the handlers passed to it, so it called the handler, getting anAnswerback. - The
Routerpassed theAnswerback to the middleware, which updated the HTTPResponsewith the proper JSON payload.
Yu can learn more about each of these components later in this document.
Adding a Fallback
This library comes with a built-in, albeit generic, handler for when the action
on an incoming web hook request doesn't match anything in your route mapping.
The function signature is the same as a normal action handler, and any callable
(a class with __invoke(), a Closure, etc.) can be used here. Just pass the
callable as an additional parameter when creating your Router. Tweaking the
previous example:
<?php use iansltx\DialogflowBridge\{Middleware, Router, Question, Answer}; // require Composer autoloader here $app = new Slim\App(); $app->post('/hook', new Middleware\DoublePass(Router::buildFromClosureArray([ 'hello' => function(Question $question, Answer $answer) : Answer { return $answer->withSpeechAndText("Hello {$question->getParam('name', 'world')}!"); } ], function (Question $question, Answer $answer) : Answer { return $answer->withSpeechAndText("Sorry, I'm not sure what to do here."); }))); $app->run();
As your application gets more copmlex, you'll want to start...
Managing Handler Dependencies
An array of anonymous functions is a quick way to start handling web hook requests,
but you'll probably want to put your handlers in your main application DI container
as your project grows. For this case, you'll want to use Router::build() instead
of Router::buildFromClosureArray(). build() takes three parameters:
- An object that implements the PSR-11
ContainerInterface(e.g. Slim 3's container) - An array of mappings between action names (as keys) and container keys (as values). This provides a whitelist so incoming requests aren't pulling random services out of the container, and allows you to map multiple action names to a single dependency if you like.
- The optional fallback callable.
Taking the previous example, but assuming we've now moved our "hello" handler into the container as "helloHandler", we'd end up with:
<?php use iansltx\DialogflowBridge\{Middleware, Router, Question, Answer}; // require Composer autoloader here $app = new Slim\App(); $app->post('/hook', new Middleware\DoublePass(Router::build($app->getContainer(), [ 'hello' => 'helloHandler' ], function (Question $question, Answer $answer) : Answer { return $answer->withSpeechAndText("Sorry, I'm not sure what to do here."); }))); $app->run();
As you switch to class-based action handlers, you can (but don't need to) implement
HandlerInterface to ensure your handler method has the right signature.
More on Middlewares
The DoublePass class can be used as an actual middleware, rather than as a
Slim route callable, as needed; its third parameter is unused and not typehinted
to allow for this.
If you'd rather use a framework that relies on the proposed PSR-15 middleware
spec, set up an instance of the PSR15 class. In that case you'll need both
a Router instance (just like DoublePass) and a callable that, given
something JSON-serializeable as its only parameter, returns a PSR-7
ResponseInterface with the parameter JSON-encoded, plus the proper Content-Type
header. If you're using Zend Diactoros, building the middleware will look like:
$middleware = new Middleware\PSR15($router, function($data) { return new \Zend\Diactoros\JsonResponse($data); });
Objects
This library provides a couple of immutable objects that make interacting with Dialogflow web hooks a bit easier.
Question
Question wraps Dialogflow's web hook request JSON, allowing cleaner access to parameters, contexts, and other aspects of the web hook request. Normally you'll be provided a Question by the router, but you can also build a Question independently:
/** @var \Psr\Http\Message\ServerRequestInterface $request */ $question = Question::fromRequest($request); // from a PSR-7 request /** @var array $data */ $question = new Question($data); // from an array, e.g. by JSON-decoding the web hook request body
See method docblocks in the Question class for more information.
Answer
An Answer can be passed directly into json_decode() to produce the response body
needed for an Dialogflow web hook call. Normally this will get generated by calling
getBaseAnswer() on a Question. Doing so informs the Answer of which contexts are
currently set, so they can be dropped if needed. This is the way the router handles
Questions and Answers, but you can also call new Answer() directly.
See method docblocks in the Answer class for more information.
iansltx/dialogflow-bridge 适用场景与选型建议
iansltx/dialogflow-bridge 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 51 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 11 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「bot」 「api.ai」 「natural language」 「NLU」 「Dialogflow」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 iansltx/dialogflow-bridge 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 iansltx/dialogflow-bridge 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 iansltx/dialogflow-bridge 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dialogflow Fulfillment Webhook v1 & v2
Dialogflow php sdk
Dialogflow php sdk
DialogFlow Webhook Fulfillment PHP sdk
Laravel 5.6 Monolog custom telegram channel
Gets the complete thought from a verb and a noun using Moby Thesaurus
统计信息
- 总下载量: 51
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-11-13