当前位置: 首页 > news >正文

钉钉机器人

准备

参考官方文档

  • 已经完成成为钉钉开发者流程。
  • 已经完成创建应用流程。
  • 已经完成添加应用能力流程

企业内部需要企业管理员开通权限,我自己创建了一个组织,用自己的组织创建机器人就方便很多,很多权限无需单独去申请了。

创建机器人

进入钉钉开放平台,页面创建机器人,发布即可:
请添加图片描述

获取appKey, appSecret

在这里插入图片描述

获取access_token

官方文档:获取企业内部应用的accessToken
python代码:

# -*- coding: utf-8 -*-
# This file is auto-generated, don't edit it. Thanks.
import sys

from typing import List

from alibabacloud_dingtalk.oauth2_1_0.client import Client as dingtalkoauth2_1_0Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_dingtalk.oauth2_1_0 import models as dingtalkoauth_2__1__0_models
from alibabacloud_tea_util.client import Client as UtilClient


class Sample:
    def __init__(self):
        pass

    @staticmethod
    def create_client() -> dingtalkoauth2_1_0Client:
        """
        使用 Token 初始化账号Client
        @return: Client
        @throws Exception
        """
        config = open_api_models.Config()
        config.protocol = 'https'
        config.region_id = 'central'
        return dingtalkoauth2_1_0Client(config)

    @staticmethod
    def main(
        args: List[str],
    ) -> None:
        client = Sample.create_client()
        get_access_token_request = dingtalkoauth_2__1__0_models.GetAccessTokenRequest(
            app_key='<your appKey>',
            app_secret='<your appSecret>'
        )
        try:
            client.get_access_token(get_access_token_request)
            return res.body.access_token
        except Exception as err:
            if not UtilClient.empty(err.code) and not UtilClient.empty(err.message):
                # err 中含有 code 和 message 属性,可帮助开发定位问题
                pass
            return ''
            
if __name__ == '__main__':
    Sample.main(sys.argv[1:])

注意:access token每次获取后2h内有效,不能频繁请求,需要做缓存

获取userId

除了上面鉴权的token,还需要userId信息,给具体的人员发机器人消息。我是管理员,给自己发消息,直接在中查看:
请添加图片描述

发送机器人消息

官方文档:批量发送人与机器人会话中机器人消息
python代码

# -*- coding: utf-8 -*-
# This file is auto-generated, don't edit it. Thanks.
import sys

from typing import List

from alibabacloud_dingtalk.robot_1_0.client import Client as dingtalkrobot_1_0Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_dingtalk.robot_1_0 import models as dingtalkrobot__1__0_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_tea_util.client import Client as UtilClient


class Sample:
    def __init__(self):
        pass

    @staticmethod
    def create_client() -> dingtalkrobot_1_0Client:
        """
        使用 Token 初始化账号Client
        @return: Client
        @throws Exception
        """
        config = open_api_models.Config()
        config.protocol = 'https'
        config.region_id = 'central'
        return dingtalkrobot_1_0Client(config)

    @staticmethod
    def main(
        args: List[str],
    ) -> None:
        client = Sample.create_client()
        batch_send_otoheaders = dingtalkrobot__1__0_models.BatchSendOTOHeaders()
        batch_send_otoheaders.x_acs_dingtalk_access_token = '<your access token>'
        batch_send_otorequest = dingtalkrobot__1__0_models.BatchSendOTORequest(
            robot_code='<your appKey>',
            user_ids=[
                '<your user id>'
            ],
            msg_key='sampleMarkdown',
            msg_param='{"text": "hello text","title": "hello title"}'
        )
        try:
            client.batch_send_otowith_options(batch_send_otorequest, batch_send_otoheaders, util_models.RuntimeOptions())
        except Exception as err:
            if not UtilClient.empty(err.code) and not UtilClient.empty(err.message):
                # err 中含有 code 和 message 属性,可帮助开发定位问题
                pass

if __name__ == '__main__':
    Sample.main(sys.argv[1:])

效果

请添加图片描述

消息格式

官方文档:企业机器人发送消息的消息类型

相关文章:

  • C 语 言 --- 扫 雷 游 戏(初 阶 版)
  • 计算机网络——通信基础和传输介质
  • 学习记录-bootstrap-弹性盒子
  • 常⻅CMS漏洞 -DeDeCMS 获取webshell
  • TISAX认证注意事项的详细介绍
  • Floyd 算法——97. 小明逛公园
  • 30天搭建消防安全培训小程序
  • python条件判断数据(解析、筛选)
  • 《AI幻觉:完美智能的幻象与真实世界的纠错指南》
  • 10、binlog
  • 实用工具--OfficeAI 助手 v0.3.20(长期免费,2025-03-18 本地支持WPSWord联动)
  • Cyberchef实用功能之-json line格式文件美化和查询
  • 数据治理之数据仓库
  • 车辆模型——运动学模型
  • Excel(函数进阶篇):FILTER函数全解读、XLOOKUP函数全解读、UNIQUE函数、数组与数组公式
  • 企业数据孤岛的纠结与恩怨
  • 使用Pygame实现记忆拼图游戏
  • 图像滤波中常用滤波器的相位响应——不是只有零相位滤波器
  • Java 大视界 -- Java 大数据分布式计算中的通信优化与网络拓扑设计(145)
  • 家庭带宽运行PCDN
  • 跨文化戏剧的拓荒者与传承者——洪深与复旦剧社的百年回响
  • 北斗专访|特赞科技范凌:现在AI主要是“说话”,接下来要“干活”了
  • 王毅谈中拉命运共同体建设“五大工程”及落实举措
  • 周启鸣加盟同济大学,曾任香港浸会大学深圳研究院院长
  • 上海下周最高气温在30℃附近徘徊,夏天越来越近
  • 总粉丝破亿!当网络大V遇见硬核科技,互联网时代如何书写上海故事?