6.3 消息结构
6.3.1 异常消息
异常消息通过 MessageListener
的 onError(V5KFException e)
函数回调,上表列出了来自服务端的异常代码和对应含义(ExceptionServerResponse
类型的异常),SDK 中的异常类型通过枚举定义,具体如下:
public enum V5ExceptionStatus {
ExceptionNoError, // 没有错误
ExceptionNotInitialized, // SDK未初始化或初始化失败
ExceptionAccountFailed, // 账号信息认证失败
ExceptionNotConnected, // 尚未建立连接
ExceptionMessageSendFailed, // 消息发送失败(MessageSendCallback参数)
ExceptionImageUploadFailed, // 图片上传失败
ExceptionNoNetwork, // 未连接到网络
ExceptionConnectionError, // 网络请求错误
ExceptionWSAuthFailed, // ws的Authorization认证失败
ExceptionConnectRepeat, // 客户端出现重复连接
ExceptionServerResponse, // 错误类型为服务器返回
ExceptionNoAudioPermission, // 无录音权限
ExceptionUnknownError; // 未知错误
}
6.3.2 会话消息
发送和接收消息都是 JSON 格式字符串,其中 o_type
为 message
的消息为会话消息,通过转换为消息对象 V5Message
传递,包含文本、位置、图片等子类消息,基类 V5Message
中主要成员定义如下:
private int state; // 消息状态发送状态
private int hit; // 问题命中与否:
// 0-问题未能有效回答
// 1-问题找到合适答案
private int message_type; // 消息类型
private String message_id; // 消息ID
private long msg_id; // 自定义消息ID,若传出去的消息携带此id则对应的机器人回复也携带此ID
private int direction; // 消息标志,定义如下:
// 0 – 座席发出的消息
// 1 – 客户发出的消息
// 2 – 机器人发出的消息
// 7 – 发给座席的求助信息
// 8 – 相关问题消息
// 9–评价问卷
private long create_time; // 时间戳
private List<V5Message> candidate; // 相关问题内容
protected JSONObject custom_content; // 自定义magic参数,键值对数组形式
消息状态有:V5Message.STATE_ARRIVED
、V5Message.STATE_FAILURE
、 V5Message.STATE_SENDING
、V5Message.STATE_UNKNOW
四种。
V5Message
是所有类型消息的基类,通过“message_type
”来区分消息类型,单个消息仅包含一种类型的消息内容,目前支持的消息类型示例如下。
消息内容中不同消息类型对应的成员定义如下:
当接收到的消息类型为 SDK 所不支持的类型时,该类型会以 V5JSONMessage
来表示,内含一个 JSONObject 类型成员,包含接收到的完整消息内容。
会话应答消息通过MessageListener
的onMessage(V5Message message)
函数回调,接收到的文本消息示例如下,即一个 V5TextMessage
包含的信息:
{
"content": "你好!",
"create_time": "1447323666",
"direction": 2,
"hit": 1,
"message_type": 1,
"o_type": "message"
}
接收文本消息的处理:
public void onMessage(V5Message message) {
if (message.getMessage_type() == V5MessageDefine.MSG_TYPE_TEXT) {
V5TextMessage textMessage = (V5TextMessage) message;
// 处理文本消息
// ......
}
}