Skip to content

Commit

Permalink
binarywang#1720 新增微信群机器人消息发送api
Browse files Browse the repository at this point in the history
  • Loading branch information
yang ran committed Aug 21, 2020
1 parent 04349ed commit 3c0ca50
Show file tree
Hide file tree
Showing 3 changed files with 235 additions and 0 deletions.
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;
}
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());
}

}
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();
}
}

0 comments on commit 3c0ca50

Please sign in to comment.