java腾讯云发送私聊
public String sendMessageToUser(Message message) throws FdServiceException {
String flag = "fail";
try {
if (StringUtils.isNullOrEmpty(message.getMessageAuthor())) {
throw new FdServiceException(ErrorCode.QUERY_EXCEPTION, "消息发送者为空");
}
if (StringUtils.isNullOrEmpty(message.getReceiverNo())) {
throw new FdServiceException(ErrorCode.QUERY_EXCEPTION, "消息接收者为空");
}
if (StringUtils.isNullOrEmpty(message.getMessageContent())) {
throw new FdServiceException(ErrorCode.QUERY_EXCEPTION, "消息内容为空");
}
Map<String, Object> map = new HashMap<>();
map.put("SyncOtherMachine", 1);
map.put("From_Account", message.getMessageAuthor());
map.put("To_Account", message.getReceiverNo());
map.put("MsgRandom", TXCloudHelper.randomInt(Integer.MAX_VALUE));
map.put("MsgTimeStamp", System.currentTimeMillis() / 1000);
List<Object> msgList = new ArrayList();
Map<String, Object> MsgContent = new HashMap<>();
MsgContent.put("Text", message.getMessageContent());
Map<String, Object> msgBody = new HashMap<>();
msgBody.put("MsgType", "TIMTextElem");
msgBody.put("MsgContent", MsgContent);
msgList.add(msgBody);
map.put("MsgBody", msgList);
String json = JSON.toJSONString(map);
JSONObject jObject = JSON.parseObject(TXCloudHelper.executePost("https://console.tim.qq.com/v4/openim/sendmsg?sdkappid=" + CloudSignHelper.skdAppid +
"&identifier=" + CloudSignHelper.identifier +
"&usersig=" + CloudSignHelper.GetSign() +
"&random=" + randomInt() +
"&contenttype=json", json));
if ("OK".equals(jObject.get("ActionStatus").toString()) && "0".equals(jObject.get("ErrorCode").toString())) {
flag = "success";
}
} catch (FdServiceException e) {
e.printStackTrace();
throw new FdServiceException(ErrorCode.QUERY_EXCEPTION, e.getExceptionInfo());
}
return flag;
}