Skip to content

Commit

Permalink
email,weixin action
Browse files Browse the repository at this point in the history
  • Loading branch information
foolcage committed Jul 2, 2018
1 parent b832a6c commit b911f77
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
113 changes: 113 additions & 0 deletions fooltrader/bot/actions.py
Original file line number Diff line number Diff line change
@@ -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)
16 changes: 16 additions & 0 deletions fooltrader/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ''

0 comments on commit b911f77

Please sign in to comment.