【Java开发】Java实现调用微信机器人,发送企业微信通知

您所在的位置:网站首页 java微信发送消息 【Java开发】Java实现调用微信机器人,发送企业微信通知

【Java开发】Java实现调用微信机器人,发送企业微信通知

2023-04-02 02:22| 来源: 网络整理| 查看: 265

企业微信机器人发送消息 一、可能需要的依赖 二、机器人地址号查看 三、效果展示 四、具体代码

一、可能需要的依赖 org.apache.httpcomponents httpclient 4.3.5 com.fasterxml.jackson.core jackson-databind 2.9.3 com.fasterxml.jackson.core jackson-core 2.9.3 com.fasterxml.jackson.core jackson-annotations 2.9.3 com.alibaba fastjson 1.1.23 commons-lang commons-lang 2.6 二、机器人地址号查看

新建一个群聊机器人,可以查看机器人的接口地址,不需要密钥和企业id号在这里插入图片描述

三、效果展示

在这里插入图片描述 输如需要发送的消息

在这里插入图片描述

获取返回值

在这里插入图片描述

微信收到消息在这里插入图片描述

四、具体代码

如果觉得不错就点个赞吧!!

package TEST3; import org.apache.http.HttpStatus; import org.apache.http.clienthods.CloseableHttpResponse; import org.apache.http.clienthods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import java.io.IOException; import java.util.Scanner; public class SendMsg2 { public static void main(String[] args) throws IOException { Scanner scan = new Scanner(System.in); System.out.println("请输入要发送的消息内容:"); String content = scan.nextLine(); sendText(content); } //消息体 //拼接json字符串 //可以进行其他的拓展 比如卡片形式、图片形式等 public static String sendText(String content) throws IOException { content = "{\n" + " \"msgtype\": \"text\",\n" + " \"text\": {\n" + " \"content\": \"" + content + "\"\n" + " }\n" + "}"; return send(content); } public static String send(String textMsg) throws IOException { CloseableHttpClient httpClient = HttpClients.createDefault();//实例化对象 HttpPost httpPost = new HttpPost("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=33d69c01-d962-478b-8945-288e00e2abf2");//url地址 httpPost.addHeader("Content-Type", "application/json; charset=utf-8");//发送消息的格式; StringEntity se = new StringEntity(textMsg, "utf-8");//编码转换 httpPost.setEntity(se); CloseableHttpResponse response = httpClient.execute(httpPost); //发送成功接收返回值 if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { String result = EntityUtils.toString(response.getEntity(), "utf-8"); System.out.println("发送微信机器人消息成功 " + result); return result; } else { System.out.println("发送微信机器人消息失败"); } // 关闭 httpClient.close(); response.close(); return "发送微信机器人消息失败"; } }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3