ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java 实现钉钉机器人单人私聊推送钉钉消息(invalidParameter.robotCode.notExsit机器人不存在)

2022-01-13 21:59:14  阅读:433  来源: 互联网

标签:notExsit Exception 机器人 err 私聊 Client aliyun new com


文章目录


常见问题异常:“code”:“invalidParameter.robotCode.notExsit”,“message”:“机器人不存在”
原因:没有发布应用

{
	"code":"invalidParameter.robotCode.notExsit",
	"requestid":"76DABAC0-8ED5-7622-B692-B9E083CB09E9",
	"message":"机器人不存在"
}

1、创建机器人应用

https://open-dev.dingtalk.com/fe/app#/corp/robot
在这里插入图片描述
在这里插入图片描述

2、开通机器人接口权限

在这里插入图片描述

3、一定要发布(否则无法调试调接口)

在这里插入图片描述

4、编写工具类

        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>dingtalk</artifactId>
            <version>1.1.88</version>
        </dependency>

DingTalkUtil

package com.liugx.dingtalk;

import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest;
import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenResponse;
import com.aliyun.dingtalkrobot_1_0.models.BatchSendOTOHeaders;
import com.aliyun.dingtalkrobot_1_0.models.BatchSendOTORequest;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;

/**
 * java 实现钉钉机器人单人私聊推送钉钉消息
 * -添加依赖
 * -获取应用凭证https://open-dev.dingtalk.com/apiExplorer?spm=ding_open_doc.document.0.0.3f99722fFLkPuA#/?devType=org&api=oauth2_1.0%23GetAccessToken
 * -机器人发送消息
 *
 * @author liugx
 * @version 1.0
 * @date 2022/1/13 20:42
 */
public class DingTalkUtil {

    private static String ACCESS_TOKEN = "";

    private static String APP_KEY = "你的";

    private static String APP_SECRET = "你的";

    /**
     * 初始化--权限Client
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dingtalkoauth2_1_0.Client createAuthClient() throws Exception {
        Config config = new Config();
        config.protocol = "https";
        config.regionId = "central";
        return new com.aliyun.dingtalkoauth2_1_0.Client(config);
    }


    /**
     * 初始化--机器人Client
     *
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dingtalkrobot_1_0.Client createClient() throws Exception {
        Config config = new Config();
        config.protocol = "https";
        config.regionId = "central";
        return new com.aliyun.dingtalkrobot_1_0.Client(config);
    }

    /**
     * 获取并设置最新accessToken,每两小时失效
     */
    public static void setAccessToken() throws Exception {
        com.aliyun.dingtalkoauth2_1_0.Client client = createAuthClient();
        GetAccessTokenRequest getAccessTokenRequest = new GetAccessTokenRequest()
                .setAppKey(APP_KEY)
                .setAppSecret(APP_SECRET);;
        try {
            GetAccessTokenResponse accessToken = client.getAccessToken(getAccessTokenRequest);
            //设置access_token
            ACCESS_TOKEN = accessToken.getBody().accessToken;
        } catch (TeaException err) {
            System.out.println(err.message);
        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            System.out.println(err.message);
        }
    }

    /**
     * 发送钉钉消息推送
     */
    private void sendDingPusher() throws Exception {
        setAccessToken();
        com.aliyun.dingtalkrobot_1_0.Client client = createClient();
        BatchSendOTOHeaders batchSendOTOHeaders = new BatchSendOTOHeaders();
        batchSendOTOHeaders.xAcsDingtalkAccessToken = ACCESS_TOKEN;
        BatchSendOTORequest batchSendOTORequest = new BatchSendOTORequest()
                .setRobotCode(APP_KEY)
                .setUserIds(java.util.Arrays.asList(
                        "01421230693825153972","manager6206"
                ))
                .setMsgKey("sampleMarkdown")
                .setMsgParam("{\"text\": \"hello text\",\"title\": \"hello title\"}");
        try {
            client.batchSendOTOWithOptions(batchSendOTORequest, batchSendOTOHeaders, new RuntimeOptions());
        } catch (TeaException err) {
            if (err.code.equals("InvalidAuthentication")){
                // access_token 过期或者不合法
                setAccessToken();
                sendDingPusher();
            }
        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
        }
    }

    public static void main(String[] args) throws Exception {
        new DingTalkUtil().sendDingPusher();
    }

}

5、效果

在这里插入图片描述

标签:notExsit,Exception,机器人,err,私聊,Client,aliyun,new,com
来源: https://blog.csdn.net/weixin_45621658/article/details/122483280

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有