一. 腾讯IM快速入门
1. 注册腾讯云账号,找到即时通信IM控制台
腾讯IM控制台:https://console.cloud.tencent.com/im
2. 创建即时通信应用
3. 进到应用里面可以看到这个应用的SDKAppID
和key
(密钥)
4. 可以参考官方提供的“服务端API接口文档”和“接口调试工具”
API接口文档:https://cloud.tencent.com/document/product/269
接口在线调试工具:https://tcc.tencentcs.com/im-api-tool/index.html
二. 基于Java对一些方法的简单封装
注:封装的方法覆盖得肯定不全面,可以根据自身需求在此基础上补充
1. 配置文件``
#腾讯im相关配置
silence.tencent.cloud.im.sdkAppId=yourSDKAppId
silence.tencent.cloud.im.key=yourKey
2. 腾讯im相关API接口常量类TencentCloudImApiConstant.java
/**
* @description: 腾讯im相关API接口
* @author: zyb
* @date: 2021/5/27 17:58
*/
public class TencentCloudImApiConstant {
/**
* 账号管理
*/
public static class AccountManage {
/**导入单个帐号*/
public static final String ACCOUNT_IMPORT = "v4/im_open_login_svc/account_import";
/**导入多个帐号*/
public static final String MULTI_ACCOUNT_IMPORT = "v4/im_open_login_svc/account_import";
/**删除帐号*/
public static final String ACCOUNT_DELETE = "v4/im_open_login_svc/account_delete";
/**查询帐号*/
public static final String ACCOUNT_CHECK = "v4/im_open_login_svc/account_check";
/**失效帐号登录状态*/
public static final String ACCOUNT_KICK = "v4/im_open_login_svc/kick";
/**查询账号在线状态*/
public static final String ACCOUNT_QUERY_STATE = "v4/openim/querystate";
}
/**
* 单聊消息
*/
public static class SingleChatManage {
/**单发单聊消息*/
public static final String SEND_MSG = "v4/openim/sendmsg";
/**批量发单聊消息*/
public static final String BATCH_SEND_MSG = "v4/openim/batchsendmsg";
/**导入单聊消息*/
public static final String IMPORT_MSG = "v4/openim/importmsg";
/**查询单聊消息*/
public static final String ADMIN_GET_ROAM_MSG = "v4/openim/admin_getroammsg";
/**撤回单聊消息*/
public static final String ADMIN_MSG_WITH_DRAW = "v4/openim/admin_msgwithdraw";
/**设置单聊消息已读*/
public static final String ADMIN_SET_MSG_READ = "v4/openim/admin_set_msg_read";
}
/**
* 全员推送
*/
public static class AllPushManage {
/**全员推送*/
public static final String IM_PUSH = "v4/all_member_push/im_push";
/**设置应用属性名称*/
public static final String IM_SET_ATTR_NAME = "v4/all_member_push/im_set_attr_name";
/**获取应用属性名称*/
public static final String IM_GET_ATTR_NAME = "v4/all_member_push/im_get_attr_name";
/**获取用户属性*/
public static final String IM_GET_ATTR = "v4/all_member_push/im_get_attr";
/**设置用户属性*/
public static final String IM_SET_ATTR = "v4/all_member_push/im_set_attr";
/**删除用户属性*/
public static final String IM_REMOVE_ATTR = "v4/all_member_push/im_remove_attr";
/**获取用户标签*/
public static final String IM_GET_TAG = "v4/all_member_push/im_get_tag";
/**添加用户标签*/
public static final String IM_ADD_TAG = "v4/all_member_push/im_add_tag";
/**删除用户标签*/
public static final String IM_REMOVE_TAG = "v4/all_member_push/im_remove_tag";
/**删除用户所有标签*/
public static final String IM_REMOVE_ALL_TAGS = "v4/all_member_push/im_remove_all_tags";
}
/**
* 资料管理
*/
public static class PortraitManage {
/**设置资料*/
public static final String PORTRAIT_SET = "v4/profile/portrait_set";
/**拉取资料*/
public static final String PORTRAIT_GET = "v4/profile/portrait_get";
}
/**
* 关系链管理
*/
public static class RelationManage {
/**添加好友*/
public static final String FRIEND_ADD = "v4/sns/friend_add";
/**导入好友*/
public static final String FRIEND_IMPORT = "v4/sns/friend_import";
/**更新好友*/
public static final String FRIEND_UPDATE = "v4/sns/friend_update";
/**删除好友*/
public static final String FRIEND_DELETE = "v4/sns/friend_delete";
/**删除所有好友*/
public static final String FRIEND_DELETE_ALL = "v4/sns/friend_delete_all";
/**校验好友*/
public static final String FRIEND_CHECK = "v4/sns/friend_check";
/**拉取好友*/
public static final String FRIEND_GET = "v4/sns/friend_get";
/**拉取指定好友*/
public static final String FRIEND_GET_LIST = "v4/sns/friend_get_list";
/**添加黑名单*/
public static final String BLACK_LIST_ADD = "v4/sns/black_list_add";
/**删除黑名单*/
public static final String BLACK_LIST_DELETE = "v4/sns/black_list_delete";
/**拉取黑名单*/
public static final String BLACK_LIST_GET = "v4/sns/black_list_get";
/**校验黑名单*/
public static final String BLACK_LIST_CHECK = "v4/sns/black_list_check";
/**添加分组*/
public static final String GROUP_ADD = "v4/sns/group_add";
/**删除分组*/
public static final String GROUP_DELETE = "v4/sns/group_delete";
/**拉取分组*/
public static final String GROUP_GET = "v4/sns/group_get";
}
/**
* 群组管理
*/
public static class GroupManage {
/**创建群组*/
public static final String CREATE_GROUP = "v4/group_open_http_svc/create_group";
/**获取群详细资料*/
public static final String GET_GROUP_INFO = "v4/group_open_http_svc/get_group_info";
/**获取群成员详细资料*/
public static final String GET_GROUP_MEMBER_INFO = "v4/group_open_http_svc/get_group_member_info";
/**修改群基础资料*/
public static final String MODIFY_GROUP_BASE_INFO = "v4/group_open_http_svc/modify_group_base_info";
/**增加群成员*/
public static final String ADD_GROUP_MEMBER = "v4/group_open_http_svc/add_group_member";
/**删除群成员*/
public static final String DELETE_GROUP_MEMBER = "v4/group_open_http_svc/delete_group_member";
/**修改群成员资料*/
public static final String MODIFY_GROUP_MEMBER_INFO = "v4/group_open_http_svc/modify_group_member_info";
/**解散群组*/
public static final String DESTROY_GROUP = "v4/group_open_http_svc/destroy_group";
/**获取用户所加入的群组*/
public static final String GET_JOINED_GROUP_LIST = "v4/group_open_http_svc/get_joined_group_list";
/**查询用户在群组中的身份*/
public static final String GET_ROLE_IN_GROUP = "v4/group_open_http_svc/get_role_in_group";
/**批量禁言和取消禁言*/
public static final String FORBID_SEND_MSG = "v4/group_open_http_svc/forbid_send_msg";
/**获取被禁言群成员列表*/
public static final String GET_GROUP_SHUT_UIN = "v4/group_open_http_svc/get_group_shutted_uin";
/**在群组中发送普通消息*/
public static final String SEND_GROUP_MSG = "v4/group_open_http_svc/send_group_msg";
/**在群组中发送系统通知*/
public static final String SEND_GROUP_SYSTEM_NOTIFICATION = "v4/group_open_http_svc/send_group_system_notification";
/**撤回群消息*/
public static final String GROUP_MSG_RECALL = "v4/group_open_http_svc/group_msg_recall";
/**转让群主*/
public static final String CHANGE_GROUP_OWNER = "v4/group_open_http_svc/change_group_owner";
/**导入群基础资料*/
public static final String IMPORT_GROUP = "v4/group_open_http_svc/import_group";
/**导入群消息*/
public static final String IMPORT_GROUP_MSG = "v4/group_open_http_svc/import_group_msg";
/**导入群成员*/
public static final String IMPORT_GROUP_MEMBER = "v4/group_open_http_svc/import_group_member";
/**设置成员未读消息计数*/
public static final String SET_UNREAD_MSG_NUM = "v4/group_open_http_svc/set_unread_msg_num";
/**删除指定用户发送的消息*/
public static final String DELETE_GROUP_MSG_BY_SENDER = "v4/group_open_http_svc/delete_group_msg_by_sender";
/**拉取群历史消息*/
public static final String GROUP_MSG_GET_SIMPLE = "v4/group_open_http_svc/group_msg_get_simple";
/**获取直播群在线人数*/
public static final String GET_ONLINE_MEMBER_NUM = "v4/group_open_http_svc/get_online_member_num";
}
/**
* 全局禁言管理
*/
public static class AllSinentManage {
/**设置全局禁言*/
public static final String SET_NO_SPEAKING = "v4/openconfigsvr/setnospeaking";
/**查询全局禁言*/
public static final String GET_NO_SPEAKING = "v4/openconfigsvr/getnospeaking";
}
/**
* 运营管理
*/
public static class OperationManage {
/**拉取运营数据*/
public static final String GET_APP_INFO = "v4/openconfigsvr/getappinfo";
/**下载消息记录*/
public static final String GET_HISTORY = "v4/open_msg_svc/get_history";
/**获取服务器 IP 地址*/
public static final String GET_IP_LIST = "v4/ConfigSvc/GetIPList";
}
}
3. 腾讯im相关常量类TencentCloudImConstant.java
/**
* @description: 腾讯im相关常量
* @author: zyb
* @date: 2021/5/31 16:37
*/
public class TencentCloudImConstant {
/**
* IM请求处理结果
*/
public final static String ACTION_STATUS_OK = "OK";
public final static String ACTION_STATUS_FAIL = "FAIL";
/**
* IM发消息是否同步到发送方(1-同步,2-不同步)
*/
public final static Integer SYNC_OTHER_MACHINE_YES = 1;
public final static Integer SYNC_OTHER_MACHINE_NO = 2;
/**
* IM消息对象类型:
* TIMTextElem 文本消息。
* TIMLocationElem 地理位置消息。
* TIMFaceElem 表情消息。
* TIMCustomElem 自定义消息,当接收方为 iOS 系统且应用处在后台时,此消息类型可携带除文本以外的字段到 APNs。一条组合消息中只能包含一个 TIMCustomElem 自定义消息元素。
* TIMSoundElem 语音消息。
* TIMImageElem 图像消息。
* TIMFileElem 文件消息。
* TIMVideoFileElem 视频消息。
*/
public final static String TIM_TEXT_ELEM = "TIMTextElem";
public final static String TIM_LOCATION_ELEM = "TIMLocationElem";
public final static String TIM_FACE_ELEM = "TIMFaceElem";
public final static String TIM_CUSTOM_ELEM = "TIMCustomElem";
public final static String TIM_SOUND_ELEM = "TIMSoundElem";
public final static String TIM_IMAGE_ELEM = "TIMImageElem";
public final static String TIM_FILE_ELEM = "TIMFileElem";
public final static String TIM_VIDEOFILE_ELEM = "TIMVideoFileElem";
/**
* 微信响应消息类型
* WX_MSG_TYPE_EVENT:事件类型,事件类型对应"user_enter_tempsession"表示建立会话
* WX_MSG_TYPE_TEXT:文本类型
* WX_MSG_TYPE_TEXT:图片类型
* WX_MSG_TYPE_TEXT:小程序卡片
*/
public final static String WX_MSG_TYPE_EVENT = "event";
public final static String WX_MSG_TYPE_TEXT = "text";
public final static String WX_MSG_TYPE_IMAGE = "image";
public final static String WX_MSG_TYPE_APPLET_CARD = "miniprogrampage";
}
4. 发送http请求工具类HttpUtil.java
import com.alibaba.fastjson.JSONObject;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class HttpUtil {
private static final CloseableHttpClient httpclient = HttpClients.createDefault();
/**
* 发送HttpGet请求
* @param url
* @return
*/
public static String sendGet(String url) {
HttpGet httpget = new HttpGet(url);
CloseableHttpResponse response = null;
try {
response = httpclient.execute(httpget);
} catch (IOException e1) {
e1.printStackTrace();
}
String result = null;
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
/**
* 发送HttpPost请求,参数为map
* @param url
* @param map
* @return
*/
public static String sendPost(String url, Map<String, String> map) {
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : map.entrySet()) {
formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
HttpPost httppost = new HttpPost(url);
httppost.setEntity(entity);
CloseableHttpResponse response = null;
try {
response = httpclient.execute(httppost);
} catch (IOException e) {
e.printStackTrace();
}
HttpEntity entity1 = response.getEntity();
String result = null;
try {
result = EntityUtils.toString(entity1);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 发送不带参数的HttpPost请求
* @param url
* @return
*/
public static String sendPost(String url) {
HttpPost httppost = new HttpPost(url);
CloseableHttpResponse response = null;
try {
response = httpclient.execute(httppost);
} catch (IOException e) {
e.printStackTrace();
}
HttpEntity entity = response.getEntity();
String result = null;
try {
result = EntityUtils.toString(entity);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static String doPost2(String url, JSONObject param) {
HttpPost httpPost = null;
String result = null;
try {
HttpClient client = new DefaultHttpClient();
httpPost = new HttpPost(url);
if (param != null) {
StringEntity se = new StringEntity(param.toString(), "utf-8");
httpPost.setEntity(se); // post方法中,加入json数据
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("Authorization", param.getString("authorization"));
}
HttpResponse response = client.execute(httpPost);
if (response != null) {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
result = EntityUtils.toString(resEntity, "utf-8");
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}
}
5. 腾讯im常用方法工具类TencentCloudImUtil.java
注:这里把userSig存到了redis缓存中,redis相关配置过程可参考另一篇文章,链接如下:
//www.greatytc.com/p/2ddeecd74e2e
import com.alibaba.fastjson.JSONObject;
import com.example.tencentcloudim.constant.TencentCloudImApiConstant;
import com.example.tencentcloudim.constant.TencentCloudImConstant;
import com.tencentyun.TLSSigAPIv2;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* @description: 腾讯im基础配置
* @author: zyb
* @date: 2021/5/27 17:32
*/
@Slf4j
@Component
public class TencentCloudImUtil {
private static final String HTTPS_URL_PREFIX = "https://console.tim.qq.com/";
private static final String APP_MANAGER = "administrator";
private static final String REDIS_IM_USER_SIG = "silence:im_user_sig:";
@Value("${silence.tencent.cloud.im.sdkAppId}")
private long sdkAppId;
@Value("${silence.tencent.cloud.im.key}")
private String key;
@Autowired
private RedisServiceUtil redisServiceUtil;
/**
* 获取腾讯云用户签名
*/
public String getTxCloudUserSig() {
String userSig = redisServiceUtil.get(REDIS_IM_USER_SIG + APP_MANAGER);
if (StringUtils.isEmpty(userSig)) {
TLSSigAPIv2 tlsSigApi = new TLSSigAPIv2(sdkAppId, key);
userSig = tlsSigApi.genUserSig(APP_MANAGER, 86400);
redisServiceUtil.set(REDIS_IM_USER_SIG + APP_MANAGER, userSig, 86400L);
}
return userSig;
}
/**
* 获取腾讯im请求路径
*/
private String getHttpsUrl(String imServiceApi, Integer random) {
return String.format("%s%s?sdkappid=%s&identifier=%s&usersig=%s&random=%s&contenttype=json",
HTTPS_URL_PREFIX, imServiceApi, sdkAppId, APP_MANAGER, this.getTxCloudUserSig(), random);
}
/**
* 导入单个账号
* @param userId 用户id
*/
public void accountImport(String userId) {
accountImport(userId, null);
}
public void accountImport(String userId, String userName) {
accountImport(userId, userName, null);
}
public void accountImport(String userId, String userName, String faceUrl) {
Integer random = RandomUtils.nextInt(0, 999999999);
String httpsUrl = getHttpsUrl(TencentCloudImApiConstant.AccountManage.ACCOUNT_IMPORT, random);
JSONObject jsonObject = new JSONObject();
jsonObject.put("Identifier", userId);
if (StringUtils.isNotEmpty(userName)) {
jsonObject.put("Nick", userName);
}
if (StringUtils.isNotEmpty(faceUrl)) {
jsonObject.put("FaceUrl", faceUrl);
}
log.info("腾讯云im导入单个账号,请求参数:{}", jsonObject.toString());
String result = HttpUtil.doPost2(httpsUrl, jsonObject);
log.info("腾讯云im导入单个账号,返回结果:{}", result);
}
/**
* 导入多个账号
* @param userIds 用户id集合
*/
public void multiAccountImport(List<String> userIds) {
Integer random = RandomUtils.nextInt(0, 999999999);
String httpsUrl = getHttpsUrl(TencentCloudImApiConstant.AccountManage.MULTI_ACCOUNT_IMPORT, random);
JSONObject jsonObject = new JSONObject();
jsonObject.put("Accounts", userIds);
log.info("腾讯云im导入多个账号,请求参数:{}", jsonObject.toString());
String result = HttpUtil.doPost2(httpsUrl, jsonObject);
log.info("腾讯云im导入单个账户,返回结果:{}", result);
}
/**
* 删除账号
* @param userIds 用户id集合
*/
public void accountDelete(List<String> userIds) {
Integer random = RandomUtils.nextInt(0, 999999999);
String httpsUrl = getHttpsUrl(TencentCloudImApiConstant.AccountManage.ACCOUNT_DELETE, random);
JSONObject jsonObject = new JSONObject();
jsonObject.put("DeleteItem", getUserIdJsonList(userIds));
log.info("腾讯云im删除账号,请求参数:{}", jsonObject.toString());
String result = HttpUtil.doPost2(httpsUrl, jsonObject);
log.info("腾讯云im删除账户,返回结果:{}", result);
}
/**
* 查询账号是否已经导入im
* @param userIds 用户id集合
*/
public String accountCheck(List<String> userIds) {
Integer random = RandomUtils.nextInt(0, 999999999);
String httpsUrl = getHttpsUrl(TencentCloudImApiConstant.AccountManage.ACCOUNT_CHECK, random);
JSONObject jsonObject = new JSONObject();
jsonObject.put("CheckItem", getUserIdJsonList(userIds));
log.info("腾讯云im查询账号,请求参数:{}", jsonObject.toString());
String result = HttpUtil.doPost2(httpsUrl, jsonObject);
log.info("腾讯云im查询账号,返回结果:{}", result);
return result;
}
private List<JSONObject> getUserIdJsonList(List<String> userIds) {
return userIds.stream().map(v -> {
JSONObject userIdJson = new JSONObject();
userIdJson.put("UserID", v);
return userIdJson;
}).collect(Collectors.toList());
}
/**
* 单发单聊消息
* @param syncOtherMachine 是否同步消息到发送方(1-同步,2-不同步)
* @param fromUserId 发送方用户id
* @param toUserId 接收方用户id
* @param msgType 消息对象类型
* @param msgContent 消息内容
*/
public String sendMsg(Integer syncOtherMachine, String fromUserId, String toUserId, String msgType, String msgContent) {
Integer random = RandomUtils.nextInt(0, 999999999);
String httpsUrl = getHttpsUrl(TencentCloudImApiConstant.SingleChatManage.SEND_MSG, random);
JSONObject jsonObject = new JSONObject();
jsonObject.put("SyncOtherMachine", syncOtherMachine);
if (StringUtils.isNotEmpty(fromUserId)) {
// 发送方不为空表示指定发送用户,为空表示为管理员发送消息
jsonObject.put("From_Account", fromUserId);
}
jsonObject.put("To_Account", toUserId);
jsonObject.put("MsgRandom", random);
List<JSONObject> msgBody = getMsgBody(msgType, msgContent);
jsonObject.put("MsgBody", msgBody);
log.info("腾讯云im单发单聊消息,请求参数:{}", jsonObject.toString());
String result = HttpUtil.doPost2(httpsUrl, jsonObject);
log.info("腾讯云im单发单聊消息,返回结果:{}", result);
return result;
}
/**
* 批量发单聊消息
* @param syncOtherMachine 是否同步消息到发送方(1-同步,2-不同步)
* @param fromUserId 发送方用户id
* @param toUserIds 接收方用户id集合
* @param msgType 消息对象类型
* @param msgContent 消息内容
*/
public String batchSendMsg(Integer syncOtherMachine, String fromUserId, List<String> toUserIds, String msgType, String msgContent) {
Integer random = RandomUtils.nextInt(0, 999999999);
String httpsUrl = getHttpsUrl(TencentCloudImApiConstant.SingleChatManage.BATCH_SEND_MSG, random);
JSONObject jsonObject = new JSONObject();
jsonObject.put("SyncOtherMachine", syncOtherMachine);
if (StringUtils.isNotEmpty(fromUserId)) {
// 发送方不为空表示指定发送用户,为空表示为管理员发送消息
jsonObject.put("From_Account", fromUserId);
}
jsonObject.put("To_Account", toUserIds);
jsonObject.put("MsgRandom", random);
List<JSONObject> msgBody = getMsgBody(msgType, msgContent);
jsonObject.put("MsgBody", msgBody);
log.info("腾讯云im批量发单聊消息,请求参数:{}", jsonObject.toString());
String result = HttpUtil.doPost2(httpsUrl, jsonObject);
log.info("腾讯云im批量发单聊消息,返回结果:{}", result);
return result;
}
/**
* 拼接发送消息内容
* @param msgType 消息类型
* @param msgContent 发送消息内容
* @return 消息内容
*/
private List<JSONObject> getMsgBody(String msgType, String msgContent) {
List<JSONObject> msgBody = new ArrayList<>();
if (msgType.equals(TencentCloudImConstant.TIM_TEXT_ELEM)) {
// 文本类型
JSONObject msgBodyJson = new JSONObject();
msgBodyJson.put("MsgType", msgType);
JSONObject msgContentObj = new JSONObject();
msgContentObj.put("Text", msgContent);
msgBodyJson.put("MsgContent", msgContentObj);
msgBody.add(msgBodyJson);
}
return msgBody;
}
/**
* 查询单聊消息
* @param fromUserId 发送方用户id
* @param toUserId 接收方用户id
* @param maxCnt 查询条数
* @param startTime 起始时间(单位:秒)
* @param endTime 结束时间(单位:秒)
* @param lastMsgKey 最后一条消息的 MsgKey
* @return 单聊消息列表
*/
public String adminGetRoamMsg(String fromUserId, String toUserId, Integer maxCnt, Long startTime, Long endTime, String lastMsgKey) {
Integer random = RandomUtils.nextInt(0, 999999999);
String httpsUrl = getHttpsUrl(TencentCloudImApiConstant.SingleChatManage.ADMIN_GET_ROAM_MSG, random);
JSONObject jsonObject = new JSONObject();
jsonObject.put("From_Account", fromUserId);
jsonObject.put("To_Account", toUserId);
jsonObject.put("MaxCnt", maxCnt);
jsonObject.put("MinTime", startTime);
jsonObject.put("MaxTime", endTime);
if (StringUtils.isNotEmpty(lastMsgKey)){
jsonObject.put("LastMsgKey", lastMsgKey);
}
log.info("腾讯云im查询单聊消息,请求参数:{}", jsonObject.toString());
String result = HttpUtil.doPost2(httpsUrl, jsonObject);
log.info("腾讯云im查询单聊消息,返回结果:{}", result);
return result;
}
/**
* 撤回单聊消息
* @param fromUserId 发送方用户id
* @param toUserId 接收方用户id
* @param msgKey MsgKey
*/
public void adminMsgWithDraw(String fromUserId, String toUserId, String msgKey) {
Integer random = RandomUtils.nextInt(0, 999999999);
String httpsUrl = getHttpsUrl(TencentCloudImApiConstant.SingleChatManage.ADMIN_MSG_WITH_DRAW, random);
JSONObject jsonObject = new JSONObject();
jsonObject.put("From_Account", fromUserId);
jsonObject.put("To_Account", toUserId);
jsonObject.put("MsgKey", msgKey);
log.info("腾讯云im撤回单聊消息,请求参数:{}", jsonObject.toString());
String result = HttpUtil.doPost2(httpsUrl, jsonObject);
log.info("腾讯云im撤回单聊消息,返回结果:{}", result);
}
/**
* 设置单聊消息已读
* @param reportUserId 读取消息的用户
* @param peerUserId 发送消息的用户
*/
public void adminSetMsgRead(String reportUserId, String peerUserId) {
Integer random = RandomUtils.nextInt(0, 999999999);
String httpsUrl = getHttpsUrl(TencentCloudImApiConstant.SingleChatManage.ADMIN_SET_MSG_READ, random);
JSONObject jsonObject = new JSONObject();
jsonObject.put("Report_Account", reportUserId);
jsonObject.put("Peer_Account", peerUserId);
log.info("腾讯云im设置单聊消息已读,请求参数:{}", jsonObject.toString());
String result = HttpUtil.doPost2(httpsUrl, jsonObject);
log.info("腾讯云im设置单聊消息已读,返回结果:{}", result);
}
}
注:完整代码可通过一下GitHub链接获取(代码中包含redis的相关配置,以及相关测试类)
https://github.com/ZYBSilence/tencent-cloud-im