当前位置: 首页 > news >正文

企业微信消息推送

基本概念:https://developer.work.weixin.qq.com/document/path/90665
推送消息:https://developer.work.weixin.qq.com/document/path/90236#10112
获取access_token: https://developer.work.weixin.qq.com/document/path/91039
在这里插入图片描述
这个看着是a标签,好像是html,其实并不是通用的html,而是企业微信在text类型消息 里面做的特殊扩展,
只能像他官方例子那样写,,,如果将 双引号 变成 单引号,,消息不回被推送过去,,如果加了 style设置了样式,,a标签就不会解析,,会将a标签也显示在文本中,这是企业微信消息的渲染规则


  1. 获取acces_token ,并缓存到redis中
  2. 发送消息
@Component
@ConfigurationProperties(prefix = "wechat")
public class WechatConfig {private String corpId;private String secret;private Integer agentId;// h5 oauth2授权地址  http://oauth2.water-kid.xyz/social-login  http://localhost:8080/#/pages/mine/oauth2-login/oauth2-loginprivate String oauth2RedirectUrl;public String getOauth2RedirectUrl() {return oauth2RedirectUrl;}public void setOauth2RedirectUrl(String oauth2RedirectUrl) {this.oauth2RedirectUrl = oauth2RedirectUrl;}// Getters and Setterspublic String getCorpId() {return corpId;}public void setCorpId(String corpId) {this.corpId = corpId;}public String getSecret() {return secret;}public void setSecret(String secret) {this.secret = secret;}public Integer getAgentId() {return agentId;}public void setAgentId(Integer agentId) {this.agentId = agentId;}
}
package org.btf.bawzh.messageDocking.wechat.service;import org.btf.bawzh.messageDocking.wechat.config.WechatConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;@Service
public class WechatMessageService {@Autowiredprivate WechatConfig wechatConfig;@Autowiredprivate RestTemplate restTemplate;/*** 发送文本消息到企业微信* @param phoneNumber 接收者用户ID* @param content 消息内容* @return 发送结果*/public Map<String, Object> sendTextMessage(String phoneNumber, String content) {// 获取access_tokenString accessToken = this.getAccessToken();String touser=this.getUserIdByMobile(phoneNumber,accessToken);// 构建请求URLString url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + accessToken;// 构建请求体Map<String, Object> requestBody = new HashMap<>();requestBody.put("touser", touser);requestBody.put("msgtype", "text");requestBody.put("agentid", wechatConfig.getAgentId());Map<String, String> textContent = new HashMap<>();textContent.put("content", content);requestBody.put("text", textContent);requestBody.put("safe", 0);// 发送POST请求Map<String, Object> response = restTemplate.postForObject(url, requestBody, Map.class);return response;}/*** 获取企业微信的access_token*/public String getAccessToken() {String TOKEN_URL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";Map<String, String> params = new HashMap<>();params.put("corpid", wechatConfig.getCorpId());params.put("corpsecret", wechatConfig.getSecret());// 构建请求URLString url = TOKEN_URL + "?corpid=" + wechatConfig.getCorpId() + "&corpsecret=" + wechatConfig.getSecret();// 发送GET请求Map<String, Object> response = restTemplate.getForObject(url, Map.class);// 从响应中获取access_tokenif (response != null && response.containsKey("access_token")) {return (String) response.get("access_token");} else {throw new RuntimeException("Failed to get access token: " + response);}}public String getUserIdByMobile(String mobile,String accessToken) {String USERID_BY_MOBILE_URL = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserid";// 构建请求URLString url = USERID_BY_MOBILE_URL + "?access_token=" + accessToken;// 构建请求体Map<String, String> requestBody = new HashMap<>();requestBody.put("mobile", mobile);// 发送POST请求Map<String, Object> response = restTemplate.postForObject(url, requestBody, Map.class);// 从响应中获取useridif (response != null && response.containsKey("userid")) {return (String) response.get("userid");} else {throw new RuntimeException("Failed to get userid by mobile: " + response);}}}

引用:https://blog.csdn.net/xh2550417582/article/details/149250505
引用:https://blog.csdn.net/TinpeaV/article/details/149028197


文章转载自:

http://2jktM0Tu.pdtjj.cn
http://rEZRmrAa.pdtjj.cn
http://wUCZSY5x.pdtjj.cn
http://cpV3JXjK.pdtjj.cn
http://DkRSai3S.pdtjj.cn
http://SOEPjMeZ.pdtjj.cn
http://914YY8l3.pdtjj.cn
http://s5TUrkV0.pdtjj.cn
http://YoxDHDoT.pdtjj.cn
http://nSZdA3NZ.pdtjj.cn
http://J5JJ9Lao.pdtjj.cn
http://Ql4aWazf.pdtjj.cn
http://9cXgyc8b.pdtjj.cn
http://pKQzJuns.pdtjj.cn
http://GIipb2Tx.pdtjj.cn
http://H1Xl2DYP.pdtjj.cn
http://1E2DImOx.pdtjj.cn
http://U8dv9RQC.pdtjj.cn
http://pEuJ6Qme.pdtjj.cn
http://JJzu4Bej.pdtjj.cn
http://Y7BGSWUE.pdtjj.cn
http://PEnSUTUn.pdtjj.cn
http://PEB4q7ew.pdtjj.cn
http://oRjrxCWE.pdtjj.cn
http://P5JczRUR.pdtjj.cn
http://HYboNBZM.pdtjj.cn
http://mzq75kHW.pdtjj.cn
http://mB9mwdWN.pdtjj.cn
http://F56Frwih.pdtjj.cn
http://wDqaWJwJ.pdtjj.cn
http://www.dtcms.com/a/377528.html

相关文章:

  • 顺序表:数据结构中的基础线性存储结构
  • 什么是X11转发?
  • OpenCV计算机视觉实战(24)——目标追踪算法
  • 4.2 I2C通信协议
  • Spring Boot 读取 YAML 配置文件
  • 【系统分析师】第20章-关键技术:微服务系统分析与设计(核心总结)
  • SAP-MM:SAP MM模块精髓:仓储地点(Storage Location)完全指南图文详解
  • Shell脚本周考习题及答案
  • 广东省省考备考(第九十六天9.10)——言语(刷题巩固第二节课)
  • Pthread定时锁与读写锁详解
  • Go模块自动导入教学文档
  • 技术文章大纲:开学季干货——知识梳理与经验分享
  • TensorFlow平台介绍
  • Vue3 中实现按钮级权限控制的最佳实践:从指令到组件的完整方案
  • 生成模型与概率分布基础
  • Cookie之domain
  • JavaSSM框架-MyBatis 框架(五)
  • 中州养老:设备管理介绍
  • 【Day 51|52 】Linux-tomcat
  • MySQL - 如果没有事务还要锁吗?
  • “高德点评”上线,阿里再战本地生活
  • JUC的常见类、多线程环境使用集合类
  • 《C++ 108好库》之1 chrono时间库和ctime库
  • C++篇(7)string类的模拟实现
  • 弱加密危害与修复方案详解
  • 【Linux】Linux常用指令合集
  • Android- Surface, SurfaceView, TextureView, SurfaceTexture 原理图解
  • 如何设计Agent 架构
  • MySQL主从不一致?DBA急救手册:14种高频坑点+3分钟定位+无损修复!
  • 拍我AI:PixVerse国内版,爱诗科技推出的AI视频生成平台