Python 发钉钉消息
import time
import datetime
import requests
import smtplib
import dingtalkchatbot.chatbot as cb
import hmac
import hashlib
import base64
import urllib.parse
class DingRobot:
def __init__(self):
self.timestamp = str(round(time.time() * 1000))
secret = 'SECd0829d3afc52d2这是创建robot时的secret'
secret_enc = secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(self.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()
self.sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
self.url = "https://oapi.dingtalk.com/robot/send?access_token=f628ece24445238aa1ca98ba4571a9acb8505e9673c7ad7cb6099b4a×tamp={}&sign={}".format(
self.timestamp, self.sign)
def dingStart(self, message):
xiaoding = cb.DingtalkChatbot(self.url)
# 发送消息
xiaoding.send_text(message)