Class: Bot

Bot(postData)

Bot基类. 请继承此类

Constructor

new Bot(postData)

构造函数,以及DuerOS请求bot的request为参数,request协议参考[http://TODO]
Parameters:
Name Type Description
postData Object DuerOS请求body
Source:
Example
const BaseBot = require('bot-sdk');
class MyBot extends BaseBot {
    constructor(postData) {
         super(postData);
         //...
    }
}

Namespaces

Card
Directive

Methods

addEventListener(event, handler) → {Bot}

对一个事件添加处理函数。比如设备端反馈的音频播放开始事件
Parameters:
Name Type Description
event string 事件名
handler function 事件处理函数。返回值非null,将作为bot的response返回DuerOS 函数返回值参考Response.build() 的输入参数
Source:
Returns:
返回自己
Type
Bot
Example
this.addEventListener('Audio', (event)=>{
    // event 为事件数据 
    // 具体数据结构参考[TODO]
});

addIntentHandler(intent, handler) → {Bot}

对一个intent添加处理函数
Parameters:
Name Type Description
intent string 意图名:'intentName'
handler function 意图处理函数。返回值非null,将作为bot的response返回DuerOS 函数返回值参考Response.build() 的输入参数
Source:
Returns:
返回自己
Type
Bot
Example
this.addIntentHandler('intentName', ()=>{
    //this.getSlot('slotName');
});

addLaunchHandler(handler) → {Bot}

对SessionEnded添加处理函数
Parameters:
Name Type Description
handler function 意图处理函数。返回值非null,将作为bot的response返回DuerOS 函数返回值参考Response.build() 的输入参数
Source:
Returns:
返回自己
Type
Bot
Example
this.addLaunchHandler(()=>{
     // 进入bot,提示用户如何操作
});

addSessionEndedHandler(handler) → {Bot}

对SessionEnded添加处理函数
Parameters:
Name Type Description
handler function 意图处理函数。返回值非null,将作为bot的response返回DuerOS 函数返回值参考Response.build() 的输入参数
Source:
Returns:
返回自己
Type
Bot
Example
this.addSessionEndedHandler(()=>{
    // todo some clear job
    // this.clearSession()
});

clearSessionAttribute() → {null}

清空session的所有字段
Source:
Returns:
Type
null

endSession()

设置多轮结束,此时bot结束多轮对话
Source:

getIntentName() → {string|null}

获取DuerOS请求中的意图名
Source:
Returns:
Type
string | null

getSessionAttribute(field, defaultValue) → {Mixied}

获取session的一个字段对应的值
Parameters:
Name Type Default Description
field string null 字段名
defaultValue Mixed null 默认值 当此字段没有值时,返回defaultValue
Source:
Returns:
Type
Mixied

getSlot(field, index) → {string}

根据槽位名获取槽位对应的值
Parameters:
Name Type Default Description
field string 槽位名
index Integer 0 第几个intent,默认第一个
Source:
Returns:
Type
string

run(build) → {string}

bot执行的入口
Parameters:
Name Type Default Description
build boolean true 是否需要打包response,输出JSON String
Source:
Returns:
返回JSON
Type
string

setSessionAttribute(field, value, defaultValue) → {null}

设置session的一个字段的值
Parameters:
Name Type Default Description
field string 字段名
value Mixed 字段对应的值
defaultValue Mixed null 默认值 当value为空时,使用defaultValue
Source:
Returns:
Type
null

setSlot(field, value, index) → {null}

设置槽位的值。如果该槽位不存在,新增一个槽位名,并设置对于的值
Parameters:
Name Type Default Description
field string 槽位名
value string
index Integer 0 第几个intent,默认第一个
Source:
Returns:
Type
null

waitAnswer() → {null}

设置多轮继续,等待用户回复
Source:
Returns:
Type
null