1.自动发送消息 

 选择自定义机器人

# python 3.8
import time
import hmac
import hashlib
import base64
import urllib.parse
import requests
import json

# 加签
timestamp = str(round(time.time() * 1000))
secret = '加签的SECXXX'
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))

token = '机器人Webhook:获取access_token==XXX'


def dingmessage():
    # 请求的URL,WebHook地址
    webhook = f"https://oapi.dingtalk.com/robot/send?access_token={token}&timestamp={timestamp}&sign={sign}"

    # 构建请求头部
    header = {"Content-Type": "application/json", "Charset": "UTF-8"}

    # 循环生成器并发送消息
    content = 'haha'
    message = {
        "msgtype": "text",
        "text": {"content": content},
        "at": {
            # @ 所有人
            "isAtAll": True
        }
    }
    message_json = json.dumps(message)
    info = requests.post(url=webhook, data=message_json, headers=header, verify=False)  # 打印返回的结果
    # print(info.text)


if __name__ == "__main__":
    dingmessage()

haha

错误汇总

// 消息内容中不包含任何关键词
{
  "errcode":310000,
  "errmsg":"keywords not in content"
}
 
// timestamp 无效
{
  "errcode":310000,
  "errmsg":"invalid timestamp"
}
 
// 签名不匹配
{
  "errcode":310000,
  "errmsg":"sign not match"
}
 
// IP地址不在白名单
{
  "errcode":310000,
  "errmsg":"ip X.X.X.X not in whitelist"