发送钉钉消息 day07-07

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

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


# 签名 接口文档 在群机器人-自定义机器人接入
def get_dd_sign():
timestamp = str(round(time.time() * 1000))
secret = 'SECbeca7f462e42a924a9150ad8bbc69b87204241d94b8734678dd4a56396793380'
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))
return timestamp, sign


# 该群机器人发送 接口文档 在服务端API-消息通知-消息类型与数据格式
def send_dd_msg(msg, at=None, at_all=False):
data = {
"msgtype": "text", # 消息类型
"text": {
# "content": "童鞋测试用一哈"
"content":msg
}, # 发送消息
"at": {
# "atMobiles": ["13049484306"],
"atMobiles":at,
"isAtAll": at_all
} # @某个人或者全部

}
timestamp, sign = get_dd_sign()
params = {
"access_token": "bdfc2cfb8f5b8546910fba1790575ae2fa0e39d47da75684f48eecd62404ad66",
"timestamp": timestamp,
"sign": sign
}
try:
res = requests.post(url, json=data, params=params)
print(res.json())
except:
print("钉钉消息没有发送成功")
else:
if res.json().get("errcode") == 0:
return True


if __name__ == '__main__':
content="童鞋测试用一哈"
at = ["18566296129"]
send_dd_msg(content,at)
上一篇:Docker Swarm(二)


下一篇:HackTheBox-poison渗透测试