钉钉机器人

准备

参考官方文档

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

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

创建机器人

进入钉钉开放平台,页面创建机器人,发布即可:

获取appKey, appSecret

获取access_token

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

python代码:

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代码

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:])

效果

消息格式

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

相关推荐
Blossom.11813 小时前
机器学习在智能制造业中的应用:质量检测与设备故障预测
人工智能·深度学习·神经网络·机器学习·机器人·tensorflow·sklearn
MidJourney中文版18 小时前
深度报告:中老年AI陪伴机器人需求分析
人工智能·机器人
微小冷1 天前
二关节机器人系统模型推导
线性代数·机器人·概率论·推导·拉格朗日函数·二关节机器人·机器人控制系统的设计
kyle~1 天前
计算机视觉---RealSense深度相机技术
人工智能·数码相机·计算机视觉·机器人·嵌入式·ros·传感器
视觉语言导航2 天前
RAL-2025 | 清华大学数字孪生驱动的机器人视觉导航!VR-Robo:面向视觉机器人导航与运动的现实-模拟-现实框架
人工智能·深度学习·机器人·具身智能
BFT白芙堂2 天前
睿尔曼系列机器人——以创新驱动未来,重塑智能协作新生态(上)
人工智能·机器学习·机器人·协作机器人·复合机器人·睿尔曼机器人
Blossom.1182 天前
机器学习在智能建筑中的应用:能源管理与环境优化
人工智能·python·深度学习·神经网络·机器学习·机器人·sklearn
Mr.Winter`2 天前
障碍感知 | 基于3D激光雷达的三维膨胀栅格地图构建(附ROS C++仿真)
人工智能·机器人·自动驾驶·ros·具身智能·环境感知
AiTEN_Robot3 天前
AGV 无人叉车关键技术问题解析:精准定位算法 / 安全避障逻辑 / 系统对接协议全方案
人工智能·机器人·自动化·制造
一颗小树x3 天前
【机器人】复现 HOV-SG 机器人导航 | 分层 开放词汇 | 3D 场景图
机器人·机器人导航·复现·hov-sg·分层 3d 场景图