Overview

Namespaces

  • Baidu
    • Duer
      • Botsdk
        • Card
        • Directive
          • AppLauncher
          • AudioPlayer
          • WebBrowser
        • Plugins

Classes

  • Baidu\Duer\Botsdk\Bot
  • Baidu\Duer\Botsdk\Card\BaseCard
  • Baidu\Duer\Botsdk\Card\ImageCard
  • Baidu\Duer\Botsdk\Card\ListCard
  • Baidu\Duer\Botsdk\Card\ListCardItem
  • Baidu\Duer\Botsdk\Card\StandardCard
  • Baidu\Duer\Botsdk\Card\TextCard
  • Baidu\Duer\Botsdk\Certificate
  • Baidu\Duer\Botsdk\Directive\AppLauncher\LaunchApp
  • Baidu\Duer\Botsdk\Directive\AudioPlayer\Play
  • Baidu\Duer\Botsdk\Directive\AudioPlayer\Stop
  • Baidu\Duer\Botsdk\Directive\BaseDirective
  • Baidu\Duer\Botsdk\Directive\WebBrowser\LaunchBrowser
  • Baidu\Duer\Botsdk\Intercept
  • Baidu\Duer\Botsdk\Log
  • Baidu\Duer\Botsdk\Nlu
  • Baidu\Duer\Botsdk\Plugins\DuerSessionIntercept
  • Baidu\Duer\Botsdk\Request
  • Baidu\Duer\Botsdk\Response
  • Baidu\Duer\Botsdk\Session

Traits

  • Baidu\Duer\Botsdk\DataObject
  • Overview
  • Namespace
  • Class
  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: 
<?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对DuerOS的返回结果
 * @author ******@baidu.com
 **/
namespace Baidu\Duer\Botsdk;

class Response{
    /**
     * Requset 实例。DuerOS的请求
     **/
    private $request;

    /**
     * Session
     **/
    private $session;

    /**
     * Nlu
     **/
    private $nlu;

    /**
     * 返回结果的标识。
     **/
    private $sourceType;

    /**
     * 多轮情况下,是否需要client停止对用户的等待输入
     **/
    private $shouldEndSession = true;

    /**
     * @param Request $request 请求对象
     * @param Session $session session对象
     * @param Nlu $nlu nlu对象
     * @return null
     **/
    public function __construct($request, $session, $nlu){
        $this->request = $request;
        $this->session = $session;
        $this->nlu = $nlu;
        $this->sourceType = $this->request->getBotId();
    }

    /**
     * 设置对话结束
     *
     * @param null
     * @return null
     **/
    public function setShouldEndSession($val){
        if($val === false) {
            $this->shouldEndSession = false; 
        }else if($val === true){
            $this->shouldEndSession = true; 
        }
    }


    /**
     * @desc 当没有结果时,返回默认值
     * @param null
     * @return null
     **/
    public function defaultResult(){
        return json_encode(['status'=>0, 'msg'=>null]);
    }

    /** 
     * @desc 构造response返回结果
     * @param array $data 数据
     * @example
     * <pre>
     * $data = [
     *    "card"=> $card // instanceof Card\BaseCard
     *    "directives"=> $directives  // array
     *    "outputSpeech"=> "string"
     *    "reprompt" => "string"
     *  ]
     * </pre>
     * @return string
     */
    public function build($data){
        if($this->nlu && $this->nlu->hasAsked()){
            $this->shouldEndSession = false;
        }

        $directives = $data['directives'] ? $data['directives'] : [];
        //directive to data
        $directives = array_values(array_filter(array_map(function($directive){
            if($directive instanceof Directive\BaseDirective) {
                return $directive->getData();    
            } 
        }, $directives))); 

        if($this->nlu){
            $arr = $this->nlu->toDirective();
            if($arr) {
                $directives[] = $arr;
            }
        }

        if(!$data['outputSpeech'] && $data['card'] && $data['card'] instanceof Card\TextCard) {
            $data['outputSpeech'] = $data['card']->getData('content');
        }

        $ret = [
            'version' => '2.0',
            'context' => ($this->nlu ? $this->nlu->toUpdateIntent() : null)?($this->nlu ? $this->nlu->toUpdateIntent() : null)    :(object)[], 
            'session' => $this->session->toResponse(),
            'response' => [
                //'needDetermine' => $this->confirm ? true : false,
                'directives' => $directives,
                'shouldEndSession' => $this->shouldEndSession,
                'card' => $data['card']?$data['card']->getData():null,
                'resource' => $data['resource'],
                'outputSpeech' => $data['outputSpeech']?$this->formatSpeech($data['outputSpeech']):null,
                'reprompt' => $data['reprompt']?[
                    'outputSpeech' => $this->formatSpeech($data['reprompt']),
                ]:null
            ]
        ];

        
        $str=json_encode($ret, JSON_UNESCAPED_UNICODE);
        return $str;
    }

    /**
     * @desc 通过正则<speak>..</speak>,判断是纯文本还是ssml,生成对应的format
     * @param string|array $mix 输入的语句
     * @return array
     **/
    public function formatSpeech($mix){
        if(is_array($mix)) {
            return $mix; 
        }

        if(preg_match('/<speak>/', $mix)) {
            return [
                'type' => 'SSML',
                'ssml' => $mix,
            ]; 
        }else{
            return [
                'type' => 'PlainText',
                'text' => $mix,
            ];
        }
    }

    public function illegalRequest() {
        return json_encode(['status'=>1, 'msg'=>'非法请求']);
    }
}
API documentation generated by ApiGen