一个很好用的 企业微信机器人 开发工具包。 Github | Gitee | Maven
<dependency>
<groupId>cn.hamm.wecom</groupId>
<artifactId>WeComRobotSDK</artifactId>
<version>1.0.0</version>
</dependency>
请注意,以下为示例代码,仅展示了常用的一些配置方法,可安装依赖后在项目中查看更多。
WeComTextMessage weComTextMessage = new WeComTextMessage();
weComTextMessage.getText().setContent("text");
weComTextMessage.send(ROBOT_KEY);
WeComImageMessage weComImageMessage = new WeComImageMessage();
String file = "src/test/resources/image.png";
Path path = Paths.get(file);
weComImageMessage.getImage().setPath(path);
weComImageMessage.send(ROBOT_KEY);
WeComNewsMessage weComNewsMessage = new WeComNewsMessage();
weComNewsMessage.getNews().addArticle(
new WeComNewsMessage.News.Article()
.setTitle("Hello")
.setDescription("World")
.setPicUrl(IMAGE_URL)
.setUrl(HAMM_CN)
).addArticle(
new WeComNewsMessage.News.Article()
.setTitle("Hello")
.setDescription("World")
.setPicUrl(IMAGE_URL)
.setUrl(HAMM_CN)
);
weComNewsMessage.send(ROBOT_KEY);
String file = "src/test/resources/test.pdf";
String mediaId = WeComFile.uploadFile(ROBOT_KEY, file);
WeComFileMessage weComFileMessage = new WeComFileMessage();
weComFileMessage.getFile().setMediaId(mediaId);
weComFileMessage.send(ROBOT_KEY);
String file = "src/test/resources/voice.amr";
String mediaId = WeComFile.uploadVoice(ROBOT_KEY, file);
WeComVoiceMessage weComVoiceMessage = new WeComVoiceMessage();
weComVoiceMessage.getVoice().setMediaId(mediaId);
weComVoiceMessage.send(ROBOT_KEY);
- 6.1 文本通知卡片
// 按需设置
WeComTemplateCardMessage weComTemplateCardMessage = new WeComTemplateCardMessage();
TemplateCard templateCard = new TemplateCard().setCardTypeText();
templateCard.getMainTitle().setTitle("TemplateCard").setDesc("desc");
templateCard.getSource()
.setDesc("desc")
.setDescColor(DescColor.GREEN)
.setIconUrl(IMAGE_URL);
templateCard.getEmphasisContent().setTitle("10000").setDesc("Desc");
templateCard.getCardAction().setUrl(HAMM_CN);
templateCard.addJump(new TemplateCard.Jump().setTitle("Github").setUrl(HAMM_CN));
templateCard.addHorizontalContent(new TemplateCard.HorizontalContent().setKeyName("Key").setValue("Value"));
templateCard.setSubTitleText("subTitleText");
weComTemplateCardMessage.setTemplateCard(templateCard);
weComTemplateCardMessage.send(ROBOT_KEY);
- 6.2 图文展示卡片
// 按需设置
WeComTemplateCardMessage weComTemplateCardMessage = new WeComTemplateCardMessage();
TemplateCard templateCard = new TemplateCard().setCardTypeImage();
templateCard.getMainTitle().setTitle("TemplateCard").setDesc("desc");
templateCard.getSource()
.setDesc("desc")
.setDescColor(DescColor.GREEN)
.setIconUrl(IMAGE_URL);
templateCard.getEmphasisContent().setTitle("10000").setDesc("Desc");
templateCard.getCardAction().setUrl(HAMM_CN);
templateCard.addJump(new TemplateCard.Jump().setTitle("Github").setUrl(HAMM_CN));
templateCard.addHorizontalContent(new TemplateCard.HorizontalContent().setKeyName("Key").setValue("Value"));
templateCard.setSubTitleText("subTitleText");
weComTemplateCardMessage.setTemplateCard(templateCard);
weComTemplateCardMessage.send(ROBOT_KEY);
// 上传语音
String file = "src/test/resources/voice.amr";
String mediaId = WeComFile.uploadVoice(ROBOT_KEY, file);
System.out.println(mediaId);
// 上传文件
String file = "src/test/resources/test.pdf";
String mediaId = WeComFile.uploadFIle(ROBOT_KEY, file);
System.out.println(mediaId);
欢迎在本仓库的 Issues
提出你的问题或者反馈发现的 BUG。 也欢迎加入我们的开发者交流QQ群: 1140258698