From 36a7e94402776b9805164d6273e7a0b7e3c939c6 Mon Sep 17 00:00:00 2001 From: hongyiheng Date: Tue, 9 Jan 2024 19:55:57 +0800 Subject: [PATCH] feat: add message api --- .../java/io/github/doocs/im/core/Message.java | 20 ++ .../SendOfficialAccountMsgRequest.java | 231 ++++++++++++++++++ .../SendOfficialAccountMsgResult.java | 53 ++++ .../io/github/doocs/im/core/MessageTest.java | 14 ++ 4 files changed, 318 insertions(+) create mode 100644 src/main/java/io/github/doocs/im/model/request/SendOfficialAccountMsgRequest.java create mode 100644 src/main/java/io/github/doocs/im/model/response/SendOfficialAccountMsgResult.java diff --git a/src/main/java/io/github/doocs/im/core/Message.java b/src/main/java/io/github/doocs/im/core/Message.java index ae5f3c1..ac3b476 100644 --- a/src/main/java/io/github/doocs/im/core/Message.java +++ b/src/main/java/io/github/doocs/im/core/Message.java @@ -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"; + /** * 单聊消息相关命令字 */ @@ -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; @@ -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()); + } } diff --git a/src/main/java/io/github/doocs/im/model/request/SendOfficialAccountMsgRequest.java b/src/main/java/io/github/doocs/im/model/request/SendOfficialAccountMsgRequest.java new file mode 100644 index 0000000..0e0d682 --- /dev/null +++ b/src/main/java/io/github/doocs/im/model/request/SendOfficialAccountMsgRequest.java @@ -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 msgBody; + + /** + * 离线推送信息配置,详细可参见 消息格式描述。 + */ + @JsonProperty("OfflinePushInfo") + private OfflinePushInfo offlinePushInfo; + + /** + * 消息回调禁止开关,只对单条消息有效 + * ForbidBeforeSendMsgCallback 表示禁止发消息前回调 + * ForbidAfterSendMsgCallback 表示禁止发消息后回调 + */ + @JsonProperty("ForbidCallbackControl") + private List forbidCallbackControl; + + /** + * 1表示消息仅发送在线订阅者,默认0表示发送所有订阅者 + */ + @JsonProperty("OnlineOnlyFlag") + private Integer onlineOnlyFlag; + + /** + * 消息发送权限,NoLastMsg 只对单条消息有效,表示不更新最近联系人会话 + * (如果该消息 OnlineOnlyFlag 设置为1,则不允许使用该字段。) + */ + @JsonProperty("SendMsgControl") + private List sendMsgControl; + + /** + * 消息自定义数据(云端保存,会发送到对端,程序卸载重装后还能拉取到) + */ + @JsonProperty("CloudCustomData") + private String cloudCustomData; + + + public SendOfficialAccountMsgRequest() { + } + + public SendOfficialAccountMsgRequest(String officialAccount, Integer random, List msgBody) { + this.officialAccount = officialAccount; + this.random = random; + this.msgBody = msgBody; + } + + public SendOfficialAccountMsgRequest(String officialAccount, Integer random, List msgBody, OfflinePushInfo offlinePushInfo, List forbidCallbackControl, Integer onlineOnlyFlag, List 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 msgBody; + private OfflinePushInfo offlinePushInfo; + private List forbidCallbackControl; + private Integer onlineOnlyFlag; + private List 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 msgBody) { + this.msgBody = msgBody; + return this; + } + + public Builder offlinePushInfo(OfflinePushInfo offlinePushInfo) { + this.offlinePushInfo = offlinePushInfo; + return this; + } + + public Builder forbidCallbackControl(List forbidCallbackControl) { + this.forbidCallbackControl = forbidCallbackControl; + return this; + } + + public Builder onlineOnlyFlag(Integer onlineOnlyFlag) { + this.onlineOnlyFlag = onlineOnlyFlag; + return this; + } + + public Builder sendMsgControl(List 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 getMsgBody() { + return msgBody; + } + + public void setMsgBody(List msgBody) { + this.msgBody = msgBody; + } + + public OfflinePushInfo getOfflinePushInfo() { + return offlinePushInfo; + } + + public void setOfflinePushInfo(OfflinePushInfo offlinePushInfo) { + this.offlinePushInfo = offlinePushInfo; + } + + public List getForbidCallbackControl() { + return forbidCallbackControl; + } + + public void setForbidCallbackControl(List forbidCallbackControl) { + this.forbidCallbackControl = forbidCallbackControl; + } + + public Integer getOnlineOnlyFlag() { + return onlineOnlyFlag; + } + + public void setOnlineOnlyFlag(Integer onlineOnlyFlag) { + this.onlineOnlyFlag = onlineOnlyFlag; + } + + public List getSendMsgControl() { + return sendMsgControl; + } + + public void setSendMsgControl(List sendMsgControl) { + this.sendMsgControl = sendMsgControl; + } + + public String getCloudCustomData() { + return cloudCustomData; + } + + public void setCloudCustomData(String cloudCustomData) { + this.cloudCustomData = cloudCustomData; + } +} diff --git a/src/main/java/io/github/doocs/im/model/response/SendOfficialAccountMsgResult.java b/src/main/java/io/github/doocs/im/model/response/SendOfficialAccountMsgResult.java new file mode 100644 index 0000000..61faa43 --- /dev/null +++ b/src/main/java/io/github/doocs/im/model/response/SendOfficialAccountMsgResult.java @@ -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 + + '}'; + } +} diff --git a/src/test/java/io/github/doocs/im/core/MessageTest.java b/src/test/java/io/github/doocs/im/core/MessageTest.java index af90287..72f2918 100644 --- a/src/test/java/io/github/doocs/im/core/MessageTest.java +++ b/src/test/java/io/github/doocs/im/core/MessageTest.java @@ -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 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()); + } }