背景
如标题。
文档
操作
我是在电脑客户端操作的。
创建一个最少3人的群,添加机器人的入口在群右上角三个点。
步骤截图如下:
得到的webhook地址要复制下来。
使用代码调用
代码调用也很简单,只要调用webhook地址,传入不同参数即可。
我在github上找到一个库。https://github.com/lqccan/wechat-work-bot
maven中添加
<!-- https://mvnrepository.com/artifact/com.github.lqccan/wechat-work-bot -->
<dependency>
<groupId>com.github.lqccan</groupId>
<artifactId>wechat-work-bot</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
java代码如下:
package cn.wework.bot;
import com.github.lqccan.wechat.work.bot.Bot;
import com.github.lqccan.wechat.work.bot.msg.ArticleMsg;
import com.github.lqccan.wechat.work.bot.msg.ImageMsg;
import com.github.lqccan.wechat.work.bot.msg.MarkdownMsg;
import com.github.lqccan.wechat.work.bot.msg.TextMsg;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
/**
* .
*
* @author lei.liu
* @since 2025-06-05
*/
public class BotTest {
private static Bot bot;
/**
* 构造机器人对象
*/
@BeforeAll
public static void buildBot() {
bot = new Bot("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxx");
}
/**
* 文字测试
*/
@Test
public void textTest() {
TextMsg textMsg = new TextMsg();
// 可以有\n
textMsg.setContent("文字发送\nhttp://wp.valuetodays.cn/archives/354");
bot.send(textMsg);
}
/**
* markdown测试
*/
@Test
public void markdownTest() {
MarkdownMsg markdownMsg = new MarkdownMsg();
markdownMsg.setContent("## 实时新增用户反馈<font color=\"warning\">132例</font>,请相关同事注意。\n" +
" >类型:<font color=\"comment\">用户反馈</font>\n" +
" >普通用户反馈:<font color=\"comment\">117例</font>\n" +
" >VIP用户反馈:<font color=\"comment\">15例</font>");
bot.send(markdownMsg);
}
/**
* 图片测试
*/
@Test
public void imageTest() {
ImageMsg imageMsg = new ImageMsg();
//本地文件
// imageMsg.setFile(new File("/Users/xxx.png"));
//网络文件
imageMsg.setUrl("https://valuetodays.github.io/statics/images/wp/mmpic/QmzRUqQuT1Oy9P_QfiENyg/1.jpeg");
bot.send(imageMsg);
}
/**
* 图文测试
*/
@Test
public void articleTest() {
ArticleMsg articleMsg = new ArticleMsg();
articleMsg.setTitle("鞠婧祎 壁纸");
articleMsg.setDescription("鞠婧祎 129张高清合集 清纯可人");
articleMsg.setUrl("http://wp.valuetodays.cn/archives/354");
articleMsg.setPicurl("https://valuetodays.github.io/statics/images/wp/mmpic/QmzRUqQuT1Oy9P_QfiENyg/1.jpeg");
bot.send(articleMsg);
}
/**
* 多个图文测试
*/
@Test
public void articleListTest() {
List<ArticleMsg> list = new ArrayList<>();
for (int i = 1; i <= 3; i++) {
ArticleMsg articleMsg = new ArticleMsg();
articleMsg.setTitle("图文"+i + " 壁纸");
articleMsg.setDescription("鞠婧祎 129张高清合集 清纯可人");
articleMsg.setUrl("http://wp.valuetodays.cn/archives/354");
articleMsg.setPicurl("https://valuetodays.github.io/statics/images/wp/mmpic/QmzRUqQuT1Oy9P_QfiENyg/1.jpeg");
list.add(articleMsg);
}
bot.send(list);
}
}
附件
如上复制到工程中就能正常运行了,再要附件就不太合适了吧_。