企业微信消息推送
基本概念: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标签也显示在文本中,这是企业微信消息的渲染规则
- 获取acces_token ,并缓存到redis中
- 发送消息
@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