forked from binarywang/WxJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yang ran
committed
Aug 21, 2020
1 parent
04349ed
commit 3c0ca50
Showing
3 changed files
with
235 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpGroupRobotService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package me.chanjar.weixin.cp.api; | ||
|
||
import me.chanjar.weixin.common.error.WxErrorException; | ||
import me.chanjar.weixin.cp.bean.article.NewArticle; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* 微信群机器人消息发送api | ||
* 文档地址:https://work.weixin.qq.com/help?doc_id=13376 | ||
* 调用地址:https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key= | ||
* | ||
* @author yr | ||
* @date 2020-8-20 | ||
*/ | ||
public interface WxCpGroupRobotService { | ||
|
||
/** | ||
* 发送text类型的消息 | ||
* | ||
* @param content 文本内容,最长不超过2048个字节,必须是utf8编码 | ||
* @param mentionedList userId的列表,提醒群中的指定成员(@某个成员),@all表示提醒所有人,如果开发者获取不到userId,可以使用mentioned_mobile_list | ||
* @param mobileList 手机号列表,提醒手机号对应的群成员(@某个成员),@all表示提醒所有人 | ||
* @throws WxErrorException 异常 | ||
*/ | ||
void sendText(String content, List<String> mentionedList, List<String> mobileList) throws WxErrorException; | ||
|
||
/** | ||
* 发送markdown类型的消息 | ||
* | ||
* @param content markdown内容,最长不超过4096个字节,必须是utf8编码 | ||
* @throws WxErrorException 异常 | ||
*/ | ||
void sendMarkDown(String content) throws WxErrorException; | ||
|
||
/** | ||
* 发送image类型的消息 | ||
* | ||
* @param base64 图片内容的base64编码 | ||
* @param md5 图片内容(base64编码前)的md5值 | ||
* @throws WxErrorException 异常 | ||
*/ | ||
void sendImage(String base64, String md5) throws WxErrorException; | ||
|
||
/** | ||
* 发送news类型的消息 | ||
* | ||
* @param articleList 图文消息,支持1到8条图文 | ||
* @throws WxErrorException 异常 | ||
*/ | ||
void sendNews(List<NewArticle> articleList) throws WxErrorException; | ||
} |
65 changes: 65 additions & 0 deletions
65
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpGroupRobotServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package me.chanjar.weixin.cp.api.impl; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import me.chanjar.weixin.common.api.WxConsts; | ||
import me.chanjar.weixin.common.error.WxErrorException; | ||
import me.chanjar.weixin.cp.api.WxCpGroupRobotService; | ||
import me.chanjar.weixin.cp.api.WxCpService; | ||
import me.chanjar.weixin.cp.bean.WxCpGroupRobotMessage; | ||
import me.chanjar.weixin.cp.bean.article.NewArticle; | ||
import me.chanjar.weixin.cp.config.WxCpConfigStorage; | ||
import me.chanjar.weixin.cp.constant.WxCpApiPathConsts; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* 微信群机器人消息发送api 实现 | ||
* | ||
* @author yr | ||
* @date 2020-08-20 | ||
*/ | ||
@RequiredArgsConstructor | ||
public class WxCpGroupRobotServiceImpl implements WxCpGroupRobotService { | ||
private final WxCpService cpService; | ||
|
||
private String getApiUrl() { | ||
WxCpConfigStorage wxCpConfigStorage = cpService.getWxCpConfigStorage(); | ||
return wxCpConfigStorage.getApiUrl(WxCpApiPathConsts.WEBHOOK_SEND) + wxCpConfigStorage.getWebhookKey(); | ||
} | ||
|
||
@Override | ||
public void sendText(String content, List<String> mentionedList, List<String> mobileList) throws WxErrorException { | ||
WxCpGroupRobotMessage message = new WxCpGroupRobotMessage() | ||
.setMsgType(WxConsts.GroupRobotMsgType.TEXT) | ||
.setContent(content) | ||
.setMentionedList(mentionedList) | ||
.setMentionedMobileList(mobileList); | ||
cpService.postWithoutToken(this.getApiUrl(), message.toJson()); | ||
} | ||
|
||
@Override | ||
public void sendMarkDown(String content) throws WxErrorException { | ||
WxCpGroupRobotMessage message = new WxCpGroupRobotMessage() | ||
.setMsgType(WxConsts.GroupRobotMsgType.MARKDOWN) | ||
.setContent(content); | ||
cpService.postWithoutToken(this.getApiUrl(), message.toJson()); | ||
} | ||
|
||
@Override | ||
public void sendImage(String base64, String md5) throws WxErrorException { | ||
WxCpGroupRobotMessage message = new WxCpGroupRobotMessage() | ||
.setMsgType(WxConsts.GroupRobotMsgType.IMAGE) | ||
.setBase64(base64) | ||
.setMd5(md5); | ||
cpService.postWithoutToken(this.getApiUrl(), message.toJson()); | ||
} | ||
|
||
@Override | ||
public void sendNews(List<NewArticle> articleList) throws WxErrorException { | ||
WxCpGroupRobotMessage message = new WxCpGroupRobotMessage() | ||
.setMsgType(WxConsts.GroupRobotMsgType.NEWS) | ||
.setArticles(articleList); | ||
cpService.postWithoutToken(this.getApiUrl(), message.toJson()); | ||
} | ||
|
||
} |
118 changes: 118 additions & 0 deletions
118
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpGroupRobotMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package me.chanjar.weixin.cp.bean; | ||
|
||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonObject; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import lombok.experimental.Accessors; | ||
import me.chanjar.weixin.cp.bean.article.NewArticle; | ||
|
||
import java.util.List; | ||
|
||
import static me.chanjar.weixin.common.api.WxConsts.GroupRobotMsgType.*; | ||
|
||
/** | ||
* 微信群机器人消息 | ||
* | ||
* @author yr | ||
* @date 2020-08-20 | ||
*/ | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Accessors(chain = true) | ||
@Data | ||
public class WxCpGroupRobotMessage { | ||
/** | ||
* 消息类型 | ||
*/ | ||
private String msgType; | ||
|
||
/** | ||
* 文本内容,最长不超过2048个字节,markdown内容,最长不超过4096个字节,必须是utf8编码 | ||
* 必填 | ||
*/ | ||
private String content; | ||
/** | ||
* userid的列表,提醒群中的指定成员(@某个成员),@all表示提醒所有人,如果开发者获取不到userid,可以使用mentioned_mobile_list | ||
*/ | ||
private List<String> mentionedList; | ||
/** | ||
* 手机号列表,提醒手机号对应的群成员(@某个成员),@all表示提醒所有人 | ||
*/ | ||
private List<String> mentionedMobileList; | ||
/** | ||
* 图片内容的base64编码 | ||
*/ | ||
private String base64; | ||
/** | ||
* 图片内容(base64编码前)的md5值 | ||
*/ | ||
private String md5; | ||
/** | ||
* 图文消息,一个图文消息支持1到8条图文 | ||
*/ | ||
private List<NewArticle> articles; | ||
|
||
public String toJson() { | ||
JsonObject messageJson = new JsonObject(); | ||
messageJson.addProperty("msgtype", this.getMsgType()); | ||
|
||
switch (this.getMsgType()) { | ||
case TEXT: { | ||
JsonObject text = new JsonObject(); | ||
JsonArray uidJsonArray = new JsonArray(); | ||
JsonArray mobileJsonArray = new JsonArray(); | ||
|
||
text.addProperty("content", this.getContent()); | ||
|
||
if (this.getMentionedList() != null) { | ||
for (String item : this.getMentionedList()) { | ||
uidJsonArray.add(item); | ||
} | ||
} | ||
if (this.getMentionedMobileList() != null) { | ||
for (String item : this.getMentionedMobileList()) { | ||
mobileJsonArray.add(item); | ||
} | ||
} | ||
text.add("mentioned_list", uidJsonArray); | ||
text.add("mentioned_mobile_list", mobileJsonArray); | ||
messageJson.add("text", text); | ||
break; | ||
} | ||
case MARKDOWN: { | ||
JsonObject text = new JsonObject(); | ||
text.addProperty("content", this.getContent()); | ||
messageJson.add("markdown", text); | ||
break; | ||
} | ||
case IMAGE: { | ||
JsonObject text = new JsonObject(); | ||
text.addProperty("base64", this.getBase64()); | ||
text.addProperty("md5", this.getMd5()); | ||
messageJson.add("image", text); | ||
break; | ||
} | ||
case NEWS: { | ||
JsonObject text = new JsonObject(); | ||
JsonArray array = new JsonArray(); | ||
for (NewArticle article : this.getArticles()) { | ||
JsonObject articleJson = new JsonObject(); | ||
articleJson.addProperty("title", article.getTitle()); | ||
articleJson.addProperty("description", article.getDescription()); | ||
articleJson.addProperty("url", article.getUrl()); | ||
articleJson.addProperty("picurl", article.getPicUrl()); | ||
array.add(articleJson); | ||
} | ||
text.add("articles", array); | ||
messageJson.add("news", text); | ||
break; | ||
} | ||
default: | ||
|
||
} | ||
|
||
return messageJson.toString(); | ||
} | ||
} |