From b911f771f2c1f75cf0231b24701d0431de5e767e Mon Sep 17 00:00:00 2001 From: foolcage <5533061@qq.com> Date: Mon, 2 Jul 2018 18:05:45 +0800 Subject: [PATCH] email,weixin action --- fooltrader/bot/actions.py | 113 ++++++++++++++++++++++++++++++++++++++ fooltrader/settings.py | 16 ++++++ 2 files changed, 129 insertions(+) create mode 100644 fooltrader/bot/actions.py diff --git a/fooltrader/bot/actions.py b/fooltrader/bot/actions.py new file mode 100644 index 0000000..b074457 --- /dev/null +++ b/fooltrader/bot/actions.py @@ -0,0 +1,113 @@ +# -*- coding: utf-8 -*- +import email +import logging +import smtplib +from email.header import Header +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText + +import requests + +from fooltrader.settings import SMTP_HOST, SMTP_PORT, EMAIL_PASSWORD, EMAIL_USER_NAME + + +class Action(object): + logger = logging.getLogger(__name__) + + def send_message(self, to_user, title, body, **kwargs): + pass + + +class EmailAction(Action): + def __init__(self) -> None: + super().__init__() + self.client = smtplib.SMTP() + self.client.connect(SMTP_HOST, SMTP_PORT) + self.client.login(EMAIL_USER_NAME, EMAIL_PASSWORD) + + def send_message(self, to_user, title, body, **kwargs): + msg = MIMEMultipart('alternative') + msg['Subject'] = Header(title).encode() + msg['From'] = "{} <{}>".format(Header('fooltrader').encode(), EMAIL_USER_NAME) + msg['To'] = to_user + + msg['Message-id'] = email.utils.make_msgid() + msg['Date'] = email.utils.formatdate() + + plain_text = MIMEText(body, _subtype='plain', _charset='UTF-8') + msg.attach(plain_text) + + try: + self.client.sendmail(EMAIL_USER_NAME, to_user, msg.as_string()) + except Exception as e: + self.logger.error('send email failed', e) + + +class WeixinAction(Action): + WEIXIN_TOKEN = 'aaa' + GET_TEMPLATE_URL = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token={}".format( + WEIXIN_TOKEN) + SEND_MSG_URL = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={}".format(WEIXIN_TOKEN) + + { + "touser": "OPENID", + "template_id": "ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY", + "url": "http://weixin.qq.com/download", + "miniprogram": { + "appid": "xiaochengxuappid12345", + "pagepath": "index?foo=bar" + }, + "data": { + "first": { + "value": "恭喜你购买成功!", + "color": "#173177" + }, + "keyword1": { + "value": "巧克力", + "color": "#173177" + }, + "keyword2": { + "value": "39.8元", + "color": "#173177" + }, + "keyword3": { + "value": "2014年9月22日", + "color": "#173177" + }, + "remark": { + "value": "欢迎再次购买!", + "color": "#173177" + } + } + } + + def send_message(self, to_user, title, body, **kwargs): + template_id = 'aaa' + the_json = { + "touser": to_user, + "template_id": template_id, + "url": "http://www.foolcage.com", + "data": { + "first": { + "value": "恭喜你购买成功!", + "color": "#173177" + }, + "keyword1": { + "value": "巧克力", + "color": "#173177" + }, + "keyword2": { + "value": "39.8元", + "color": "#173177" + }, + "keyword3": { + "value": "2014年9月22日", + "color": "#173177" + }, + "remark": { + "value": "欢迎再次购买!", + "color": "#173177" + } + } + } + requests.post(self.SEND_MSG_URL, the_json) diff --git a/fooltrader/settings.py b/fooltrader/settings.py index 193cc85..4143112 100644 --- a/fooltrader/settings.py +++ b/fooltrader/settings.py @@ -129,3 +129,19 @@ ES_HOSTS = ['localhost:9200'] # ES_HOSTS = ['localhost:9200'] + + +# the action account settings +SMTP_HOST = 'smtpdm.aliyun.com' +SMTP_PORT = '80' + +EMAIL_USER_NAME = '' + +if not EMAIL_USER_NAME: + EMAIL_USER_NAME = os.environ.get('EMAIL_USER_NAME') + +EMAIL_PASSWORD = '' +if not EMAIL_PASSWORD: + EMAIL_PASSWORD = os.environ.get('EMAIL_PASSWORD') + +WEIXIN_TOKEN = ''