[新功能] 增加微信发送接口

This commit is contained in:
msincenselee 2020-01-23 22:34:23 +08:00
parent c4e0c9f136
commit 3ba4f23629

View File

@ -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)