首页 > 编程语言 > 详细

python发送钉钉消息

时间:2019-10-29 18:59:59      阅读:199      评论:0      收藏:0      [点我收藏+]
import requests
import time
import hashlib
import hmac
import base64
import re

def SendMessage(message = ):

    # secret:密钥,机器人安全设置页面,加签一栏下面显示的SEC开头的字符串,例如:SECxxxxxxxx
    secret = SECxxxxxxxx
    # access_token:创建完钉钉机器人之后会自动生成,例如:access_tokenxxxx
    access_token = access_tokenxxxx
    # timestamp:当前时间戳,单位是毫秒,与请求调用时间误差不能超过1小时
    timestamp = int(round(time.time() * 1000))

    # 加密,获取sign和timestamp
    data = (str(timestamp) + \n + secret).encode(utf-8)
    secret = secret.encode(utf-8)
    signature = base64.b64encode(hmac.new(secret, data, digestmod=hashlib.sha256).digest())
    reg = re.compile(r"‘(.*)‘")
    signature = str(re.findall(reg,str(signature))[0])

    # 发送信息
    url = https://oapi.dingtalk.com/robot/send?access_token=%s&sign=%s&timestamp=%s % (access_token,signature,timestamp)
    headers = {"Content-Type": "application/json ;charset=utf-8 "}
    try:
        response = requests.post(url, headers = headers, json = message, timeout = (3,60))
        print(response)
        response_msg = str(response.status_code) +   + str(response.content)
        print(response_msg)
    except Exception as error_msg:
        print(error_msg===+str(error_msg))
        response_msg = error_msg

    return response_msg

if __name__ == "__main__":
    msg = {"msgtype":"text","text":{"content":"测试"},"at":{"isAtAll":False}}
    SendMessage(msg)

钉钉官方文档:https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq

python发送钉钉消息

原文:https://www.cnblogs.com/turbolxq/p/11760650.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!