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

一、可能需要的依赖

        <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 "发送微信机器人消息失败";
    }
}

  • 5
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Why_so?

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值