Skip to content

face(企业微信):发送群聊机器人消息, 增加文件消息 #2649

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,13 @@ public interface WxCpGroupRobotService {
* @throws WxErrorException 异常
*/
void sendNews(String webhookUrl, List<NewArticle> articleList) throws WxErrorException;

/**
* 发送文件类型的消息
*
* @param webhookUrl webhook地址
* @param mediaId 文件id
* @throws WxErrorException 异常
*/
void sendFile(String webhookUrl, String mediaId) throws WxErrorException;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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;
Expand All @@ -14,8 +13,6 @@
import java.util.List;

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 实现
Expand Down Expand Up @@ -59,7 +56,7 @@ public void sendNews(List<NewArticle> articleList) throws WxErrorException {
@Override
public void sendText(String webhookUrl, String content, List<String> mentionedList, List<String> mobileList) throws WxErrorException {
this.cpService.postWithoutToken(webhookUrl, new WxCpGroupRobotMessage()
.setMsgType(TEXT)
.setMsgType(GroupRobotMsgType.TEXT)
.setContent(content)
.setMentionedList(mentionedList)
.setMentionedMobileList(mobileList)
Expand All @@ -69,7 +66,7 @@ public void sendText(String webhookUrl, String content, List<String> mentionedLi
@Override
public void sendMarkdown(String webhookUrl, String content) throws WxErrorException {
this.cpService.postWithoutToken(webhookUrl, new WxCpGroupRobotMessage()
.setMsgType(MARKDOWN)
.setMsgType(GroupRobotMsgType.MARKDOWN)
.setContent(content)
.toJson());
}
Expand All @@ -89,4 +86,11 @@ public void sendNews(String webhookUrl, List<NewArticle> articleList) throws WxE
.setArticles(articleList).toJson());
}

@Override
public void sendFile(String webhookUrl, String mediaId) throws WxErrorException {
this.cpService.postWithoutToken(webhookUrl, new WxCpGroupRobotMessage()
.setMsgType(GroupRobotMsgType.FILE)
.setMediaId(mediaId).toJson());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public class WxCpGroupRobotMessage implements Serializable {
*/
private List<NewArticle> articles;

/**
* 文件id
*/
private String mediaId;

public String toJson() {
JsonObject messageJson = new JsonObject();
messageJson.addProperty("msgtype", this.getMsgType());
Expand Down Expand Up @@ -112,6 +117,12 @@ public String toJson() {
messageJson.add("news", text);
break;
}
case FILE: {
JsonObject file = new JsonObject();
file.addProperty("media_id", this.getMediaId());
messageJson.add("file", file);
break;
}
default:

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,12 @@ public static class GroupRobotMsgType {
* 图文消息(点击跳转到外链).
*/
public static final String NEWS = "news";

/**
* 文件类型消息.
*/
public static final String FILE = "file";

}

/**
Expand Down