指令回调处理
更新时间:2020-03-17
用户输入语音或者文字信息后,SDK会根据上下文识别出用户的意图,并返回对应的指令(directive),开发者通过传入指令监听器(IDirectiveListener)的实现来接收意图指令,并进行对应逻辑处理。
目前SDK内置的IDirectiveListener接口如下图:
如图,目前共有八种IDirectiveListener
的实现。
开发者需要在SDK中注册自己相应的IDirectiveListener
来获取SDK的指令回调,步骤如下:
- 开发者编写
IDirectiveListener
的接口实现类 - 通过
RobotSDKEngine
的addDirectiveListeners/addDirectiveListener
方法传入IDirectiveListener
实现类到SDK中 - 当接口对应事件触发时,在
IDirectiveListener
的回调方法中获得对应数据并实现相应业务逻辑
可以通过以下代码片段来理解这段过程:
Java
1// 1. 实现 IDirective 的接口实现类,此处以 IDialogListener 为例
2IDialogListener mDialogListener = new IDialogListener() {
3 @Override
4 public void onVoiceOutput(String content) {
5 // 3. 处理NLU解析返回的指令...
6 // 处理语音回复播放相关逻辑
7 }
8
9 @Override
10 public void onTextOutput(String content) {
11 // 3. 处理NLU解析返回的指令...
12 // 处理文字对话显示相关逻辑
13 }
14
15 @Override
16 public void onHints(List <string> hints) {
17 // 3. 处理NLU解析返回的指令...
18 // 处理提示文案显示相关逻辑
19 }
20
21 @Override
22 public void onEnd() {
23 // 3. 处理NLU解析返回的指令...
24 // 对话结束相关逻辑
25 }
26 };
27
28......
29
30// 2. 将接口实现类传入 SDK
31RobotSDKEngine.getInstance().addDirectiveListener(mDialogListener);
32RobotSDKEngine.getInstance().addDirectiveListener(NamespaceGroup.DIALOG, mDialogListener);</string>
各个Listener说明如下:
IDialogListener
IDialogListener :对话相关指令监听器
Java
1public interface IDialogListener extends IDirectiveListener {
2 /**
3 * 语音播报指令
4 *
5 * @param content 需要播报的语音内容
6 */
7 void onVoiceOutput(String content);
8
9 /**
10 * 文字回复展示指令
11 *
12 * @param content 回复用户的对话内容
13 */
14 void onTextOutput(String content);
15
16 /**
17 * 提示文本展示指令
18 *
19 * @param hints 需要展示的提示文本 List
20 */
21 void onHints(List <string> hints);
22
23 /**
24 * 对话结束指令
25 *
26 * 当用户说出对话结束关键字(例如"再见"、"拜拜")时触发回调
27 *
28 */
29 void onEnd();
30}</string>
IScreenListener
IScreenListener - 屏幕内容展示相关指令
Java
1public interface IScreenListener extends IDirectiveListener {
2
3 /**
4 * 文本卡片展示指令
5 * @param card 文本类型卡片 model
6 */
7 void onRenderTextCard(TextCard card);
8
9 /**
10 * 图片卡片展示指令
11 * @param card 图片卡片 model
12 */
13 void onRenderImageCard(ImageCard card);
14
15 /**
16 * 普通列表卡片展示指令
17 * @param listCard 普通列表卡片 model
18 */
19 void onRenderNormalList(ListCard <normalcarditem> listCard);
20
21 /**
22 * 图片列表卡片展示指令
23 * @param listCard 图片列表卡片 model
24 */
25 void onRenderSimpleImgList(ListCard<simpleimagecarditem> listCard);
26
27 /**
28 * 视频列表卡片展示指令
29 * @param listCard 视频列表卡片 model
30 */
31 void onRenderVideoList(ListCard<videocarditem> listCard);
32
33 /**
34 * 用户卡片展示指令
35 * @param card 用户卡片 model
36 * @param data 用户照片的二进制数据
37 */
38 void onRenderPerson(UserCard card, byte[] data);
39
40 /**
41 * 天气卡片展示指令
42 * @param card 天气卡片 model
43 */
44 void onRenderWeather(WeatherInfoCard card);
45
46 /**
47 * 表情展示指令
48 * @param expression 表情关键字,例如 Sad、Happy
49 */
50 void onRenderExpression(String expression);
51}</videocarditem></simpleimagecarditem></normalcarditem>
IActionListener
IActionListener - 动作控制相关指令
Java
1public interface IActionListener extends IDirectiveListener {
2 /**
3 * 行走指令
4 * @param direction 行走方向
5 * @param distance 行走距离
6 * @param distanceUnit 距离单位
7 */
8 void onWalk(String direction, String distance, String distanceUnit);
9
10 /**
11 * 转向指令
12 * @param direction 转向方向
13 * @param angle 转向角度
14 * @param angleUnit 角度单位
15 */
16 void onTurn(String direction, String angle, String angleUnit);
17
18 /**
19 * 举手指令
20 * @param hands hand字段可为以下内容:hand, right_hand, left_hand, double_hand
21 */
22 void onRaiseHands(String hands);
23
24 /**
25 * 巡航指令
26 * @param switchStatus 状态开关
27 */
28 void onCruise(String switchStatus);
29
30 /**
31 * 充电指令
32 * @param switchStatus 状态开关
33 */
34 void onCharge(String switchStatus);
35
36 /**
37 * 握手指令
38 */
39 void onShakeHands();
40
41 /**
42 * 拥抱指令
43 */
44 void onHug();
45
46 /**
47 * 摇头指令
48 */
49 void onTwistHead();
50
51 /**
52 * 向左看指令
53 */
54 void onTurnHeadLeft();
55
56 /**
57 * 向右看指令
58 */
59 void onTurnHeadRight();
60
61 /**
62 * 停止运动指令
63 */
64 void onStop();
65
66 /**
67 * 打招呼指令
68 */
69 void onWave();
70
71 /**
72 * 动一下指令
73 */
74 void onDoAnAction();
75}
ISpeakerControllerListener
ISpeakerControllerListener - 扬声器控制指令
Java
1public interface ISpeakerControllerListener extends IDirectiveListener {
2 /**
3 * 静音指令
4 */
5 void onSetMute();
6
7 /**
8 * 音量调节指令
9 * @param volumeControl
10 * @param volumeValue
11 */
12 void onAdjustVolume(String volumeControl, String volumeValue);
13}
IInstructionListener
IInstructionListener - 特殊指令
Java
1public interface IInstructionListener extends IDirectiveListener {
2
3 /**
4 * 点亮查询指令
5 */
6 void onInquireEnergy();
7
8 /**
9 * 展示功能指令
10 */
11 void onShowFeatures();
12}
IErrorListener
IErrorListener - 错误监听
Java
1public interface IErrorListener extends IDirectiveListener {
2 /**
3 * 错误监听
4 * @param code 错误码
5 * @param errorDescription 错误描述
6 */
7 void onError(int code, String errorDescription);
8}
IStateListener
IStateListener - 指令处理状态监听
Java
1public interface IStateListener extends IDirectiveListener {
2 /**
3 * 当NLU能力识别完成本次对话请求中的用户意图,
4 * SDK 开始向开发者回传本次对话请求返回的意图指令(Directives)时回调
5 */
6 void onDirectiveStart();
7
8 /**
9 * 当 SDK 完成了本次对话请求返回的全部意图指令的回传时回调
10 */
11 void onDirectiveStop();
12}
ICustomListener
ICustomListener - 用户自定义能力监听
Java
1public interface ICustomListener extends IDirectiveListener {
2
3 /**
4 * 用户自定义意图回调监听
5 *
6 * @param namespace 指令 namespace
7 * @param name 指令 name
8 * @param payloads 指令参数 map
9 * @return
10 */
11 boolean onDirectiveReceived(String namespace, String name, HashMap payloads);
12}