Skip to content

Commit 36a7e94

Browse files
committed
feat: add message api
1 parent 2cc0426 commit 36a7e94

File tree

4 files changed

+318
-0
lines changed

4 files changed

+318
-0
lines changed

src/main/java/io/github/doocs/im/core/Message.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public class Message {
2121

2222
public static final String SERVICE_NAME_MSG_EXT = "openim_msg_ext_http_svc";
2323

24+
public static final String SERVICE_NAME_OFFICIAL_ACCOUNT = "official_account_open_http_svc";
25+
2426
/**
2527
* 单聊消息相关命令字
2628
*/
@@ -34,6 +36,7 @@ public class Message {
3436
public static final String MODIFY_C2C_MSG_COMMAND = "modify_c2c_msg";
3537
public static final String GET_KEY_VALUES_COMMAND = "get_key_values";
3638
public static final String SET_KEY_VALUES_COMMAND = "set_key_values";
39+
public static final String SEND_OFFICIAL_ACCOUNT_MSG_COMMAND = "send_official_account_msg";
3740

3841
private final ImClient imClient;
3942

@@ -210,4 +213,21 @@ public SetKeyValuesResult setKeyValues(SetKeyValuesRequest setKeyValuesRequest,
210213
String url = imClient.getUrl(SERVICE_NAME_MSG_EXT, SET_KEY_VALUES_COMMAND, random);
211214
return HttpUtil.post(url, setKeyValuesRequest, SetKeyValuesResult.class, imClient.getConfig());
212215
}
216+
217+
/**
218+
* 公众号用户发送广播消息
219+
*
220+
* @param sendOfficialAccountMsgRequest 请求参数
221+
* @return 结果
222+
* @throws IOException 异常
223+
*/
224+
public SendOfficialAccountMsgResult sendOfficialAccountMsg(SendOfficialAccountMsgRequest sendOfficialAccountMsgRequest) throws IOException {
225+
String url = imClient.getUrl(SERVICE_NAME_OFFICIAL_ACCOUNT, SEND_OFFICIAL_ACCOUNT_MSG_COMMAND);
226+
return HttpUtil.post(url, sendOfficialAccountMsgRequest, SendOfficialAccountMsgResult.class, imClient.getConfig());
227+
}
228+
229+
public SendOfficialAccountMsgResult sendOfficialAccountMsg(SendOfficialAccountMsgRequest sendOfficialAccountMsgRequest, long random) throws IOException {
230+
String url = imClient.getUrl(SERVICE_NAME_OFFICIAL_ACCOUNT, SEND_OFFICIAL_ACCOUNT_MSG_COMMAND, random);
231+
return HttpUtil.post(url, sendOfficialAccountMsgRequest, SendOfficialAccountMsgResult.class, imClient.getConfig());
232+
}
213233
}
Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
package io.github.doocs.im.model.request;
2+
3+
import com.fasterxml.jackson.annotation.JsonInclude;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import io.github.doocs.im.model.message.TIMMsgElement;
6+
7+
import java.io.Serializable;
8+
import java.util.List;
9+
10+
/**
11+
* 公众号用户发送广播消息-请求参数
12+
*
13+
* @author hyh
14+
* @since 2024/01/09 16:22
15+
*/
16+
@JsonInclude(JsonInclude.Include.NON_NULL)
17+
public class SendOfficialAccountMsgRequest extends GenericRequest implements Serializable {
18+
private static final long serialVersionUID = -511076816330563359L;
19+
20+
21+
/**
22+
* 发送消息的公众号用户。
23+
*/
24+
@JsonProperty("Official_Account")
25+
private String officialAccount;
26+
27+
/**
28+
* 无符号32位整数(取值范围:0 - 4294967295)。
29+
* 如果5分钟内两条消息的内容和 Random 随机值都相同的情况下,后一条消息将被当做重复消息而丢弃。
30+
*/
31+
@JsonProperty("Random")
32+
private Integer random;
33+
34+
/**
35+
* 消息体,详细可参阅 消息格式描述。
36+
*/
37+
@JsonProperty("MsgBody")
38+
private List<TIMMsgElement> msgBody;
39+
40+
/**
41+
* 离线推送信息配置,详细可参见 消息格式描述。
42+
*/
43+
@JsonProperty("OfflinePushInfo")
44+
private OfflinePushInfo offlinePushInfo;
45+
46+
/**
47+
* 消息回调禁止开关,只对单条消息有效
48+
* ForbidBeforeSendMsgCallback 表示禁止发消息前回调
49+
* ForbidAfterSendMsgCallback 表示禁止发消息后回调
50+
*/
51+
@JsonProperty("ForbidCallbackControl")
52+
private List<String> forbidCallbackControl;
53+
54+
/**
55+
* 1表示消息仅发送在线订阅者,默认0表示发送所有订阅者
56+
*/
57+
@JsonProperty("OnlineOnlyFlag")
58+
private Integer onlineOnlyFlag;
59+
60+
/**
61+
* 消息发送权限,NoLastMsg 只对单条消息有效,表示不更新最近联系人会话
62+
* (如果该消息 OnlineOnlyFlag 设置为1,则不允许使用该字段。)
63+
*/
64+
@JsonProperty("SendMsgControl")
65+
private List<String> sendMsgControl;
66+
67+
/**
68+
* 消息自定义数据(云端保存,会发送到对端,程序卸载重装后还能拉取到)
69+
*/
70+
@JsonProperty("CloudCustomData")
71+
private String cloudCustomData;
72+
73+
74+
public SendOfficialAccountMsgRequest() {
75+
}
76+
77+
public SendOfficialAccountMsgRequest(String officialAccount, Integer random, List<TIMMsgElement> msgBody) {
78+
this.officialAccount = officialAccount;
79+
this.random = random;
80+
this.msgBody = msgBody;
81+
}
82+
83+
public SendOfficialAccountMsgRequest(String officialAccount, Integer random, List<TIMMsgElement> msgBody, OfflinePushInfo offlinePushInfo, List<String> forbidCallbackControl, Integer onlineOnlyFlag, List<String> sendMsgControl, String cloudCustomData) {
84+
this.officialAccount = officialAccount;
85+
this.random = random;
86+
this.msgBody = msgBody;
87+
this.offlinePushInfo = offlinePushInfo;
88+
this.forbidCallbackControl = forbidCallbackControl;
89+
this.onlineOnlyFlag = onlineOnlyFlag;
90+
this.sendMsgControl = sendMsgControl;
91+
this.cloudCustomData = cloudCustomData;
92+
}
93+
94+
private SendOfficialAccountMsgRequest(Builder builder) {
95+
this.officialAccount = builder.officialAccount;
96+
this.random = builder.random;
97+
this.msgBody = builder.msgBody;
98+
this.offlinePushInfo = builder.offlinePushInfo;
99+
this.forbidCallbackControl = builder.forbidCallbackControl;
100+
this.onlineOnlyFlag = builder.onlineOnlyFlag;
101+
this.sendMsgControl = builder.sendMsgControl;
102+
this.cloudCustomData = builder.cloudCustomData;
103+
}
104+
105+
public static Builder builder() {
106+
return new Builder();
107+
}
108+
109+
110+
public static final class Builder {
111+
private String officialAccount;
112+
private Integer random;
113+
private List<TIMMsgElement> msgBody;
114+
private OfflinePushInfo offlinePushInfo;
115+
private List<String> forbidCallbackControl;
116+
private Integer onlineOnlyFlag;
117+
private List<String> sendMsgControl;
118+
private String cloudCustomData;
119+
120+
private Builder() {
121+
}
122+
123+
public SendOfficialAccountMsgRequest build() {
124+
return new SendOfficialAccountMsgRequest(this);
125+
}
126+
127+
public Builder officialAccount(String officialAccount) {
128+
this.officialAccount = officialAccount;
129+
return this;
130+
}
131+
132+
public Builder random(Integer random) {
133+
this.random = random;
134+
return this;
135+
}
136+
137+
public Builder msgBody(List<TIMMsgElement> msgBody) {
138+
this.msgBody = msgBody;
139+
return this;
140+
}
141+
142+
public Builder offlinePushInfo(OfflinePushInfo offlinePushInfo) {
143+
this.offlinePushInfo = offlinePushInfo;
144+
return this;
145+
}
146+
147+
public Builder forbidCallbackControl(List<String> forbidCallbackControl) {
148+
this.forbidCallbackControl = forbidCallbackControl;
149+
return this;
150+
}
151+
152+
public Builder onlineOnlyFlag(Integer onlineOnlyFlag) {
153+
this.onlineOnlyFlag = onlineOnlyFlag;
154+
return this;
155+
}
156+
157+
public Builder sendMsgControl(List<String> sendMsgControl) {
158+
this.sendMsgControl = sendMsgControl;
159+
return this;
160+
}
161+
162+
public Builder cloudCustomData(String cloudCustomData) {
163+
this.cloudCustomData = cloudCustomData;
164+
return this;
165+
}
166+
}
167+
168+
public String getOfficialAccount() {
169+
return officialAccount;
170+
}
171+
172+
public void setOfficialAccount(String officialAccount) {
173+
this.officialAccount = officialAccount;
174+
}
175+
176+
public Integer getRandom() {
177+
return random;
178+
}
179+
180+
public void setRandom(Integer random) {
181+
this.random = random;
182+
}
183+
184+
public List<TIMMsgElement> getMsgBody() {
185+
return msgBody;
186+
}
187+
188+
public void setMsgBody(List<TIMMsgElement> msgBody) {
189+
this.msgBody = msgBody;
190+
}
191+
192+
public OfflinePushInfo getOfflinePushInfo() {
193+
return offlinePushInfo;
194+
}
195+
196+
public void setOfflinePushInfo(OfflinePushInfo offlinePushInfo) {
197+
this.offlinePushInfo = offlinePushInfo;
198+
}
199+
200+
public List<String> getForbidCallbackControl() {
201+
return forbidCallbackControl;
202+
}
203+
204+
public void setForbidCallbackControl(List<String> forbidCallbackControl) {
205+
this.forbidCallbackControl = forbidCallbackControl;
206+
}
207+
208+
public Integer getOnlineOnlyFlag() {
209+
return onlineOnlyFlag;
210+
}
211+
212+
public void setOnlineOnlyFlag(Integer onlineOnlyFlag) {
213+
this.onlineOnlyFlag = onlineOnlyFlag;
214+
}
215+
216+
public List<String> getSendMsgControl() {
217+
return sendMsgControl;
218+
}
219+
220+
public void setSendMsgControl(List<String> sendMsgControl) {
221+
this.sendMsgControl = sendMsgControl;
222+
}
223+
224+
public String getCloudCustomData() {
225+
return cloudCustomData;
226+
}
227+
228+
public void setCloudCustomData(String cloudCustomData) {
229+
this.cloudCustomData = cloudCustomData;
230+
}
231+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package io.github.doocs.im.model.response;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
5+
import java.io.Serializable;
6+
7+
/**
8+
* 公众号用户发送广播消息-结果
9+
*
10+
* @author hyh
11+
* @since 2024/01/09 19:39
12+
*/
13+
public class SendOfficialAccountMsgResult extends GenericResult implements Serializable {
14+
private static final long serialVersionUID = 4691540073143009818L;
15+
/**
16+
* 消息发送的时间戳,对应后台 server 时间
17+
*/
18+
@JsonProperty("MsgTime")
19+
private Integer msgTime;
20+
21+
/**
22+
* 消息唯一标识,用于撤回。长度不超过50个字符
23+
*/
24+
@JsonProperty("MsgKey")
25+
private String msgKey;
26+
27+
public Integer getMsgTime() {
28+
return msgTime;
29+
}
30+
31+
public void setMsgTime(Integer msgTime) {
32+
this.msgTime = msgTime;
33+
}
34+
35+
public String getMsgKey() {
36+
return msgKey;
37+
}
38+
39+
public void setMsgKey(String msgKey) {
40+
this.msgKey = msgKey;
41+
}
42+
43+
@Override
44+
public String toString() {
45+
return "SendOfficialAccountMsgResult{" +
46+
"msgTime=" + msgTime +
47+
", msgKey='" + msgKey + '\'' +
48+
", actionStatus='" + actionStatus + '\'' +
49+
", errorInfo='" + errorInfo + '\'' +
50+
", errorCode=" + errorCode +
51+
'}';
52+
}
53+
}

src/test/java/io/github/doocs/im/core/MessageTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,18 @@ void testSetKeyValues() throws IOException {
192192
System.out.println(result);
193193
Assertions.assertEquals(ActionStatus.OK, result.getActionStatus());
194194
}
195+
196+
@Test
197+
void testSendOfficialAccountMsg() throws IOException {
198+
TIMTextMsgElement msg = new TIMTextMsgElement("hello world");
199+
List<TIMMsgElement> msgBody = Collections.singletonList(msg);
200+
SendOfficialAccountMsgRequest request = SendOfficialAccountMsgRequest.builder()
201+
.officialAccount("bingo")
202+
.random(123)
203+
.msgBody(msgBody)
204+
.build();
205+
SendOfficialAccountMsgResult result = client.message.sendOfficialAccountMsg(request);
206+
System.out.println(result);
207+
Assertions.assertEquals(ActionStatus.OK, result.getActionStatus());
208+
}
195209
}

0 commit comments

Comments
 (0)