跳转至

消息审计

消息审计即为会话消息回调。用户在有度客户端发起的会话消息均会回调至设置的URL地址。

回调包括会话的创建和修改,以及发送文本、图片、文件、语音、图文混合等消息类型。

收到回调后应立即回复JSON响应全局状态码ERRCODE_OK(0)或字符串"OK",否则会重复收到回调。

回调地址设置

在有度管理后台->管理工具->消息审计 设置回调URL,URL必须为完整的HTTP URL

img

初始化客户端

int buin = 14731009; // 请填写企业总机号码
String appName = "消息审计"; //请填写消息审计名称
String appId = "sysMsgMonitor"; // 请填写消息审计AppId
String appToken = "ks14276pp1gwsxqd"; //请填写消息审计token
String appAesKey = "Lv77EOpSDjehnF/dwG7TASqMOMZiyhxrP7H1vhoRuCA="; // 请填写消息审计EncodingAesKey
int SERVER_PORT = 8080; //监听的端口
AppServer server;
try {
    server = new AppServer(SERVER_PORT);
} catch (ServiceException e) {
    e.printStackTrace();
}
YDApp ydApp = new YDApp(buin, "", appName, appId, appToken, appAesKey);

集成示例

如果仅测试功能,可以使用YDSDK中AppServerTest测试用例进行测试。

此处为Web项目集成消息审计功能。

功能说明

接收消息审计回调

方法1-接收消息审计推送(依赖方法2)

@RequestMapping("/ydapp/a/receive")
@ResponseBody
public String receive(HttpServletRequest request) throws IOException {
    int buin = 14731009; // 请填写企业总机号码
    String appName = "消息审计"; //请填写消息审计名称
    String appId = "sysMsgMonitor"; // 请填写消息审计AppId
    String appToken = "ks14276pp1gwsxqd"; //请填写消息审计token
    String appAesKey = "Lv77EOpSDjehnF/dwG7TASqMOMZiyhxrP7H1vhoRuCA="; // 请填写消息审计EncodingAesKey

    YDApp ydApp = new YDApp(buin, "", appName, appId, appToken, appAesKey);

    //请求参数
    Map<String, String> map = new HashMap<>(3);
    Enumeration<String> keys = request.getParameterNames();
    while (keys.hasMoreElements()) {
        String key = keys.nextElement();
        map.put(key, request.getParameter(key));
    }

    //请求数据体
    InputStream in = request.getInputStream();
    byte[] reqData = Helper.readInputStream(in);

    AppClient appClient = new AppClient(ydApp);
    try {
        //处理接收到的数据体
        handleReceive(map, reqData, appClient, ydApp);
    } catch (GeneralEntAppException e) {
        e.printStackTrace();
    }
    //返回接收成功,不返回接收成功会重复收到消息推送
    return "ok";
}

方法2-处理加密消息(依赖方法3)

/**
 * 解密消息体
 * @param reqParam  请求参数
 * @param reqData 请求数据体
 * @param appClient 客户端
 * @param app 客户端实体
 * @throws GeneralEntAppException 异常
 * @throws IOException 异常
 */
private void handleReceive(Map<String, String> reqParam, byte[] reqData, AppClient appClient, YDApp app) throws GeneralEntAppException, IOException {
    String content = Helper.utf8String(reqData);
    JsonObject reqJson = Helper.parseJson(content);
    int buin = Helper.getInt("toBuin", reqJson);
    String appId = Helper.getString("toApp", reqJson);
    String encrypt = Helper.getString("encrypt", reqJson);
    if (buin != app.getBuin()) {
        System.out.println(String.format("[%s][error] from buin %d is unequal local buin %d", app.getAppName(), buin, app.getBuin()));
        return;
    }

    if (!appId.equals(app.getAppId())) {
        System.out.println(String.format("[%s][error] from appId %s is unequal local appId %s", app.getAppName(), appId, app.getAppId()));
        return;
    }

    boolean fromYD = Helper.signatureIsValid(reqParam, app.getToken(), encrypt);
    if (!fromYD) {
        System.out.println(String.format("[%s][error] signature is invalid", app.getAppName()));
        return;
    }
    ReceiveMessage msg = appClient.decrypt(encrypt);
    receive(msg);
}

方法3-处理解密后的明文消息

/**
 * 接收到的明文消息
 * @param message 消息类
 */
private void receive(ReceiveMessage message) {
    try {
        String msgType = message.getMsgType();
        switch (msgType) {
            case Const.Message_App_Type_Event: {
                EventBody event = message.getAsEvent();
                System.out.println(String.format("receive app event, appId:%s, operation:%s ", event.getId(), event.getOperation()));
                break;
            }
            case Const.Message_App_Type_Sms: {
                SmsBody smsMsg = message.getAsSmsMsg();
                System.out.println(String.format("receive app sms msg: %s", smsMsg.getContent()));
                break;
            }
            case Const.Message_Session_Type_Text: {
                String text = message.getAsTextMsg();
                System.out.println(String.format("receive text msg: %s", text));
                break;
            }
            case Const.Message_Session_Type_File: {
                FileBody appFile = message.getAsFileMsg();
                System.out.println(String.format("receive file msg, mediaId:: %s, you can download it by AppClient::downloadFileAndSave", appFile.getMediaId()));
                break;
            }
            case Const.Message_Session_Type_Image: {
                ImageBody appImg = message.getAsImageMsg();
                System.out.println(String.format("receive img msg mediaId: %s, you can download it by AppClient::downloadImageAndSave", appImg.getMediaId()));
                break;
            }
            case Const.Message_Session_Create: {
                SessionCreateBody sessionCreate = message.getAsSessionCreate();
                System.out.println(String.format("receive session create event: %s", sessionCreate.toJsonString()));
                break;
            }
            case Const.Message_Session_Update: {
                SessionUpdateBody sessionUpdate = message.getAsSessionUpdate();
                System.out.println(String.format("receive sesion update event: %s", sessionUpdate.toJsonString()));
                break;
            }
            case Const.Message_Session_Type_Audio: {
                AudioBody audio = message.getAsAudioMsg();
                System.out.println(String.format("receive audio msg, mediaId:%s", audio.getMediaId()));
                break;
            }
            case Const.Message_Session_Type_Complex: {
                ComplexBody complexBody = message.getAsComplexMsg();
                System.out.println(String.format("receive complex msg: %s", complexBody.getBody()));
                break;
            }
            case Const.Message_Session_Type_System: {
                RecvSystemMsgBody systemMsgBody = message.getAsSystemMsg();
                System.out.println(String.format("receive system msg: %s", systemMsgBody.getMsg()));
                break;
            }
            case Const.Message_Session_Type_Broadcast: {
                RecvBroadcastBody broadcastBody = message.getAsBroadcastMsg();
                System.out.println(String.format("receive broadcast msg: %s", broadcastBody.getMsg()));
                break;
            }
            default: {
                break;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}