1
0
mirror of synced 2025-12-18 22:08:01 +08:00

🎨 优化企业微信群机器人发送消息的相关接口,提供无需提前设置webhookKey即可使用的重构方法

This commit is contained in:
Binary Wang
2020-09-20 15:25:04 +08:00
parent 489942a99b
commit 6713787bb9
4 changed files with 91 additions and 27 deletions

View File

@@ -31,7 +31,7 @@ public interface WxCpGroupRobotService {
* @param content markdown内容最长不超过4096个字节必须是utf8编码
* @throws WxErrorException 异常
*/
void sendMarkDown(String content) throws WxErrorException;
void sendMarkdown(String content) throws WxErrorException;
/**
* 发送image类型的消息
@@ -49,4 +49,43 @@ public interface WxCpGroupRobotService {
* @throws WxErrorException 异常
*/
void sendNews(List<NewArticle> articleList) throws WxErrorException;
/**
* 发送text类型的消息
*
* @param webhookUrl webhook地址
* @param content 文本内容最长不超过2048个字节必须是utf8编码
* @param mentionedList userId的列表提醒群中的指定成员(@某个成员)@all表示提醒所有人如果开发者获取不到userId可以使用mentioned_mobile_list
* @param mobileList 手机号列表,提醒手机号对应的群成员(@某个成员)@all表示提醒所有人
* @throws WxErrorException 异常
*/
void sendText(String webhookUrl, String content, List<String> mentionedList, List<String> mobileList) throws WxErrorException;
/**
* 发送markdown类型的消息
*
* @param webhookUrl webhook地址
* @param content markdown内容最长不超过4096个字节必须是utf8编码
* @throws WxErrorException 异常
*/
void sendMarkdown(String webhookUrl, String content) throws WxErrorException;
/**
* 发送image类型的消息
*
* @param webhookUrl webhook地址
* @param base64 图片内容的base64编码
* @param md5 图片内容base64编码前的md5值
* @throws WxErrorException 异常
*/
void sendImage(String webhookUrl, String base64, String md5) throws WxErrorException;
/**
* 发送news类型的消息
*
* @param webhookUrl webhook地址
* @param articleList 图文消息支持1到8条图文
* @throws WxErrorException 异常
*/
void sendNews(String webhookUrl, List<NewArticle> articleList) throws WxErrorException;
}

View File

@@ -305,7 +305,7 @@ public abstract class BaseWxCpServiceImpl<H, P> implements WxCpService, RequestH
return null;
} catch (IOException e) {
log.error("\n【请求地址】: {}\n【请求参数】{}\n【异常信息】{}", uri, data, e.getMessage());
throw new RuntimeException(e);
throw new WxErrorException(WxError.builder().errorMsg(e.getMessage()).errorCode(-1).build(),e);
}
}

View File

