【python&钉钉】使用python给钉钉发送文字消息、图片消息

【python&钉钉】使用python给钉钉发送图片消息

  • 一、在钉钉群中创建机器人
  • 二、生成签名
  • 三、python调用钉钉机器人发送图片消息整体代码
  • 四、钉钉上看到的效果
  • python:3.12.0

    一、在钉钉群中创建机器人

    1、在电脑的钉钉客户端中“添加机器人”
    2、选择“自定义(通过webhook接入自定义服务)”机器人
    3、安全设置点击“加签”(也可以设置自定义或IP地址)
    4、将“加签”的内容复制出来备用
    5、点击完成
    6、复制“Webhook”内容备用

    二、生成签名

    1、引入python相关依赖

    import requests
    import json
    import base64
    import hmac
    import hashlib
    import time
    

    备注:
    1)requests是Python HTTP客户端库;是python语言的第三方的库,专门用于发送HTTP请求。
    2)json(JavaScript Object Notation)是Python内置的一个用于处理JSON数据的模块。是一种轻量级的数据交换格式,常用于Web应用程序之间的数据传输。
    3)base64是Python中一种用64个字符表示任意二进制数据的方法
    4)hmac模块提供了基于密钥的消息认证码(HMAC)算法,用于验证消息的完整性和身份认证。它可以用于保护敏感数据,例如密码、API密钥等
    5)hashlib 模块主要用于进行哈希(hash)操作
    6)time是python中操作时间函数的相关函数和方法

    2、生成签名

     # 电脑端钉钉群里添加自定义机器人获取,第一步的“Webhook”链接后缀
    access_token = YOU_ACCESS_TOKEN
     # 从钉钉机器人设置中获取,即第一步的“加签”
    secret = YOU_SECRET
     #要发送的图片地址
    picURL = "https://q5.itc.cn/q_70/images03/20240612/5e4ff010635c4a0389e03a305271f8b2.jpeg"
    
    # 当前时间戳
    timestamp = str(round(time.time() * 1000))
    
    # 拼接字符串
    string_to_sign = '{}\n{}'.format(timestamp, secret)
    
    # 生成签名
    sign = hmac.new(secret.encode('utf-8'), string_to_sign.encode('utf-8'), digestmod=hashlib.sha256).digest()
    sign = base64.b64encode(sign).decode('utf-8')
    

    3、准备要发送的数据
    1)发送文字

    {
    		'msgtype':'text',
    		'text':{
    			'content': '要发送的文字'
    		}
    	}
    

    2)发送图片

    msg = {
            "msgtype": "image",
            "image": {
                "picURL": picURL
            }
    }
    

    3)其他发送消息方式参考:https://open.dingtalk.com/document/orgapp/common-message

    4、发送请求

    # 发送POST请求
    response = requests.post(webhook_url, data=json.dumps(msg), headers={'Content-Type': 'application/json'})
    
    # 所有返回的内容
    print(response.json())
    
    # 打印响应状态码
    print(response.status_code)
    
    # 打印响应内容
    print(response.text)
    

    三、python调用钉钉机器人发送图片消息整体代码

    import requests
    import json
    import base64
    import hmac
    import hashlib
    import time
    
     # 电脑端钉钉群里添加自定义机器人获取,第一步的“Webhook”链接后缀
    access_token = YOU_ACCESS_TOKEN
     # 从钉钉机器人设置中获取,即第一步的“加签”
    secret = YOU_SECRET
     #要发送的图片地址
    picURL = "https://q5.itc.cn/q_70/images03/20240612/5e4ff010635c4a0389e03a305271f8b2.jpeg"
    
    # 当前时间戳
    timestamp = str(round(time.time() * 1000))
    
    # 拼接字符串
    string_to_sign = '{}\n{}'.format(timestamp, secret)
    
    # 生成签名
    sign = hmac.new(secret.encode('utf-8'), string_to_sign.encode('utf-8'), digestmod=hashlib.sha256).digest()
    sign = base64.b64encode(sign).decode('utf-8')
    
    # 钉钉机器人的Webhook URL
    webhook_url = 'https://oapi.dingtalk.com/robot/send?access_token={}&timestamp={}&sign={}'.format(access_token, timestamp, sign)
    
    msg = {
            'msgtype':'text',
            'text':{
                'content': '要发送的文字'
            }
        }
    # 发送POST请求
    response = requests.post(webhook_url, data=json.dumps(msg), headers={'Content-Type': 'application/json'})
    
    # 所有返回的内容
    print(response.json())
    
    # 打印响应状态码
    print(response.status_code)
    
    # 打印响应内容
    print(response.text)
    

    四、钉钉上看到的效果

    作者:纳米小川

    物联沃分享整理
    物联沃-IOTWORD物联网 » 【python&钉钉】使用python给钉钉发送文字消息、图片消息

    发表回复