From 6713787bb956b41da24697098d44c3496a7e3554 Mon Sep 17 00:00:00 2001 From: Binary Wang Date: Sun, 20 Sep 2020 15:25:04 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E4=BC=98=E5=8C=96=E4=BC=81=E4=B8=9A?= =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E7=BE=A4=E6=9C=BA=E5=99=A8=E4=BA=BA=E5=8F=91?= =?UTF-8?q?=E9=80=81=E6=B6=88=E6=81=AF=E7=9A=84=E7=9B=B8=E5=85=B3=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=EF=BC=8C=E6=8F=90=E4=BE=9B=E6=97=A0=E9=9C=80=E6=8F=90?= =?UTF-8?q?=E5=89=8D=E8=AE=BE=E7=BD=AEwebhookKey=E5=8D=B3=E5=8F=AF?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=9A=84=E9=87=8D=E6=9E=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../weixin/cp/api/WxCpGroupRobotService.java | 41 ++++++++++- .../cp/api/impl/BaseWxCpServiceImpl.java | 2 +- .../api/impl/WxCpGroupRobotServiceImpl.java | 73 +++++++++++++------ .../impl/WxCpGroupRobotServiceImplTest.java | 2 +- 4 files changed, 91 insertions(+), 27 deletions(-) diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpGroupRobotService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpGroupRobotService.java index 007dff78f..b5a9579e0 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpGroupRobotService.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpGroupRobotService.java @@ -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 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 mentionedList, List 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 articleList) throws WxErrorException; } diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/BaseWxCpServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/BaseWxCpServiceImpl.java index 97faa4c96..a04f46989 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/BaseWxCpServiceImpl.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/BaseWxCpServiceImpl.java @@ -305,7 +305,7 @@ public abstract class BaseWxCpServiceImpl 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); } } diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpGroupRobotServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpGroupRobotServiceImpl.java index c5e71f5f2..996249eb3 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpGroupRobotServiceImpl.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpGroupRobotServiceImpl.java @@ -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 mentionedList, List 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 articleList) throws WxErrorException { - WxCpGroupRobotMessage message = new WxCpGroupRobotMessage() + this.sendNews(this.getWebhookUrl(), articleList); + } + + @Override + public void sendText(String webhookUrl, String content, List mentionedList, List 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 articleList) throws WxErrorException { + this.cpService.postWithoutToken(this.getWebhookUrl(), new WxCpGroupRobotMessage() .setMsgType(GroupRobotMsgType.NEWS) - .setArticles(articleList); - cpService.postWithoutToken(this.getApiUrl(), message.toJson()); + .setArticles(articleList).toJson()); } } diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpGroupRobotServiceImplTest.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpGroupRobotServiceImplTest.java index 40597fa24..57bd9b750 100644 --- a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpGroupRobotServiceImplTest.java +++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpGroupRobotServiceImplTest.java @@ -46,7 +46,7 @@ public class WxCpGroupRobotServiceImplTest { ">类型:用户反馈 \n" + ">普通用户反馈:117例 \n" + ">VIP用户反馈:15例"; - robotService.sendMarkDown(content); + robotService.sendMarkdown(content); } @Test