@@ -1,6 +1,7 @@
package me.chanjar.weixin.cp.api.impl;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpGroupRobotService;
import me.chanjar.weixin.cp.api.WxCpService;
@@ -8,14 +9,16 @@ import me.chanjar.weixin.cp.bean.article.NewArticle;
import me.chanjar.weixin.cp.bean.message.WxCpGroupRobotMessage;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
import me.chanjar.weixin.cp.constant.WxCpApiPathConsts;
import org.apache.commons.lang3.StringUtils;
import java.util.List;
import static me.chanjar.weixin.cp.constant.WxCpConsts.GroupRobotMsgType;
import static me.chanjar.weixin.cp.constant.WxCpConsts.GroupRobotMsgType.*;
import static me.chanjar.weixin.cp.constant.WxCpConsts.GroupRobotMsgType.MARKDOWN;
import static me.chanjar.weixin.cp.constant.WxCpConsts.GroupRobotMsgType.TEXT;
/**
* 微信群机器人消息发送api 实现
* 企业微信群机器人消息发送api 实现
*
* @author yr
* @date 2020-08-20
@@ -24,44 +27,66 @@ import static me.chanjar.weixin.cp.constant.WxCpConsts.GroupRobotMsgType.*;
public class WxCpGroupRobotServiceImpl implements WxCpGroupRobotService {
private final WxCpService cpService;
private String getApiUrl() {
WxCpConfigStorage wxCpConfigStorage = cpService.getWxCpConfigStorage();
return wxCpConfigStorage.getApiUrl(WxCpApiPathConsts.WEBHOOK_SEND) + wxCpConfigStorage.getWebhookKey();
private String getWebhookUrl() throws WxErrorException {
WxCpConfigStorage wxCpConfigStorage = this.cpService.getWxCpConfigStorage();
final String webhookKey = wxCpConfigStorage.getWebhookKey();
if (StringUtils.isEmpty(webhookKey)) {
throw new WxErrorException(WxError.builder().errorCode(-1).errorMsg("请先设置WebhookKey").build());
}
return wxCpConfigStorage.getApiUrl(WxCpApiPathConsts.WEBHOOK_SEND) + webhookKey;
}
@Override
public void sendText(String content, List<String> mentionedList, List<String> mobileList) throws WxErrorException {
WxCpGroupRobotMessage message = new WxCpGroupRobotMessage()
.setMsgType(TEXT)
.setContent(content)
.setMentionedList(mentionedList)
.setMentionedMobileList(mobileList);
cpService.postWithoutToken(this.getApiUrl(), message.toJson());
this.sendText(this.getWebhookUrl(), content, mentionedList, mobileList);
}
@Override
public void sendMarkDown(String content) throws WxErrorException {
WxCpGroupRobotMessage message = new WxCpGroupRobotMessage()
.setMsgType(MARKDOWN)
.setContent(content);
cpService.postWithoutToken(this.getApiUrl(), message.toJson());
public void sendMarkdown(String content) throws WxErrorException {
this.sendMarkdown(this.getWebhookUrl(), content);
}
@Override
public void sendImage(String base64, String md5) throws WxErrorException {
WxCpGroupRobotMessage message = new WxCpGroupRobotMessage()
.setMsgType(GroupRobotMsgType.IMAGE)
.setBase64(base64)
.setMd5(md5);
cpService.postWithoutToken(this.getApiUrl(), message.toJson());
this.sendImage(this.getWebhookUrl(), base64, md5);
}
@Override
public void sendNews(List<NewArticle> articleList) throws WxErrorException {
WxCpGroupRobotMessage message = new WxCpGroupRobotMessage()
this.sendNews(this.getWebhookUrl(), articleList);
}
@Override
public void sendText(String webhookUrl, String content, List<String> mentionedList, List<String> mobileList) throws WxErrorException {
this.cpService.postWithoutToken(webhookUrl, new WxCpGroupRobotMessage()
.setMsgType(TEXT)
.setContent(content)
.setMentionedList(mentionedList)
.setMentionedMobileList(mobileList)
.toJson());
}
@Override
public void sendMarkdown(String webhookUrl, String content) throws WxErrorException {
this.cpService.postWithoutToken(webhookUrl, new WxCpGroupRobotMessage()
.setMsgType(MARKDOWN)
.setContent(content)
.toJson());
}
@Override
public void sendImage(String webhookUrl, String base64, String md5) throws WxErrorException {
this.cpService.postWithoutToken(this.getWebhookUrl(), new WxCpGroupRobotMessage()
.setMsgType(GroupRobotMsgType.IMAGE)
.setBase64(base64)
.setMd5(md5).toJson());
}
@Override
public void sendNews(String webhookUrl, List<NewArticle> articleList) throws WxErrorException {
this.cpService.postWithoutToken(this.getWebhookUrl(), new WxCpGroupRobotMessage()
.setMsgType(GroupRobotMsgType.NEWS)
.setArticles(articleList);
cpService.postWithoutToken(this.getApiUrl(), message.toJson());
.setArticles(articleList).toJson());
}
}

View File

@@ -46,7 +46,7 @@ public class WxCpGroupRobotServiceImplTest {
">类型:<font color=\"comment\">用户反馈</font> \n" +
">普通用户反馈:<font color=\"comment\">117例</font> \n" +
">VIP用户反馈:<font color=\"comment\">15例</font>";
robotService.sendMarkDown(content);
robotService.sendMarkdown(content);
}
@Test