请直接看原文:

【Java开发】Java实现调用微信机器人,发送企业微信通知_java 企业微信推送机器人消息-CSDN博客

-------------------------------------------------------------------------------------------------------------------------------

企业微信机器人发送消息

一、可能需要的依赖

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.5</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.9.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.9.3</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.1.23</version>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>

二、机器人地址号查看

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

三、效果展示

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

在这里插入图片描述

获取返回值

在这里插入图片描述

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

四、具体代码

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

package TEST3;

import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.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 "发送微信机器人消息失败";
    }
}

 

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