Skip to content

Commit

Permalink
feat: add message api
Browse files Browse the repository at this point in the history
  • Loading branch information
hongyiheng committed Jan 9, 2024
1 parent 2cc0426 commit 36a7e94
Show file tree
Hide file tree
Showing 4 changed files with 318 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/main/java/io/github/doocs/im/core/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class Message {

public static final String SERVICE_NAME_MSG_EXT = "openim_msg_ext_http_svc";

public static final String SERVICE_NAME_OFFICIAL_ACCOUNT = "official_account_open_http_svc";

/**
* 单聊消息相关命令字
*/
Expand All @@ -34,6 +36,7 @@ public class Message {
public static final String MODIFY_C2C_MSG_COMMAND = "modify_c2c_msg";
public static final String GET_KEY_VALUES_COMMAND = "get_key_values";
public static final String SET_KEY_VALUES_COMMAND = "set_key_values";
public static final String SEND_OFFICIAL_ACCOUNT_MSG_COMMAND = "send_official_account_msg";

private final ImClient imClient;

Expand Down Expand Up @@ -210,4 +213,21 @@ public SetKeyValuesResult setKeyValues(SetKeyValuesRequest setKeyValuesRequest,
String url = imClient.getUrl(SERVICE_NAME_MSG_EXT, SET_KEY_VALUES_COMMAND, random);
return HttpUtil.post(url, setKeyValuesRequest, SetKeyValuesResult.class, imClient.getConfig());
}

/**
* 公众号用户发送广播消息
*
* @param sendOfficialAccountMsgRequest 请求参数
* @return 结果
* @throws IOException 异常
*/
public SendOfficialAccountMsgResult sendOfficialAccountMsg(SendOfficialAccountMsgRequest sendOfficialAccountMsgRequest) throws IOException {
String url = imClient.getUrl(SERVICE_NAME_OFFICIAL_ACCOUNT, SEND_OFFICIAL_ACCOUNT_MSG_COMMAND);
return HttpUtil.post(url, sendOfficialAccountMsgRequest, SendOfficialAccountMsgResult.class, imClient.getConfig());
}

public SendOfficialAccountMsgResult sendOfficialAccountMsg(SendOfficialAccountMsgRequest sendOfficialAccountMsgRequest, long random) throws IOException {
String url = imClient.getUrl(SERVICE_NAME_OFFICIAL_ACCOUNT, SEND_OFFICIAL_ACCOUNT_MSG_COMMAND, random);
return HttpUtil.post(url, sendOfficialAccountMsgRequest, SendOfficialAccountMsgResult.class, imClient.getConfig());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
package io.github.doocs.im.model.request;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.github.doocs.im.model.message.TIMMsgElement;

import java.io.Serializable;
import java.util.List;

/**
* 公众号用户发送广播消息-请求参数
*
* @author hyh
* @since 2024/01/09 16:22
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public class SendOfficialAccountMsgRequest extends GenericRequest implements Serializable {
private static final long serialVersionUID = -511076816330563359L;


/**
* 发送消息的公众号用户。
*/
@JsonProperty("Official_Account")
private String officialAccount;

/**
* 无符号32位整数(取值范围:0 - 4294967295)。
* 如果5分钟内两条消息的内容和 Random 随机值都相同的情况下,后一条消息将被当做重复消息而丢弃。
*/
@JsonProperty("Random")
private Integer random;

/**
* 消息体,详细可参阅 消息格式描述。
*/
@JsonProperty("MsgBody")
private List<TIMMsgElement> msgBody;

/**
* 离线推送信息配置,详细可参见 消息格式描述。
*/
@JsonProperty("OfflinePushInfo")
private OfflinePushInfo offlinePushInfo;

/**
* 消息回调禁止开关,只对单条消息有效
* ForbidBeforeSendMsgCallback 表示禁止发消息前回调
* ForbidAfterSendMsgCallback 表示禁止发消息后回调
*/
@JsonProperty("ForbidCallbackControl")
private List<String> forbidCallbackControl;

/**
* 1表示消息仅发送在线订阅者,默认0表示发送所有订阅者
*/
@JsonProperty("OnlineOnlyFlag")
private Integer onlineOnlyFlag;

/**
* 消息发送权限,NoLastMsg 只对单条消息有效,表示不更新最近联系人会话
* (如果该消息 OnlineOnlyFlag 设置为1,则不允许使用该字段。)
*/
@JsonProperty("SendMsgControl")
private List<String> sendMsgControl;

/**
* 消息自定义数据(云端保存,会发送到对端,程序卸载重装后还能拉取到)
*/
@JsonProperty("CloudCustomData")
private String cloudCustomData;


public SendOfficialAccountMsgRequest() {
}

public SendOfficialAccountMsgRequest(String officialAccount, Integer random, List<TIMMsgElement> msgBody) {
this.officialAccount = officialAccount;
this.random = random;
this.msgBody = msgBody;
}

public SendOfficialAccountMsgRequest(String officialAccount, Integer random, List<TIMMsgElement> msgBody, OfflinePushInfo offlinePushInfo, List<String> forbidCallbackControl, Integer onlineOnlyFlag, List<String> sendMsgControl, String cloudCustomData) {
this.officialAccount = officialAccount;
this.random = random;
this.msgBody = msgBody;
this.offlinePushInfo = offlinePushInfo;
this.forbidCallbackControl = forbidCallbackControl;
this.onlineOnlyFlag = onlineOnlyFlag;
this.sendMsgControl = sendMsgControl;
this.cloudCustomData = cloudCustomData;
}

private SendOfficialAccountMsgRequest(Builder builder) {
this.officialAccount = builder.officialAccount;
this.random = builder.random;
this.msgBody = builder.msgBody;
this.offlinePushInfo = builder.offlinePushInfo;
this.forbidCallbackControl = builder.forbidCallbackControl;
this.onlineOnlyFlag = builder.onlineOnlyFlag;
this.sendMsgControl = builder.sendMsgControl;
this.cloudCustomData = builder.cloudCustomData;
}

public static Builder builder() {
return new Builder();
}


public static final class Builder {
private String officialAccount;
private Integer random;
private List<TIMMsgElement> msgBody;
private OfflinePushInfo offlinePushInfo;
private List<String> forbidCallbackControl;
private Integer onlineOnlyFlag;
private List<String> sendMsgControl;
private String cloudCustomData;

private Builder() {
}

public SendOfficialAccountMsgRequest build() {
return new SendOfficialAccountMsgRequest(this);
}

public Builder officialAccount(String officialAccount) {
this.officialAccount = officialAccount;
return this;
}

public Builder random(Integer random) {
this.random = random;
return this;
}

public Builder msgBody(List<TIMMsgElement> msgBody) {
this.msgBody = msgBody;
return this;
}

public Builder offlinePushInfo(OfflinePushInfo offlinePushInfo) {
this.offlinePushInfo = offlinePushInfo;
return this;
}

public Builder forbidCallbackControl(List<String> forbidCallbackControl) {
this.forbidCallbackControl = forbidCallbackControl;
return this;
}

public Builder onlineOnlyFlag(Integer onlineOnlyFlag) {
this.onlineOnlyFlag = onlineOnlyFlag;
return this;
}

public Builder sendMsgControl(List<String> sendMsgControl) {
this.sendMsgControl = sendMsgControl;
return this;
}

public Builder cloudCustomData(String cloudCustomData) {
this.cloudCustomData = cloudCustomData;
return this;
}
}

public String getOfficialAccount() {
return officialAccount;
}

public void setOfficialAccount(String officialAccount) {
this.officialAccount = officialAccount;
}

public Integer getRandom() {
return random;
}

public void setRandom(Integer random) {
this.random = random;
}

public List<TIMMsgElement> getMsgBody() {
return msgBody;
}

public void setMsgBody(List<TIMMsgElement> msgBody) {
this.msgBody = msgBody;
}

public OfflinePushInfo getOfflinePushInfo() {
return offlinePushInfo;
}

public void setOfflinePushInfo(OfflinePushInfo offlinePushInfo) {
this.offlinePushInfo = offlinePushInfo;
}

public List<String> getForbidCallbackControl() {
return forbidCallbackControl;
}

public void setForbidCallbackControl(List<String> forbidCallbackControl) {
this.forbidCallbackControl = forbidCallbackControl;
}

public Integer getOnlineOnlyFlag() {
return onlineOnlyFlag;
}

public void setOnlineOnlyFlag(Integer onlineOnlyFlag) {
this.onlineOnlyFlag = onlineOnlyFlag;
}

public List<String> getSendMsgControl() {
return sendMsgControl;
}

public void setSendMsgControl(List<String> sendMsgControl) {
this.sendMsgControl = sendMsgControl;
}

public String getCloudCustomData() {
return cloudCustomData;
}

public void setCloudCustomData(String cloudCustomData) {
this.cloudCustomData = cloudCustomData;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.github.doocs.im.model.response;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.io.Serializable;

/**
* 公众号用户发送广播消息-结果
*
* @author hyh
* @since 2024/01/09 19:39
*/
public class SendOfficialAccountMsgResult extends GenericResult implements Serializable {
private static final long serialVersionUID = 4691540073143009818L;
/**
* 消息发送的时间戳,对应后台 server 时间
*/
@JsonProperty("MsgTime")
private Integer msgTime;

/**
* 消息唯一标识,用于撤回。长度不超过50个字符
*/
@JsonProperty("MsgKey")
private String msgKey;

public Integer getMsgTime() {
return msgTime;
}

public void setMsgTime(Integer msgTime) {
this.msgTime = msgTime;
}

public String getMsgKey() {
return msgKey;
}

public void setMsgKey(String msgKey) {
this.msgKey = msgKey;
}

@Override
public String toString() {
return "SendOfficialAccountMsgResult{" +
"msgTime=" + msgTime +
", msgKey='" + msgKey + '\'' +
", actionStatus='" + actionStatus + '\'' +
", errorInfo='" + errorInfo + '\'' +
", errorCode=" + errorCode +
'}';
}
}
14 changes: 14 additions & 0 deletions src/test/java/io/github/doocs/im/core/MessageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,18 @@ void testSetKeyValues() throws IOException {
System.out.println(result);
Assertions.assertEquals(ActionStatus.OK, result.getActionStatus());
}

@Test
void testSendOfficialAccountMsg() throws IOException {
TIMTextMsgElement msg = new TIMTextMsgElement("hello world");
List<TIMMsgElement> msgBody = Collections.singletonList(msg);
SendOfficialAccountMsgRequest request = SendOfficialAccountMsgRequest.builder()
.officialAccount("bingo")
.random(123)
.msgBody(msgBody)
.build();
SendOfficialAccountMsgResult result = client.message.sendOfficialAccountMsg(request);
System.out.println(result);
Assertions.assertEquals(ActionStatus.OK, result.getActionStatus());
}
}

0 comments on commit 36a7e94

Please sign in to comment.