Spring项目实现企业微信消息推送

个人理解

首先拥有一个可以用于测试的企业微信,获取到

企业的id 【cropID】
企业应用的id【agentID】
管理组的凭证秘钥【corpSecret】

其次是创建用于发送请求的封装类,详细的字段及说明请参考 官方文档
在这里插入图片描述
建立封装类后需要做的只有两步 —>
1.发起指定 URL 的 GET 请求来获取token

https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid= " 输入你的企业ID “&corpsecret=” 管理组的凭证秘钥 "

2.将GET请求获取到的 token 取出,放入到指定 URL 的 PSET 请求中 发送需要推送的消息和接受消息的企业微信成员账号

https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token= “GET请求获取到的 token”

实作范例

配置 application.yml

  weixin: # 这组是测试用的配置, 用户要加入 luce 的企业微信群组才能正常使用
  	url: https://qyapi.weixin.qq.com
    corpid: wwda5771bac49cd769 # 企业ID
    corpsecret: WCtyURlQUa91EjxMb4cc9axpaam40hXkXBf3m_INi_0  # 凭证秘钥
    agentid: 1000002 # 应用ID

Dto类

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import javax.validation.constraints.NotBlank;


@ApiModel(description = "消息创建DTO")
public class MessageCreateDto {

    @ApiModelProperty(value = "成员ID列表(消息接收者,最多支持1000个)。每个元素的格式为: corpid/userid,其中,corpid为该互联成员所属的企业,userid为该互联成员所属企业中的帐号。如果是本企业的成员,则直接传userid即可")
    private String toUser;

    @NotBlank
    @ApiModelProperty(value = "消息内容,最长不超过2048个字节", required = true)
    private String content;

    public String getToUser() {
        return toUser;
    }

