使用python进行钉钉机器人的消息发送(带颜色标题、图片、及@人)


#!/usr/bin/python3
import time
import hmac
import hashlib
import base64
import urllib.parse
import os

timestamp = str(round(time.time() * 1000))
secret = 'SEC×××××××××××××××××××××××××××××××××××××××××××'
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)
webhookurl = 'https://oapi.dingtalk.com/robot/send?access_token=××××××××××××××××××××××××××××××××××' + '&timestamp=' + timestamp + '&sign=' + sign
os.environ['url'] = webhookurl
#os.environ['xiantime'] = time.strftime('%H:%M:%S')
os.environ['xiantime'] = time.ctime()
os.system(''' curl ${url} -H 'Content-Type: application/json' -d '{"msgtype": "text","text":{"content":"现在时间是 '"$xiantime"'   !"}}' ''')
#print(webhookurl)


data = {
"msgtype": "markdown",
"markdown": {
"title": info1,
"text":"### <font color=%s>%s</font>"%(color,标题) + '\n\n '+ 内容+"![screenshot](%s)"%(图片链接)+"@手机号1  @手机号2" },
"at":{
"atMobiles":["手机号1","手机号2"],
"isAtAll": False,
},

color格式##00FF00

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM