企业微信机器人发送消息

  • 一、可能需要的依赖
  • 二、必须的信息
  • 三、效果展示
  • 四、具体代码

一、可能需要的依赖

之前导依赖的时候没有特别标记,现在分不清哪个是哪个了,应该就在这些里面

        <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号和密钥号,登录企业微信管理网页进行查看


还需要企业的ID号

三、效果展示

输如需要发送的消息

获取返回值

企业微信收到消息

四、具体代码

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

1、这是主要实现的类

package TEST2;import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
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 org.springframework.stereotype.Service;import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;@Service
public class SendMsg {private static CloseableHttpClient httpClient;private static HttpPost httpPost;//用于提交登陆数据private static HttpGet httpGet;//用于获得登录后的页面//CorpID  企业ID//AGENTID 应用的ID//Secret 应用的ID对应的密钥public static final String CONTENT_TYPE = "Content-Type";public static final Integer AGENTID = 1000002;public static final String CORPID = "wwf38c66706451ab6d";public static final String CORPSECRET = "S1-z2PjSX0BSUn8U9rXjWqoo6Ix56lqaH5JD0gAf63Q";static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//private static Gson gson = new Gson();/**** @param toUser 用户的ID 格式"UserID1|UserID2|UserID3"* @param contentValue 推送消息内容* @throws IOException*/public static void sendTextMesg(String toUser, String contentValue) throws IOException {String token = getToken(CORPID,CORPSECRET);String postData = createPostData(toUser, "text",AGENTID, "content", contentValue);String response = post("utf-8", CONTENT_TYPE,"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=", postData, token);System.out.println("获取到的token======>" + token);System.out.println("请求数据======>" + postData);System.out.println("发送微信的响应数据======>" + response);}public static String getToken(String corpId, String corpSecret) throws IOException {String resp = toAuth( "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpId + "&corpsecret=" + corpSecret);//拼接字符串得到urlMap<String, Object> map = gson.fromJson(resp,new TypeToken<Map<String, Object>>() {}.getType());System.out.println(map);return map.get("access_token").toString();}protected static String toAuth(String Get_Token_Url) throws IOException {httpClient = HttpClients.createDefault();httpGet = new HttpGet(Get_Token_Url);CloseableHttpResponse response = httpClient.execute(httpGet);System.out.println(response.toString());String resp;try {HttpEntity entity = response.getEntity();System.out.println(response.getAllHeaders());resp = EntityUtils.toString(entity, "utf-8");EntityUtils.consume(entity);} finally {response.close();}return resp;}private static String createPostData(String touser, String msgtype, int agent_id, String contentKey, String contentValue) {Map<String,Object> weChatData = new HashMap<>();weChatData.put("touser",touser);weChatData.put("agentid",agent_id);weChatData.put("msgtype",msgtype);Map<Object, Object> content = new HashMap<Object, Object>();content.put(contentKey, contentValue + "\n--------\n" + df.format(new Date()));weChatData.put("text",content);System.out.println(gson.toJson(weChatData));return gson.toJson(weChatData);}private static String post(String charset, String contentType, String url, String data, String token) throws IOException {CloseableHttpClient httpclient = HttpClients.createDefault();httpPost = new HttpPost(url + token);httpPost.setHeader(CONTENT_TYPE, contentType);httpPost.setEntity(new StringEntity(data, charset));CloseableHttpResponse response = httpclient.execute(httpPost);String resp;try {HttpEntity entity = response.getEntity();resp = EntityUtils.toString(entity, charset);EntityUtils.consume(entity);} finally {response.close();}return resp;}
}

2、执行的类

package TEST2;import java.io.IOException;
import java.util.Scanner;public class Test {public static void main(String[] args) throws IOException {String user = "xxxx";Scanner scan = new Scanner(System.in);String content = scan.nextLine();
//        String content = "你好呀~";SendMsg. sendTextMesg(user, content);}
}

【Java开发】Java实现企业微信消息推送,通过应用发送相关推荐

  1. Spring Boot 实现企业微信消息推送

    1 Maven依赖 <!-- 阿里JSON解析器 --><dependency><groupId>com.alibaba</groupId><ar ...

  2. 企业微信消息推送接口大全

    该类存放接口所用到的企业微信的接口地址 package com.common;public final class QyapiUrl {/*** 请求nginx转发服务器地址*/public stat ...

  3. Node-Red 实践:企业微信消息推送

    一个偶然的机会,认识了node-red.这种拖拽控件编写代码的方式给了我很深刻的印象.能够通过简单的拖拽实现mqtt,http,websocket,tcp的服务,作为基于网络的业务流程demon非常方 ...

  4. 企业微信消息推送卡片按钮互动的使用

    企业微信回调推送消息互动模板 前置条件 # 企业微信后台中设置了url 按以上设置 # 回调url: http://www.baidu.com# token: xxxxx# EncodingAESKe ...

  5. (二)企业微信消息推送

    1.所需信息 CorpID:企业ID AgentID:应用ID Secret:应用密钥 2.功能实现 参考内容:企业微信-发送应用消息 public static class QYWXHelper{/ ...

  6. 【corpwechat-bot】一个好用的企业微信消息推送python接口库

    文章目录 前言 Requirements Result Features Usage 前言 本文同步发表在blog.gentlecp.com corpwechat-bot是一个python封装的企业机 ...

  7. 企业微信消息推送(一)接收消息服务器URL

    1.点击左上角头像,打开微信管理平台 2.创建应用 3.获取五个参数 3.1获取应用的AgentId.Secret 3.2获取企业ID 配置接收消息服务器URL 企业微信限制过多,公司域名所有权检验不 ...

  8. C# 企业微信消息推送对接,实现天气预报推送

    准备工作 需要获取的东西1. 企业Id,2.应用secret 和 应用ID 获取企业id 注册完成后,在我的企业=>企业信息=>最下面企业id 获取应用secret 和  应用ID 发送微 ...

  9. 微信接收不到企业微信消息推送

    使用微信提供的接口,给公司微信企业号的相关人员推送消息时. 一般是微信端的企业号和企业微信都能接收到推送的消息,但有一次企业微信版本更新后发现微信端的推送消息没有接收到. 原因找了很久,最终发现在企业 ...

最新文章

  1. R语言可视化包ggplot2绘制线性回归模型曲线实战( Linear Regression Line)
  2. php 位深度,javascript - 流程图获取深度,求各位算法高手帮帮忙
  3. Qt QML实现阴影字体
  4. #pragma pack(n)和sizeof求结构的大小
  5. jsp iframe嵌入php,jsp嵌套iframe从iframe中表单提交并传值到外层_html/css_WEB-ITnose
  6. 买了服务器之后如何操作系统,买了服务器之后如何操作系统
  7. python xlsxwriter dict_python_xlsxwriter模块
  8. android 多点
  9. php+mysql将大数据sql文件导入数据库
  10. 扇贝有道180923每日一句
  11. Linux 进程间通信 无名管道(pipe)
  12. 泛微oa连接mysql,泛微OA 数据库维护笔记(e-cology)
  13. 考公 | 张小龙讲申论(2019地市级真题)
  14. Python版蚊子大作战源代码,超解压的灭蚊小游戏,多种道具
  15. leapftp怎么用,掌握leapftp的使用方法只需6步
  16. Android Glide加载网络图片不显示,但浏览器能打开图片
  17. esp8266,esp32中的SPI FLASH 访问模式(QIO QOUT DIO DOUT)
  18. 换行标签<br>和水平线标签<hr>
  19. window安装kubectl管理远程k8s集群
  20. DeFi:解决 EIP-4626 中的滑点问题

热门文章

  1. latex sign_我抛弃LaTex排版的若干个原因(Overleaf在线排版工具推荐 )
  2. 大型网站技术架构的前十个脚印
  3. vue路由(十二)路由独享守卫beforeEnter和页面内组件路由守卫:beforeRouteEnter,beforeRouteLeave,beforeUpdate
  4. 使用JS实现无刷新读取json接口数据
  5. 五分钟让你搞懂Nginx负载均衡原理及四种负载均衡算法
  6. 球面镜的焦距计算公式
  7. osgEarth示例分析——osgearth_drawables
  8. css自动换行加前置_CSS实现自动换行的技巧
  9. 利用Excel实现度分秒转换十进制度
  10. Ubuntu 18.04.4 禁用 nouveau