1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458: 459: 460: 461: 462: 463: 464: 465: 466: 467: 468: 469: 470: 471: 472: 473: 474: 475: 476: 477: 478: 479: 480: 481: 482: 483: 484: 485: 486: 487: 488: 489: 490: 491: 492: 493: 494: 495: 496: 497: 498: 499: 500: 501: 502: 503: 504: 505: 506: 507: 508: 509: 510: 511: 512: 513: 514: 515: 516: 517: 518: 519: 520: 521: 522: 523: 524: 525: 526: 527: 528: 529: 530: 531: 532: 533: 534: 535: 536: 537: 538: 539: 540:
<?php
/**
* Copyright (c) 2017 Baidu, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @desc Bot-sdk基类。使用都需要继承这个类
* @author ******@baidu.com
**/
namespace Baidu\Duer\Botsdk;
abstract class Bot{
private $handler = [];
private $intercept = [];
private $event = [];
public $certificate;
/**
* DuerOS对Bot的请求。instance of Request
**/
public $request;
/**
* Bot返回给DuerOS的结果。instance of Response
**/
public $response;
/**
* DuerOS提供的session。instance of Session
* 短时记忆能力
**/
public $session;
/**
* 度秘NLU对query解析的结果。instance of Nlu
**/
public $nlu;
/**
* 统计Bot运行中产生的技能数据。instance of BotMonitor
**/
public $botMonitor;
/**
* 构造函数
* @example
* <pre>
* // 子类调用
* parent::__construct($requestBody, $privateKey);
* // 或者
* parent::__construct($privateKey);
* </pre>
*
* @param array $postData us对bot的数据。默认可以为空,sdk自行获取
* @param string $privateKey 私钥内容
* @return null
**/
public function __construct($postData=[], $privateKey = '') {
/**
* 如果第一个参数是字符串,认为是privateKey
*/
if ($postData && is_string($postData)) {
$privateKey = $postData;
$postData = false;
}
if(!$postData){
$rawInput = file_get_contents("php://input");
$rawInput = str_replace("", "", $rawInput);
$postData = json_decode($rawInput, true);
//Logger::debug($this->getSourceType() . " raw input" . $raw_input);
}
//$this->botMonitor = new BotMonitor($postData);
$this->request = new Request($postData);
$this->certificate = new Certificate($privateKey);
$this->session = $this->request->getSession();
$this->nlu = $this->request->getNlu();
$this->response = new Response($this->request, $this->session, $this->nlu);
}
/**
* @desc 添加对LaunchRequest 的处理函数
* @example
* <pre>
* $this->addLaunchHandler(function(){
* return [
* 'outputSpeech' => '欢迎使用'
* ];
* });
* </pre>
*
* @param function $func 处理函数。返回值作为response给DuerOS
* @return null
**/
protected function addLaunchHandler($func) {
return $this->addHandler('LaunchRequest', $func);
}
/**
* @desc 添加对SessionEndedRequest 的处理函数
* @example
* <pre>
* $this->addSessionEndedHandler(function(){
* // TODO: clear status
* });
* </pre>
*
* @param function $func 处理函数。DuerOS不会使用该返回值
* @return null
**/
protected function addSessionEndedHandler($func) {
return $this->addHandler('SessionEndedRequest', $func);
}
/**
* @desc 添加对特定意图的处理函数
* @example
* <pre>
* $this->addIntentHandler('intentName', function(){
* return [
* 'outputSpeech' => '你的意图,我已经处理好了'
* ];
* });
* </pre>
*
* @param string $intentName 意图名称
* @param function $func 处理函数。返回值作为response给DuerOS
* @return null
**/
protected function addIntentHandler($intentName, $func) {
return $this->addHandler('#'.$intentName, $func);
}
/**
* @desc 条件处理。顺序相关,优先匹配先添加的条件:
* 1、如果满足,则执行,有返回值则停止
* 2、满足条件,执行回调返回null,继续寻找下一个满足的条件
* @param string $mix 条件,比如意图以'#'开头'#intentName';或者是'LaunchRequest'、'SessionEndedRequest'
* @param function $func 处理函数,满足$mix条件后执行该函数
* @return null
**/
protected function addHandler($mix, $func=null){
if(is_string($mix) && $func) {
$arr = [];
$arr[] = [$mix => $func];
$mix = $arr;
}
if(!is_array($mix)) {
return;
}
foreach($mix as $item){
foreach($item as $k => $v) {
if(!$k || !$v) {
continue;
}
$this->handler[] = [
'rule' => $k,
'func' => $v,
];
}
}
}
/**
* @desc 拦截器
* 1、在event处理、条件处理之前执行Intercept.preprocess,返回非null,终止后续执行。将返回值返回
* 1、在event处理、条件处理之之后执行Intercept.postprocess
*
* @param Intercept $intercept
* @return null
**/
protected function addIntercept(Intercept $intercept){
$this->intercept[] = $intercept;
}
/**
* @desc 绑定一个事件的处理回调。
* @link http://developer.dueros.baidu.com/doc/dueros-conversational-service/device-interface/audio-player_markdown 具体事件参考
*
* @example
* <pre>
* $this->addEventListener('AudioPlayer.PlaybackStarted', function($event){
* return [
* 'outputSpeech' => '事件处理好啦',
* ];
* });
* </pre>
*
* @param string $event 绑定的事件名称,比如AudioPlayer.PlaybackStarted
* @param function $func 处理函数,传入参数为事件的request,返回值做完response给DuerOS
* @return null
**/
protected function addEventListener($event, $func){
if($event && $func) {
$this->event[$event] = $func;
}
}
/**
* @desc 快捷方法。获取第一个intent的名字
*
* @param null
* @return string
**/
public function getIntentName(){
if($this->nlu){
return $this->nlu->getIntentName();
}
}
/**
* @desc 快捷方法。获取session某个字段,与Session的getData功能相同
* @param string $field 属性的key
* @param string $default 如果该字段为空,使用$default返回
* @return string
**/
public function getSessionAttribute($field=null, $default=null){
return $this->session->getData($field, $default);
}
/**
* @desc 快捷方法。设置session某个字段,与Session的setData功能相同。
* $field = 'a.b.c' 表示设置session['a']['b']['c'] 的值
* @param string $field 属性的key
* @param string $value 对应的值
* @param string $default 如果$value为空,使用$default
**/
public function setSessionAttribute($field, $value, $default=null){
return $this->session->setData($field, $value, $default);
}
/**
* @desc 快捷方法。清空session,与Session的clear相同
* @param null
* @return null
**/
public function clearSessionAttribute(){
return $this->session->clear();
}
/**
* @desc 快捷方法。获取一个槽位的值,与Nlu中的getSlot相同
* @param string $field 槽位名
* @param integer $index 第几个intent,默认第一个
* @return string
**/
public function getSlot($field, $index = 0){
if($this->nlu){
return $this->nlu->getSlot($field, $index);
}
}
/**
* @desc 快捷方法。设置一个槽位的值,与Nlu中的setSlot相同
* @param string $field 槽位名
* @param string $value 槽位的值
* @param integer $index 第几个intent,默认第一个
* @return string
**/
public function setSlot($field, $value, $index = 0){
if($this->nlu){
return $this->nlu->setSlot($field, $value, $index);
}
}
/**
* @desc 告诉DuerOS,在多轮对话中,等待用户的回答。
* 注意:如果有设置Nlu的ask,SDK自动告诉DuerOS,无须调用
* @param null
* @return null
**/
public function waitAnswer(){
//should_end_session
$this->response->setShouldEndSession(false);
}
/**
* @desc 告诉DuerOS,需要结束对话
*
* @param null
* @return null
**/
public function endDialog(){
$this->response->setShouldEndSession(true);
}
/**
* @desc 事件路由添加后,需要执行此函数,对添加的条件、事件进行判断。
* 将第一个return 非null的结果作为此次的response
*
* @param boolean $build 如果为false:不进行response封装,直接返回handler的result
* @return array|string 封装后的结果为json string
**/
public function run($build=true){
// 验证请求签名
if(!$this->certificate->verifyRequest()) {
return $this->response->illegalRequest();
}
//handler event
$eventHandler = $this->getRegisterEventHandler();
//check domain
if($this->request->getType() == 'IntentRequset' && !$this->nlu && !$eventHandler) {
return $this->response->defaultResult();
}
//intercept beforeHandler
$ret = [];
foreach($this->intercept as $intercept) {
//$this->botMonitor->setPreEventStart();
$ret = $intercept->preprocess($this);
//$this->botMonitor->setPreEventEnd();
if($ret) {
break;
}
}
if(!$ret) {
//event process
if($eventHandler) {
//$this->botMonitor->setDeviceEventStart();
$event = $this->request->getEventData();
$ret = $this->callFunc($eventHandler, $event);
//$this->botMonitor->setDeviceEventEnd();
}else{
//$this->botMonitor->setEventStart();
$ret = $this->dispatch();
//$this->botMonitor->setEventEnd();
}
}
//intercept afterHandler
foreach($this->intercept as $intercept) {
//$this->botMonitor->setPostEventStart();
$ret = $intercept->postprocess($this, $ret);
//$this->botMonitor->setPostEventEnd();
}
//$this->botMonitor->setResponseData($ret);
if(!$build) {
return $ret;
}
return $this->response->build($ret);
}
/**
* @param null
* @return array
**/
protected function dispatch(){
if(!$this->handler) {
return;
}
foreach($this->handler as $item) {
if($this->checkHandler($item['rule'])) {
$ret = $this->callFunc($item['func']);
if($ret) {
return $ret;
}
}
}
}
/**
* @param null
* @return function
**/
private function getRegisterEventHandler() {
$eventData = $this->request->getEventData();
if($eventData['type']) {
$key = $eventData['type'];
if($this->event[$key]) {
return $this->event[$key];
}
}
}
/**
* @param function $func
* @param mixed $arg
* @return mixed
**/
private function callFunc($func, $arg=null){
$ret;
if(is_string($func)){
$ret = call_user_func([$this, $func], [$arg]);
}else{
$ret = $func($arg);
}
return $ret;
}
/**
* @param string $rule
* @return array
* [
* [
* 'type' => 'str',
* 'value' => 'babab\'\"ab session slot #gagga isset > gag',
* ],
* [
* 'type' => 'no_str',
* 'value' => '#intent',
* ],
* ]
**/
private function getToken($rule){
$token = [];
return $this->_getToken($token, $rule);
}
/**
* @param null
* @return null
**/
private function _getToken(&$token, $rule) {
if($rule === "" || $rule === null) {
return $token;
}
$rgCom = '/[^"\']*/';
preg_match($rgCom, $rule, $m);
$token[] = [
"type" => "no_str",
"value" => $m[0],
];
$last = substr($rule, mb_strlen($m[0]));
if($last !== "" || $last !== null){
for($i=1;$i<mb_strlen($last);$i++){
$c = $last[$i];
if($c == "\\"){
++$i;
continue;
}
if($c == $last[0]){
$s = substr($last, 0, $i + 1);
$last = substr($last, mb_strlen($s));
$token[] = [
"type" => "str",
"value" => $s,
];
break;
}
}
}
if($last){
return $this->_getToken($token, $last);
}
return $token;
}
/**
* @param string $handler
* @return boolean
**/
private function checkHandler($handler){
$token = $this->getToken($handler);
if(!is_array($token)) {
return false;
}
$arr = [];
foreach($token as $t) {
if($t['type'] == 'str') {
$arr[] = $t['value'];
}else{
$arr[] = $this->tokenValue($t['value']);
}
}
$str = implode('', $arr);
//字符串中有$
$str = str_replace('$', '\$', $str);
//var_dump($str);
$func = create_function('', 'return ' . implode('', $arr) . ';');
return $func();
}
/**
* @param string $str
* @return string
**/
private function tokenValue($str){
if($str === '' || $str === null) {
return '';
}
$rg = [
'intent' => '/#([\w\.\d_]+)/',
'session' => '/session\.([\w\.\d_]+)/',
'slot' => '/slot\.([\w\d_]+)/',
'requestType' => '/^(LaunchRequest|SessionEndedRequest)$/',
];
$self = $this;
foreach($rg as $k=>$r) {
$str = preg_replace_callback($r, function($m) use($self, $k){
if($k == 'intent'){
return json_encode($self->getIntentName() == $m[1]);
}else if($k == 'session') {
return json_encode($self->getSessionAttribute($m[1]));
}else if($k == 'slot') {
return json_encode($self->getSlot($m[1]));
}else if($k == 'requestType') {
return json_encode($self->request->getType() == $m[1]);
}
}, $str);
}
return $str;
}
}