钉钉实现效果![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e72a72ddf3abaf68fcd951e150552cc5.png)
工具模块
dingdingtalk / utils / tools001.py
import time
# 001-打印时间
def get_now_time(bool_value = 1):
""" 默认bool_value为true表示打印当前时分秒"""
if bool_value:
return time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
else:
return time.strftime('%Y-%m-%d',time.localtime(time.time()))
消息发送封装模块
import json
import requests
from utils import tools001
# 1、结合 requests 发送消息
HEADERS = {"Content-Type": "application/json;charset=utf-8"}
# 加密方式1
secret_key_words = "【量化推送】"
## 钉钉群地址
url = "https://oapi.dingtalk.com/robot/send?access_token=2d4e5bc62254d55c69f0cd055a9087604abceb456bfd0e0wedb02df5a30bea2d4"
# 推送标的交易信息
def send_finance_info(bool_value, buy_sell_signal, fin_name, fin_code, trade_num, trade_price, reason):
'''
bool_value: 是否打印出时分秒,默认已打印
buy_sell_signal: 买卖信号, 做多为1, 做空为0, 其他无操作
fin_name: 标的名称
fin_code: 标的代码
trade_num: 交易数量
trade_price: 交易价位
reason: 推荐理由
'''
if buy_sell_signal == 1:
buy_sell_direction = 'buy(买入)'
elif buy_sell_signal == 0:
buy_sell_direction = 'sell(卖出)'
else:
buy_sell_direction = 'error(无操作)'
now_date_time = tools001.get_now_time(bool_value)
content00 = '******************************************************'
content01 = '\n' + secret_key_words + now_date_time + '\n'
content02 = ' 交易时间: ' + now_date_time + '\n'
content03 = ' 标的名称: ' + fin_name + '\n'
content04 = ' 标的编码: ' + fin_code + '\n'
content05 = ' 头寸方向: ' + buy_sell_direction + '\n'
content06 = ' 交易数量: ' + str(trade_num) + ' (手数) \n'
content07 = ' 交易价位: ' + str(trade_price[0]) + '至' + str(trade_price[1]) + ' (元) \n'
content08 = ' 推荐理由: ' + reason + '\n'
content09 = '******************************************************' + '\n'
content = content00 + content01 + content02 + content03 + content04 + content05 + content06 + content07 + content08 + content09
return content
content = send_finance_info(True, 1, '平安银行', '000001', 20, [14.35,14.45] ,'该支标的目前已经跌倒低谷建议抄底入手')
data = {
"msgtype": "text", # sgtype 为消息类型,“text” 为普通文本
"text": {
"content": content
},
"isAtAll": True # isAtAll 为布尔值,是否 @ 所有人,如果设置为 False,可以通过 atMobiles,@指定人(atMobiles 处配置手机号码)
}
response = requests.post(url, json.dumps(data), headers=HEADERS)