Skip to content

Commit 3c0ca50

Browse files
author
yang ran
committed
binarywang#1720 新增微信群机器人消息发送api
1 parent 04349ed commit 3c0ca50

File tree

3 files changed

+235
-0
lines changed

3 files changed

+235
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package me.chanjar.weixin.cp.api;
2+
3+
import me.chanjar.weixin.common.error.WxErrorException;
4+
import me.chanjar.weixin.cp.bean.article.NewArticle;
5+
6+
import java.util.List;
7+
8+
/**
9+
* 微信群机器人消息发送api
10+
* 文档地址:https://work.weixin.qq.com/help?doc_id=13376
11+
* 调用地址:https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=
12+
*
13+
* @author yr
14+
* @date 2020-8-20
15+
*/
16+
public interface WxCpGroupRobotService {
17+
18+
/**
19+
* 发送text类型的消息
20+
*
21+
* @param content 文本内容,最长不超过2048个字节,必须是utf8编码
22+
* @param mentionedList userId的列表,提醒群中的指定成员(@某个成员),@all表示提醒所有人,如果开发者获取不到userId,可以使用mentioned_mobile_list
23+
* @param mobileList 手机号列表,提醒手机号对应的群成员(@某个成员),@all表示提醒所有人
24+
* @throws WxErrorException 异常
25+
*/
26+
void sendText(String content, List<String> mentionedList, List<String> mobileList) throws WxErrorException;
27+
28+
/**
29+
* 发送markdown类型的消息
30+
*
31+
* @param content markdown内容,最长不超过4096个字节,必须是utf8编码
32+
* @throws WxErrorException 异常
33+
*/
34+
void sendMarkDown(String content) throws WxErrorException;
35+
36+
/**
37+
* 发送image类型的消息
38+
*
39+
* @param base64 图片内容的base64编码
40+
* @param md5 图片内容(base64编码前)的md5值
41+
* @throws WxErrorException 异常
42+
*/
43+
void sendImage(String base64, String md5) throws WxErrorException;
44+
45+
/**
46+
* 发送news类型的消息
47+
*
48+
* @param articleList 图文消息,支持1到8条图文
49+
* @throws WxErrorException 异常
50+
*/
51+
void sendNews(List<NewArticle> articleList) throws WxErrorException;
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package me.chanjar.weixin.cp.api.impl;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import me.chanjar.weixin.common.api.WxConsts;
5+
import me.chanjar.weixin.common.error.WxErrorException;
6+
import me.chanjar.weixin.cp.api.WxCpGroupRobotService;
7+
import me.chanjar.weixin.cp.api.WxCpService;
8+
import me.chanjar.weixin.cp.bean.WxCpGroupRobotMessage;
9+
import me.chanjar.weixin.cp.bean.article.NewArticle;
10+
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
11+
import me.chanjar.weixin.cp.constant.WxCpApiPathConsts;
12+
13+
import java.util.List;
14+
15+
/**
16+
* 微信群机器人消息发送api 实现
17+
*
18+
* @author yr
19+
* @date 2020-08-20
20+
*/
21+
@RequiredArgsConstructor
22+
public class WxCpGroupRobotServiceImpl implements WxCpGroupRobotService {
23+
private final WxCpService cpService;
24+
25+
private String getApiUrl() {
26+
WxCpConfigStorage wxCpConfigStorage = cpService.getWxCpConfigStorage();
27+
return wxCpConfigStorage.getApiUrl(WxCpApiPathConsts.WEBHOOK_SEND) + wxCpConfigStorage.getWebhookKey();
28+
}
29+
30+
@Override
31+
public void sendText(String content, List<String> mentionedList, List<String> mobileList) throws WxErrorException {
32+
WxCpGroupRobotMessage message = new WxCpGroupRobotMessage()
33+
.setMsgType(WxConsts.GroupRobotMsgType.TEXT)
34+
.setContent(content)
35+
.setMentionedList(mentionedList)
36+
.setMentionedMobileList(mobileList);
37+
cpService.postWithoutToken(this.getApiUrl(), message.toJson());
38+
}
39+
40+
@Override
41+
public void sendMarkDown(String content) throws WxErrorException {
42+
WxCpGroupRobotMessage message = new WxCpGroupRobotMessage()
43+
.setMsgType(WxConsts.GroupRobotMsgType.MARKDOWN)
44+
.setContent(content);
45+
cpService.postWithoutToken(this.getApiUrl(), message.toJson());
46+
}
47+
48+
@Override
49+
public void sendImage(String base64, String md5) throws WxErrorException {
50+
WxCpGroupRobotMessage message = new WxCpGroupRobotMessage()
51+
.setMsgType(WxConsts.GroupRobotMsgType.IMAGE)
52+
.setBase64(base64)
53+
.setMd5(md5);
54+
cpService.postWithoutToken(this.getApiUrl(), message.toJson());
55+
}
56+
57+
@Override
58+
public void sendNews(List<NewArticle> articleList) throws WxErrorException {
59+
WxCpGroupRobotMessage message = new WxCpGroupRobotMessage()
60+
.setMsgType(WxConsts.GroupRobotMsgType.NEWS)
61+
.setArticles(articleList);
62+
cpService.postWithoutToken(this.getApiUrl(), message.toJson());
63+
}
64+
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package me.chanjar.weixin.cp.bean;
2+
3+
import com.google.gson.JsonArray;
4+
import com.google.gson.JsonObject;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Data;
7+
import lombok.NoArgsConstructor;
8+
import lombok.experimental.Accessors;
9+
import me.chanjar.weixin.cp.bean.article.NewArticle;
10+
11+
import java.util.List;
12+
13+
import static me.chanjar.weixin.common.api.WxConsts.GroupRobotMsgType.*;
14+
15+
/**
16+
* 微信群机器人消息
17+
*
18+
* @author yr
19+
* @date 2020-08-20
20+
*/
21+
@AllArgsConstructor
22+
@NoArgsConstructor
23+
@Accessors(chain = true)
24+
@Data
25+
public class WxCpGroupRobotMessage {
26+
/**
27+
* 消息类型
28+
*/
29+
private String msgType;
30+
31+
/**
32+
* 文本内容,最长不超过2048个字节,markdown内容,最长不超过4096个字节,必须是utf8编码
33+
* 必填
34+
*/
35+
private String content;
36+
/**
37+
* userid的列表,提醒群中的指定成员(@某个成员),@all表示提醒所有人,如果开发者获取不到userid,可以使用mentioned_mobile_list
38+
*/
39+
private List<String> mentionedList;
40+
/**
41+
* 手机号列表,提醒手机号对应的群成员(@某个成员),@all表示提醒所有人
42+
*/
43+
private List<String> mentionedMobileList;
44+
/**
45+
* 图片内容的base64编码
46+
*/
47+
private String base64;
48+
/**
49+
* 图片内容(base64编码前)的md5值
50+
*/
51+
private String md5;
52+
/**
53+
* 图文消息,一个图文消息支持1到8条图文
54+
*/
55+
private List<NewArticle> articles;
56+
57+
public String toJson() {
58+
JsonObject messageJson = new JsonObject();
59+
messageJson.addProperty("msgtype", this.getMsgType());
60+
61+
switch (this.getMsgType()) {
62+
case TEXT: {
63+
JsonObject text = new JsonObject();
64+
JsonArray uidJsonArray = new JsonArray();
65+
JsonArray mobileJsonArray = new JsonArray();
66+
67+
text.addProperty("content", this.getContent());
68+
69+
if (this.getMentionedList() != null) {
70+
for (String item : this.getMentionedList()) {
71+
uidJsonArray.add(item);
72+
}
73+
}
74+
if (this.getMentionedMobileList() != null) {
75+
for (String item : this.getMentionedMobileList()) {
76+
mobileJsonArray.add(item);
77+
}
78+
}
79+
text.add("mentioned_list", uidJsonArray);
80+
text.add("mentioned_mobile_list", mobileJsonArray);
81+
messageJson.add("text", text);
82+
break;
83+
}
84+
case MARKDOWN: {
85+
JsonObject text = new JsonObject();
86+
text.addProperty("content", this.getContent());
87+
messageJson.add("markdown", text);
88+
break;
89+
}
90+
case IMAGE: {
91+
JsonObject text = new JsonObject();
92+
text.addProperty("base64", this.getBase64());
93+
text.addProperty("md5", this.getMd5());
94+
messageJson.add("image", text);
95+
break;
96+
}
97+
case NEWS: {
98+
JsonObject text = new JsonObject();
99+
JsonArray array = new JsonArray();
100+
for (NewArticle article : this.getArticles()) {
101+
JsonObject articleJson = new JsonObject();
102+
articleJson.addProperty("title", article.getTitle());
103+
articleJson.addProperty("description", article.getDescription());
104+
articleJson.addProperty("url", article.getUrl());
105+
articleJson.addProperty("picurl", article.getPicUrl());
106+
array.add(articleJson);
107+
}
108+
text.add("articles", array);
109+
messageJson.add("news", text);
110+
break;
111+
}
112+
default:
113+
114+
}
115+
116+
return messageJson.toString();
117+
}
118+
}

0 commit comments

Comments
 (0)