插件实现了钉钉和企业微信推送消息

本文详细介绍了如何开发一个消息推送插件,支持钉钉和企业微信的消息推送。首先,需要在钉钉和企业微信后台创建应用,获取必要的配置参数如agentId、appKey和appSecret。接着,通过调用API获取Token,再获取用户信息,最后发送消息。整个过程涵盖了从获取授权到实际推送的完整步骤,适用于企业内部通讯系统的集成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目需要开发一个消息推送插件,今天整理一下做一个记录。

消息推送插件实现了钉钉和企业微信推送消息的功能。

首先介绍的是钉钉的实现方式:

1.需要在钉钉后台创建一个应用,并且需要记录下agentId、appKey、appSecret三个值,具体的做法不会的请移步下面的链接;
钉钉开放平台:https://developers.dingtalk.com/document/;

2.获取钉钉的Token;

 public static OapiGettokenResponse getToken(String appKey, String appSecret) {
   
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
        OapiGettokenRequest request = new OapiGettokenRequest();
        request.setAppkey(appKey);
        request.setAppsecret(appSecret);
        request.setHttpMethod("GET");
        try {
   
            OapiGettokenResponse response = client.execute(request);
            return response;
        } catch (ApiException | com.taobao.api.ApiException e) {
   
            e.printStackTrace();
        }
        return null;
    }

3.获取钉钉用户信息;

public static OapiV2UserGetbymobileResponse getByMobile(String appKey, String appSecret,String phone) {
   
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getbymobile");
        OapiV2UserGetbymobileRequest req = new OapiV2UserGetbymobileRequest();
        req.setMobile(phone);
        OapiGettokenResponse oapiGettokenResponse =getToken(appKey,appSecret);
        String accessToken = oapiGettokenResponse.getAccessToken();
        try {
   
            OapiV2UserGetbymobileResponse rsp = client.execute(req, accessToken);
            return rsp;
        } catch (ApiException | com.taobao.api.ApiException e) {
   
            e.printStackTrace();
        }
        return null;
    }

4.推送钉钉消息;

public static OapiMessageCorpconversationAsyncsendV2Response asyncsendV2(Integer agentId, String appKey, String appSecret, String user, String content) {
   
        OapiV2UserGetbymobileResponse oapiV2UserGetbymobileResponse = getByMobile(appKey, appSecret,user);
        if (!oapiV2UserGetbymobileResponse.isSuccess()) {
   
            log.error(oapiV2UserGetbymobileResponse.getErrmsg());
            return null;
        }
        OapiV2UserGetbymobileResponse.UserGetByMobileResponse result = oapiV2UserGetbymobileResponse.getResult();

        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值