From 3ba4f23629321fef23e09cbbd3b17a9a3a8886ef Mon Sep 17 00:00:00 2001 From: msincenselee Date: Thu, 23 Jan 2020 22:34:23 +0800 Subject: [PATCH] =?UTF-8?q?[=E6=96=B0=E5=8A=9F=E8=83=BD]=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=BE=AE=E4=BF=A1=E5=8F=91=E9=80=81=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/trader/util_wechat.py | 89 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 vnpy/trader/util_wechat.py diff --git a/vnpy/trader/util_wechat.py b/vnpy/trader/util_wechat.py new file mode 100644 index 00000000..e0557ca3 --- /dev/null +++ b/vnpy/trader/util_wechat.py @@ -0,0 +1,89 @@ +# -*- coding:utf-8 -*- +''' +通过wxpusher发送Weixin的消息 +http://wxpusher.zjiecode.com/ +开通步骤: +1、关注公众号,注册 +2、通过公众号,获取UID +3、通过网站=》应用列表=》新建应用,如vnpy2,并获得APP_TOOKEN +4、应用列表=》应用(vnpy2)=》 关注. +''' + +from threading import Lock, Thread +import requests +import json +import sys +import traceback +from urllib.parse import urlencode +from datetime import datetime + +global wechat_lock +wechat_lock = Lock() + +# 这里可以设置UID +UID = 'UID_kZguGPBQPWn41Ni9FK4CgPts2Kj' +APP_TOKEN = 'AT_aDuiQu41dmAQV2vUMXOaaTDrWyhKJN2' + + +class wechat_thread(Thread): + """ + 采用线程方式,不阻塞 + """ + + def __init__(self, uid: str, content: str, topic_ids: list = [], url: str = ''): + + # text:消息标题,最长为256,必填。 + # desp:消息内容,最长64Kb,可空,支持MarkDown。 + + super(wechat_thread, self).__init__(name="wechat_thread") + self.request_url = "http://wxpusher.zjiecode.com/api/send/message" + self.uid = uid + self.content = content + self.topic_ids = topic_ids + self.url = url + self.lock = wechat_lock + + def run(self): + if self.content is None or len(self.content) == 0: + return + params = {} + params['appToken'] = APP_TOKEN + params['content'] = self.content + params['contentType'] = 1 + params['topicIds'] = self.topic_ids + params['uids'] = [self.uid] + params['url'] = self.url + + # 发送请求 + try: + response = requests.post(self.request_url, json=params).json() + if not response.get('success', False): + print(response) + except Exception as e: + print("{} wechat_thread sent failed! ex:{},trace:{}".format(datetime.now(), str(e), traceback.format_exc()), + file=sys.stderr) + return + + print("wechat_thread sent successful!") + + +def send_wx_msg(content=''): + """ + 发送微信Msg + :param content: 发送内容 + + :return: + """ + if len(text) == 0: + return + + t = wechat_thread(uid=UID, content=content) + t.daemon = False + # t.run() + t.start() + + +if __name__ == '__main__': + text = u'微信测试标题!!!!\n第二行' + + send_wx_msg(text)