如何对接钉钉发送消息

Angel

效果

image.png

接口调用流程

如下图所示,在调用钉钉服务端接口前,您需要完成以下准备工作:

  1. 添加接口调用权限。应用创建后默认只开放登录和消息通知接口的调用权限,您需要根据开发需要,添加对应的接口使用权限。详细信息请参考添加接口调用权限
  2. 获取应用的access_token。access_token相当于是身份凭证。调用接口时,通过access_token来鉴权调用者身份。

调用方式

钉钉开放平台提供了API Explorer和SDK方便开发者调用服务端API。

配置钉钉

登录钉钉开放平台

网址: https://open-dev.dingtalk.com/#/index
注意: 登录账号需要企业管理员

创建应用

点击头部的应用开发-> 企业内部开发->H5微应用->创建应用

填写应用名称和应用描述 开发方式选择-> 企业自助开发
最后点击确定创建

配置权限

您需要根据要开发的应用,添加需要使用的接口权限。
参考以下操作,添加接口权限:

  1. 登录开发者后台
  2. 应用开发页面,单击目标应用进入应用详情页面。
  3. 在应用详情页,单击接口权限,然后单击添加接口权限。
  4. 在弹出的页面,选择要添加的接口,然后单击确认注意添加通讯录接口权限后,需配置通讯录授权范围,即通讯录相关接口可以获取哪些部门员工的通讯录信息。开发者在调用通讯录接口时,只能获取到在授权范围内的员工通讯录信息。获取非授权范围内的员工通讯录信息会提示错误。详情请参见设置通讯录权限范围。

企业会话、手机号获取userid 必须添加

配置服务器出口ip

调用钉钉API/SDK 需要让钉钉服务器信任调用方
参考以下操作,添加服务器ip:

  1. 点击刚刚创建好的H5微应用
  2. 点击开发管理选项
  3. 修改其中的配置


注意服务器出口IP为调用方程序部署的服务器IP,需要填写上才有权限调用钉钉的API/SDK
应用首页地址可以填写自己的公司官网,如果没有则填写https://www.dingtalk.com即可

应用凭证

拿到应用凭证即可开始开发

编写代码

发送消息需要的接口如下

  1. 获取企业内部access_token接口 这个是必须的
  2. 根据手机号获取userid
  3. 发送工作通知
package fun.angelive.webhook.service;

import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.dingtalk.api.response.OapiServiceGetCorpTokenResponse;
import com.dingtalk.api.response.OapiV2UserGetbymobileResponse;

public interface IDingDingService {
    /**
     * 钉钉获取企业内部access_token
     * @return OapiGettokenResponse
     */
    OapiGettokenResponse getToken();

    /**
     * 发送工作通知
     * @return OapiMessageCorpconversationAsyncsendV2Response
     */
    OapiMessageCorpconversationAsyncsendV2Response asyncsendV2();

    /**
     * 根据手机号获取userid
     * @param phone 手机号
     * @return OapiV2UserGetbymobileResponse
     */
    OapiV2UserGetbymobileResponse getByMobile(String phone);
}

接着写实现类

package fun.angelive.webhook.service.impl;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.dingtalk.api.request.OapiServiceGetCorpTokenRequest;
import com.dingtalk.api.request.OapiV2UserGetbymobileRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.dingtalk.api.response.OapiServiceGetCorpTokenResponse;
import com.dingtalk.api.response.OapiV2UserGetbymobileResponse;
import com.taobao.api.ApiException;
import fun.angelive.webhook.service.IDingDingService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@AllArgsConstructor
public class DingDingServiceImpl implements IDingDingService {

    @Override
    public OapiGettokenResponse getToken() {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
        OapiGettokenRequest request = new OapiGettokenRequest();
        request.setAppkey("yourAppkey");
        request.setAppsecret("yourAppsecret");
        request.setHttpMethod("GET");
        try {
            OapiGettokenResponse response = client.execute(request);
            return response;
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public OapiMessageCorpconversationAsyncsendV2Response asyncsendV2() {
        OapiV2UserGetbymobileResponse oapiV2UserGetbymobileResponse = getByMobile("yourPhone");
        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");
        OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
        // yourAgentId
        request.setAgentId(1000000L);
        request.setUseridList(result.getUserid());
        request.setToAllUser(false);

        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        msg.setMsgtype("text");
        msg.setText(new OapiMessageCorpconversationAsyncsendV2Request.Text());
        msg.getText().setContent("我要下班啦");
        request.setMsg(msg);

        OapiGettokenResponse oapiGettokenResponse = this.getToken();
        String accessToken = oapiGettokenResponse.getAccessToken();
        try {
            OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(request, accessToken);
            return rsp;
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public OapiV2UserGetbymobileResponse getByMobile(String phone) {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getbymobile");
        OapiV2UserGetbymobileRequest req = new OapiV2UserGetbymobileRequest();
        req.setMobile(phone);

        OapiGettokenResponse oapiGettokenResponse = this.getToken();
        String accessToken = oapiGettokenResponse.getAccessToken();
        try {
            OapiV2UserGetbymobileResponse rsp = client.execute(req, accessToken);
            return rsp;
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return null;
    }
}

最后写一个Controller 调用Service方法即可实现钉钉发送消息

阅读 2.3k
43 声望
0 粉丝
0 条评论
你知道吗?

43 声望
0 粉丝
文章目录
宣传栏