共计 1516 个字符,预计需要花费 4 分钟才能阅读完成。
写这个脚本的需求是,因为有多个账户费用充值问题容易遗漏,所以需要 python 脚本定时提醒!目前为负数还没有测试过。
import json
from aliyunsdkcore.client import AcsClient
from aliyunsdkbssopenapi.request.v20171214.QueryAccountBalanceRequest import QueryAccountBalanceRequest
from dingtalkchatbot.chatbot import DingtalkChatbot
# 阿里云 AccessKey ID 和 Access Key Secret,以及账户 ID 和名称
accounts = [
{
'access_key_id': 'xxxx',
'access_key_secret': 'xxxx',
'account_name': '账户 1'
},
{
'access_key_id': 'xxx',
'access_key_secret': 'xxxx',
'account_name': '账户 2'
},
{
'access_key_id': 'xx',
'access_key_secret': 'xxxx',
'account_name': '账户 3'
},
{
'access_key_id': 'xxxxx',
'access_key_secret': 'xxxxx',
'account_name': '账户 4'
}
]
# 钉钉机器人 webhook 地址
webhook_url = 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxx'
# 创建 ACS 客户端并查询余额
balances = []
total_balance = 0
for account in accounts:
client = AcsClient(account['access_key_id'], account['access_key_secret'], 'cn-hangzhou')
request = QueryAccountBalanceRequest()
request.set_accept_format('json')
response = client.do_action_with_exception(request)
result = json.loads(response)
balance = float(result['Data']['AvailableCashAmount'].replace(',', ''))
balances.append((account['account_name'], balance))
total_balance += balance
# 发送钉钉消息
dingtalk_robot = DingtalkChatbot(webhook_url)
message = "### <center><font color='blue'> 账户余额定时提醒 </font></center>\n\n"
message += '[** 点击进行续费 **](https://www.aliyun.com)\n'
#message += '''## 总余额:%.2f 元 \n\n''' % total_balance
message += '''| 账户 ID| 可用余额 |\n|-|-|\n'''
for account_id, balance in balances:
message += '''|%s|%.2f 元 |\n''' % (account_id, balance)
dingtalk_robot.send_markdown(title='多账户余额查询结果', text=message)
填入你的 ak 和账户名称即可
效果如下图
正文完