【Python笔记】钉钉自定义机器人

到钉钉群中添加自定义机器人
【Python笔记】钉钉自定义机器人_第1张图片

【Python笔记】钉钉自定义机器人_第2张图片
打码的信息是下面代码要填写的信息

python版本(3.8-3.10)

import time
import hmac
import hashlib
import base64
import urllib.parse

timestamp = str(round(time.time() * 1000))
secret = '机器人密钥'
secret_enc = secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp, secret)
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))


import requests,json
headers={'Content-Type': 'application/json'}
webhook = '机器人的webhook'+'×tamp='+timestamp+"&sign="+sign

data = data = {
    "text": {
        "content":"大家好"
    },
    "msgtype":"text"
}



res = requests.post(webhook, data=json.dumps(data), headers=headers)

print(res.text)

运行即可


可发送格式

  • text
  • link
  • Markdown
data = {
    "at": {
        "atMobiles":[
            "被@人的手机号"
        ],
        "atUserIds":[
            "被@人的用户userid"
        ],
        "isAtAll": "是否@所有人" False True
    },
    "text": {
        "content":"大家好"
    },
    "msgtype":"text"
}
data = {
    "msgtype": "link", 
    "link": {
        "text": "消息内容", 
        "title": "消息标题", 
        "picUrl": "图片链接", 
        "messageUrl": "点击消息跳转的链接"
    }
}
data = {
     "msgtype": "markdown",
     "markdown": {
         "title":"消息标题",
         "text": "消息内容"
     },
      "at": {
          "atMobiles": [
              "被@人的手机号"
          ],
          "atUserIds": [
              "被@人的用户userid"
          ],
          "isAtAll": "是否@所有人" False True
      }
 }

官网文档:https://open.dingtalk.com/document/robots/custom-robot-access

你可能感兴趣的:(Python,python,机器人,开发语言)