赞
踩
1.先在群里添加机器人,然后获取机器人的webhook地址:
假设webhook是:https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=693a91f6-7xxx-4bc4-97a0-0ec2sifa5aaa
2.有多种方式发送群消息,可以采用curl,也可以采用发送https请求的方式,我这里采用okhttp发送http请求,pom如下:
<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.0</version>
</dependency>
调用okhttp核心代码如下:
/** * @content:要发送的消息 * WECHAT_GROUP:机器人的webhook */ public static String callWeChatBot(String content) { OkHttpClient client = new OkHttpClient() .newBuilder() .build(); MediaType mediaType = MediaType.parse("application/json"); RequestBody body = RequestBody.create(content,mediaType); Request request = new Request.Builder() .url(WECHAT_GROUP) .method("POST", body) .addHeader("Content-Type", "application/json") .build(); Response response
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。