浙政钉工作通知消息接口对接详细步骤

本文介绍了如何通过阅读钉钉官方文档,配置DingDingConfig,以及使用浙政钉的消息通知接口进行工作通知的发送,包括设置接收者、构造请求参数和测试结果的展示。
摘要由CSDN通过智能技术生成

首先详细阅读钉钉官方文档。查阅你所需要的服务端 API。

专有钉钉门户icon-default.png?t=N7T8https://openplatform-portal.dg-work.cn/portal/#/helpdoc?apiType=QUICK_START&docKey=3355321例如这里我对接的是浙政钉的消息通知接口,可查官方文档看具体位置,如图:

这是我的 demo,注意:发送通知有很多种形式,我这里是以卡片形式展示,其他可参考官方文档。

1.相关配置文件

@ToString
public class DingDingConfig {

    // appKey
    public static String appKey = "";

    // 浙政钉域名  openplatform-pro.ding.zj.gov.cn
    public static String domainName = "";

    // appSecret
    public static String appSecret = "";
}

2.工作通知测试接口,注意:这里入参很多,只需要填写必须的即可,不需要全部填写。如图:

3.我这里的 accountId 作为每个用户的唯一标识符,即发送通知的对象。 消息通知接口测通后即可进行开发使用。

/**
     * 消息通知
     *
     * @param accountId
     * @return
     */
    public Object getMessage(String accountId) {
        Long time = System.currentTimeMillis();
        String msgID = String.valueOf(time);
        ExecutableClient executableClient = ExecutableClient.getInstance();
        executableClient.setDomainName(DingDingConfig.domainName);
        executableClient.setProtocal("https");
        executableClient.setAccessKey(DingDingConfig.otherAppKey);
        executableClient.setSecretKey(DingDingConfig.otherAppSecret);
        executableClient.init();

        // executableClient保证单例
        IntelligentPostClient intelligentPostClient = executableClient.newIntelligentPostClient("/message/workNotification");
        OapiMessageWorkNotificationRequest oapiMessageWorkNotificationRequest = new OapiMessageWorkNotificationRequest();
        // 接收者的部门id列表
//        oapiMessageWorkNotificationRequest.setOrganizationCodes("字符串");
        // 接收人用户ID
        oapiMessageWorkNotificationRequest.setReceiverIds(accountId);
        // 租户ID
        oapiMessageWorkNotificationRequest.setTenantId("对应的租户id");
        // 业务消息id,注意这里的msgID,每次通知都需要不一样的msgID参数,如果重复会提示本条内容已重复发送等相关内容,因此我这里设置为每次发送通知的时间
        oapiMessageWorkNotificationRequest.setBizMsgId(msgID);
        // 设置消息对象
        String msg;
        msg = "{\r\n"
                + "    \"msgtype\": \"action_card\",\r\n"
                + "    \"action_card\": {\r\n"
                + "        \"title\": \"工作通知\",\r\n"
                + "        \"markdown\": \"您有一条新的审批通知\",\r\n"
                + "        \"single_title\": \"查看详情点击打开链接\",\r\n"
                + "        \"single_url\": \"http://域名:端口/xxxx.html?ddtab=true\", \r\n"
                + "        \"single_pc_url\": \"http://域名:端口/xxxx.html?ddtab=true\" \r\n"
                + "    }\r\n"
                + "}";
        oapiMessageWorkNotificationRequest.setMsg(msg);
        OapiMessageWorkNotificationResponse apiResult = intelligentPostClient.post(oapiMessageWorkNotificationRequest);
        System.out.println("apiResult" + JSON.toJSON(apiResult));
        return apiResult;
    }

4.测试结果:浙政钉消息通知会收到如图所示的卡片通知消息:

 

注意:这里我的查看详情可跳转链接http://域名:端口/xxxx.html?ddtab=true,后面添加参数ddtab=true即可在浙政钉内部跳转打开进第三方网址。

如图:

 

对接微信公众号模板消息接口,需要进行以下步骤: 1. 在微信公众平台上创建模板消息,获取模板 ID 和模板消息中需要填充的关键词。 2. 在 PHP 代码中编写发送模板消息的代码,包括获取 access_token 和发送模板消息。 3. 在用户触发某些事件时,调用 PHP 发送模板消息的代码。 以下是一个简单的 PHP 示例代码: ```php // 获取 access_token function getAccessToken($appID, $appSecret) { $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appID}&secret={$appSecret}"; $result = file_get_contents($url); $json = json_decode($result, true); if (isset($json['access_token'])) { return $json['access_token']; } else { return false; } } // 发送模板消息 function sendTemplateMessage($accessToken, $openID, $templateID, $data, $url = '') { $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$accessToken}"; $data = [ 'touser' => $openID, 'template_id' => $templateID, 'url' => $url, 'data' => $data ]; $data = json_encode($data, JSON_UNESCAPED_UNICODE); $result = http_post_data($url, $data); $json = json_decode($result, true); if (isset($json['errcode']) && $json['errcode'] == 0) { return true; } else { return false; } } // 发送 POST 请求 function http_post_data($url, $data_string) { $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string)) ); ob_start(); curl_exec($ch); $result = ob_get_contents(); ob_end_clean(); curl_close($ch); return $result; } // 示例代码 $appID = 'your-app-id'; $appSecret = 'your-app-secret'; $accessToken = getAccessToken($appID, $appSecret); $openID = 'your-open-id'; $templateID = 'your-template-id'; $data = [ 'first' => ['value' => 'Hello World!', 'color' => '#173177'], 'keyword1' => ['value' => 'value1', 'color' => '#173177'], 'keyword2' => ['value' => 'value2', 'color' => '#173177'], 'remark' => ['value' => 'This is a remark.', 'color' => '#173177'] ]; $url = 'http://example.com'; $result = sendTemplateMessage($accessToken, $openID, $templateID, $data, $url); if ($result) { echo '发送成功!'; } else { echo '发送失败!'; } ``` 请注意,以上代码仅作为示例,需要根据实际情况进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值