SpringBoot接入企微机器人

1、企业微信创建机器人(如何创建不懂的请自行百度,很简单的),成功后能获取到一个Webhook地址:https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=693a91f6-7xxx-4bc4-97a0-0ec2sifa5aaa

2、创建一个SpringBoot项目(具备基础结构即可,里面的类可暂时忽略)

3、创建dto对象,具体对象的构建可以参考机器人发送消息的入参,群机器人配置说明参考官方文档:https://developer.work.weixin.qq.com/document/path/99110

  • 这里的结构可以自己根据文档里数据结构进行构建
  • 自己需要什么就创建对应的dto
  • 我这里是单纯的发送文字,更多的可以发送图片、链接、附件等,具体参考文档
java 复制代码
@Data
@Accessors(chain = true)
public class Publisher {
    private String msgtype;
    private Media file;
    private Text text;
}
java 复制代码
@Data
@Accessors(chain = true)
public class Text {
    private String content;
    private List<String> mentioned_list;
    private List<String> mentioned_mobile_list;
}
java 复制代码
@Data
@Accessors(chain = true)
public class Media {
    private String media_id;
}

4、创建一个接口发送实现类,如果只有一种发送方式,可以不用接口,我这里用了两种方式,分别是通过单纯的http请求的方式发送请求,另一种是通过feignClient的方式进行接口请求,所以使用了接口。

java 复制代码
public interface ISenderService {
    void send(Publisher publisher) throws Exception;
}

5、第一种:使用基础的http请求方式调用企微机器人,这是比较简单的方式,我这里就不提供HttpUtil类的代码了,相信你自己的项目中这种util类肯定会有的,代码相对简单,不同项目会有一些不同,但基本都大同小异。

  • 这里的wechatrobot.url配置在配置文件中进行配置,配置的就是上面我们获取到的企微机器人的Webhook地址了
java 复制代码
@Service
public class HttpSender implements ISenderService {
    @Value("${wechatrobot.url}")
    private String url;
    @Override
    public void send(Publisher publisher) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        String request = objectMapper.writeValueAsString(publisher);
        HttpUtil.post(url, request);
    }
}

6、第二种:使用FeginClient的方式调用企微机器人,记得启动类上加上注解:@EnableFeignClients,这里涉及到SpringCloud的知识了

  • 如果你的项目里不使用SpringCloud,那就不用关心这个了,了解一下即可,对应pom.xml文件的springCloud的依赖需要自己引入,懂的都懂(DDDD)
java 复制代码
@Service
public class FeginClientSender implements ISenderService{
    @Autowired
    private WechatRobotActions wechatRobotActions;
    @Override
    public void send(Publisher publisher) throws Exception {
        wechatRobotActions.publish(publisher);
    }
}
java 复制代码
@Component
@FeignClient(name = "wechat-robot", url = "${wechatrobot.url}")
public interface WechatRobotActions {
    @PostMapping(path = "",consumes = "application/json")
    void publish( Publisher publisher);
}

7、配置文件:bootstrap.yaml,除了项目本身的一些基础配置,记得加上我们前面用到的配置:wechatrobot.url

java 复制代码
wechatrobot:
  url: https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=693a91f6-7xxx-4bc4-97a0-0ec2sifa5aaa

8、测试类,并不复杂,相信都会写

java 复制代码
@SpringBootTest
class ISenderServiceTest {
    @Autowired
    private HttpSender httpSender;
    @Autowired
    private FeginClientSender feginClientSender;
    @Test
    void send() throws Exception {
        Publisher publisher = new Publisher();
        publisher.setMsgtype("text");
        publisher.setText(new Text().setContent("我是甲鱼王,咿呀咿呀哟"));
        httpSender.send(publisher);
    }
    @Test
    void send1() throws Exception {
        Publisher publisher = new Publisher();
        publisher.setMsgtype("text");
        publisher.setText(new Text().setContent("我也是甲鱼王,咿呀咿呀哟"));
        feginClientSender.send(publisher);
    }
}

9、实践

上面说了也可以发送图片,包括链接等,类似于下面这种

代码如下,实体类相信自己懂得修改

java 复制代码
@Test
    void send2() throws Exception {
        Publisher publisher = new Publisher();
        publisher.setMsgtype("news");
        List<Article> articles = new ArrayList<>();
        articles.add(new Article()
                .setTitle("2024新安排,元旦放假五天")
                .setDescription("2024放假安排")
                .setUrl("https://pic3.zhimg.com/50/v2-e513e8daa9fd7971719e79ad0d8c8c73_hd.jpg")
                .setPicurl("https://www.gov.cn/zhengce/jiedu/tujie/202310/W020231025350204905165_ORIGIN.jpg"));
        publisher.setNews(new News().setArticles(articles));
        feginClientSender.send(publisher);
    }
相关推荐
LiRuiJie11 分钟前
深入剖析Spring Boot / Spring 应用中可自定义的扩展点
java·spring boot·spring
尚学教辅学习资料2 小时前
Ruoyi-vue-plus-5.x第五篇Spring框架核心技术:5.1 Spring Boot自动配置
vue.js·spring boot·spring
晚安里3 小时前
Spring 框架(IoC、AOP、Spring Boot) 的必会知识点汇总
java·spring boot·spring
上官浩仁3 小时前
springboot ioc 控制反转入门与实战
java·spring boot·spring
叫我阿柒啊4 小时前
从Java全栈到前端框架:一位程序员的实战之路
java·spring boot·微服务·消息队列·vue3·前端开发·后端开发
中国胖子风清扬4 小时前
Rust 序列化技术全解析:从基础到实战
开发语言·c++·spring boot·vscode·后端·中间件·rust
中國龍在廣州4 小时前
GPT-5冷酷操盘,游戏狼人杀一战封神!七大LLM狂飙演技,人类玩家看完沉默
人工智能·gpt·深度学习·机器学习·计算机视觉·机器人
泰迪智能科技7 小时前
案例分享|企微智能会话风控系统:为尚丰盈铝业筑牢沟通安全防线
安全·企业微信
zskj_zhyl7 小时前
七彩喜智慧养老:科技向善,让“养老”变“享老”的智慧之选
大数据·人工智能·科技·物联网·机器人
微盛企微增长小知识7 小时前
企业微信AI怎么用才高效?3大功能+5个实操场景,实测效率提升50%
人工智能·企业微信