共计 912 个字符,预计需要花费 3 分钟才能阅读完成。
背景需求:
因为使用了第三方的身份认证信息,当套餐余额不足的情况下会导致认证功能不可用。
所以使用 python 获取套餐剩余结果发送到钉钉。
import requests
import time
import hashlib
from dingtalkchatbot.chatbot import DingtalkChatbot
dingtalk_webhook ='https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxx'
# 通知的钉钉群 @人员的手机号列表,可以为空
at_mobiles = []
key = "xxxxxxxx" #套餐的 key 信息
timestamp = int(time.time())
openId = "xxxxxxxxxxx" #appid 通过用户中心查看
sign_str = key + str(timestamp) + openId
sign = hashlib.md5(sign_str.encode()).hexdigest()
url = "https://apis.juhe.cn/userapi/api_balance"
params = {
"key": key,
"timestamp": timestamp,
"sign": sign
}
response = requests.get(url, params=params)
data = response.json()
instances = [data['result']["api_balance"]]
#print(instances)
def dingdingpush():
dingtalk = DingtalkChatbot(dingtalk_webhook)
msg = "聚合身份认证套餐余量查询:\n\n"
msg += "| 所属账户 | 余量条数 |\n"
msg += "|---|---|\n"
for instance in instances:
msg += f"| 聚合 |{instance}|\t\t|\n"
dingtalk.send_markdown(title="聚合身份认证套餐余量查询", text=msg, at_mobiles=at_mobiles)
dingdingpush()
发送结果如下
正文完