# -*- coding: utf-8 -*-
import time
import hmac
import hashlib
import base64
import urllib.parse
import requests
import json
import sys
import os
from email.mime.text import MIMEText
from email.header import Header
class dd:
def __init__(self, secret, url,atmobiles, mes):
self.timestamp = str(round(time.time() * 1000))
self.secret = secret
self.secret_enc = self.secret.encode('utf-8')
self.string_to_sign = '{}\n{}'.format(self.timestamp, self.secret)
self.string_to_sign_enc = self.string_to_sign.encode('utf-8')
self.hmac_code = hmac.new(self.secret_enc, self.string_to_sign_enc, digestmod=hashlib.sha256).digest()
self.sign = urllib.parse.quote_plus(base64.b64encode(self.hmac_code))
self.url = url
self.atmobiles = atmobiles
self.mes = mes
def req(self):
#把第二步中获取到的 timestamp和sign拼接到URL中
url = f'{self.url}'f'×tamp={self.timestamp}'f'&sign={self.sign}'
h = {'content-type': 'application/json', 'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0'}
#d里面的at参数是需要at的人参数,只有at的人存在这个参数里面才会@成功
d = json.dumps({"msgtype": "text", "text": {"content": self.mes}, "at": {"atMobiles": self.atmobiles, "isAtAll": "false"}})
req = requests.post(url, data=d, headers=h)
print(req.text)
if __name__ == '__main__':
secret = '*************************90c8a5df55ba4b91f6451a7d' # 这里填的就是上面获取的加签**
url = 'https://oapi.dingtalk.com/robot/send?access_token=**********************************d138a8d6da841edaf34200b9f9710'
atmobiles = ["138xxxx0751","157xxxx1995"]
mes = '哈喽啊,树哥!'
dd = dd(secret, url, atmobiles, mes)
dd.req()
05-24
145
07-27
352
06-25