美文网首页
钉钉机器人发送消息

钉钉机器人发送消息

作者: Leoguo小哥 | 来源:发表于2022-07-13 11:43 被阅读0次

# -*- coding: utf-8 -*-

import time, os, paramiko,json,requests

import time

import hmac

import hashlib

import base64

import urllib.parse

"""

钉钉机器人

"""

def Dingding_send_msg(msg):

# URL为钉钉机器人的access_token地址,目前机器人地址不变,写死在方法中

# secret :钉钉机器人秘钥

# msg: 提示信息

    timestamp =str(round(time.time() *1000))

secret ='SEC'

    url ="https://oapi.dingtalk.com/robot/send?access_token="

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

print(timestamp)

print(sign)

headers = {'Content-Type':'application/json;charset=utf-8'}

data = {

"msgtype":"text", # 发送消息类型为文本

    "at": {

"isAtAll":False, # True为@所有人

    },

    "text": {

"content": msg, # 消息正文

    }

}

url1 = url+'&timestamp='+timestamp+'&sign='+sign

print(url1)

requests.post(url1, data=json.dumps(data), headers=headers)

if __name__ =='__main__':

Dingding_send_msg("1000份礼物吗哈哈")

第二种消息

message = {"msgtype":"markdown","markdown": {"title": case_name_ch,

        "text":"**<font color=#FF0000 size=3>%s </font>**  \n  **步骤**:%s  \n **原因**:%s  \n **请求url**:%s  \n **UID**:%s  \n    **币种**:%s  \n**时间**:%s"%(case_name_ch+" 失败",stepname,reason,url,uid,currency,time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))}}

相关文章

网友评论

      本文标题:钉钉机器人发送消息

      本文链接:https://www.haomeiwen.com/subject/fqozbrtx.html