    public void setToUser(String toUser) {
        this.toUser = toUser;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

Servuce层


import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.ione.base.exception.IllegalApiUsageException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * 企业微信API. QyApi stand for 企業 API
 */
@Service
public class QyApiService {

    private Logger logger = LoggerFactory.getLogger(QyApiService.class);

    private String url;
    private String corpid;
    private String corpsecret;
    private String agentid;

    private LoadingCache<String,String> tokenCache = CacheBuilder.newBuilder()
            .maximumSize(1)
            .expireAfterWrite(3600,TimeUnit.SECONDS) // weixin token expire in 7200 sec
            .build(new CacheLoader<String, String>() {
                @Override
                public String load(String s) throws Exception {
                    return getToken();
                }
            });

	/**
	 * ${ione.weixin.url} 用来获取配置中的固定值
	 *
	 */
    @Value(value = "${ione.weixin.url}")
    public void setUrl(String url) {
        this.url = url;
    }

    @Value(value = "${ione.weixin.corpid}")
    public void setCorpid(String corpid) {
        this.corpid = corpid;
    }

    @Value(value = "${ione.weixin.corpsecret}")
    public void setCorpsecret(String corpsecret) {
        this.corpsecret = corpsecret;
    }

    @Value(value = "${ione.weixin.agentid}")
    public void setAgentid(String agentid) {
        this.agentid = agentid;
    }

	/**
	 *获取token 的方法
	 *
	 */
    protected String getToken() {
        String apiUrl = String.format("%s/cgi-bin/gettoken?corpid=%s&corpsecret=%s", url, corpid, corpsecret);
        RestTemplate template = new RestTemplate();
        Map<String, Object> result = template.getForObject(apiUrl, Map.class);
        String token = (String) result.get("access_token");
        logger.debug("[QY] token url:  {}", apiUrl);
        logger.info("[QY] token result: {}", result);
        int errcode = (int) result.getOrDefault("errcode", 0);
        String errmsg = (String) result.getOrDefault("errmsg", 0);
        if (errcode != 0) {
            throw new IllegalApiUsageException(errmsg);
        }
        return token;
    }

    /**
     * 发送消息. https://open.work.weixin.qq.com/api/doc/90000/90135/90250
     *
     * @param toUser  成员ID列表(消息接收者,最多支持1000个)。每个元素的格式为: corpid/userid,其中,corpid为该互联成员所属的企业,userid为该互联成员所属企业中的帐号。如果是本企业的成员,则直接传userid即可
     * @param content 消息内容,最长不超过2048个字节
     * @return
     */
    public Map<String, Object> sendMessage(String toUser, String content) {
        String token = "";
        try {
            token = this.tokenCache.get("TOKEN");
        } catch (ExecutionException e) {
            logger.error("get token from cache fail", e);
        }
        String apiUrl = String.format("%s/cgi-bin/message/send?access_token=%s", url, token);
        Map<String, Object> params = new HashMap<>();
        params.put("touser", toUser);
        params.put("msgtype", "text");
        params.put("agentid", agentid);
        params.put("safe", 0);
        params.put("text", Map.of("content", content));
        RestTemplate template = new RestTemplate();
        logger.debug("[QY] send message url: {}", url);
        logger.info("[QY] send message param: {}", params);
        Map<String, Object> result = template.postForObject(apiUrl, params, Map.class);
        logger.info("[QY] send message result: {}", result);
        return result;
    }
}

Controller层

import com.ione.weixin.domain.dto.MessageCreateDto;
import com.ione.weixin.service.QyApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Map;

@Api(tags = "企业微信API")
@RestController
public class WeixinController {

    private QyApiService service;

    @Autowired
    public void setService(QyApiService service) {
        this.service = service;
    }

    @ApiOperation(value = "发送消息. https://open.work.weixin.qq.com/api/doc/90000/90135/90250")
    @PostMapping("weixin/messages")
    Map<String, Object> sendMessage(@RequestBody @Valid MessageCreateDto dto) {
        return service.sendMessage(dto.getToUser(), dto.getContent());
    }
}

调用示范

/**
     * 企业微信推送:内容
     */
    private final StringBuffer content = new StringBuffer();
    /**
     * 企业微信推送:账号
     */
    private final StringBuffer userNo = new StringBuffer();
	/**
	 * 在业务逻辑中通过 StringBuffer 的 append() 方法添加需要通知的用户账号和通知内容
	 */
	//发送企业微信通知
     if (content.length() > 0 && !StringUtils.isEmpty(userNo)) {
         weixinClient.sendMessage(Map.of("toUser", userNo, "content", content));
     }
     //清除字符缓存
     content.delete(0, content.length());
     userNo.delete(0, userNo.length());
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Spring Boot中实现微信小程序消息推送可以参考以下步骤: 1. 配置微信小程序的AppID和AppSecret等信息,可以在application.properties或application.yml文件中配置。 2. 创建一个发送消息的接口,可以使用Restful API实现,接口的具体实现可以参考微信开发者文档。 3. 在接口实现中,需要获取access_token,可以通过调用微信提供的获取access_token的接口实现,具体可以参考微信开发者文档。 4. 获取access_token后,就可以调用微信提供的发送模板消息或客服消息的接口实现消息推送了。 下面是一个简单的示例: ```java @RestController public class MessageController { @Value("${wx.appId}") private String appId; @Value("${wx.appSecret}") private String appSecret; @GetMapping("/sendMsg") public String sendMsg(String openId, String content) { String accessToken = getAccessToken(); String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + accessToken; String json = "{\"touser\":\"" + openId + "\",\"msgtype\":\"text\",\"text\":{\"content\":\"" + content + "\"}}"; String result = HttpUtil.post(url, json); return result; } private String getAccessToken() { String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret; String result = HttpUtil.get(url); JSONObject json = JSON.parseObject(result); return json.getString("access_token"); } } ``` 在这个例子中,我们使用了阿里巴巴的fastjson库来处理JSON数据,HttpUtil是一个自己实现的HTTP请求工具类。需要注意的是,在实际使用中,需要根据具体业务需求来调整消息内容和接口调用方式。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值