Skip to content
This repository has been archived by the owner on May 21, 2022. It is now read-only.

Commit

Permalink
Removed:sc.ftqq WeChat push
Browse files Browse the repository at this point in the history
Added:sct.ftqq WeChat push
Added:go-scf WeChat push
  • Loading branch information
BenjiaH committed Aug 8, 2021
1 parent 98c25c1 commit bcd6e83
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 23 deletions.
8 changes: 6 additions & 2 deletions common/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,17 @@ def email_push(self):
return self._all_info[3]

@property
def sckey(self):
def sendkey(self):
return self._all_info[4]

@property
def email(self):
def userid(self):
return self._all_info[5]

@property
def email(self):
return self._all_info[6]

@property
def row(self):
return self._row
Expand Down
49 changes: 39 additions & 10 deletions common/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import requests
import os

from urllib import parse
from datetime import datetime
from email.mime.text import MIMEText
from common.config import global_config
Expand Down Expand Up @@ -90,32 +91,57 @@ def _load_errno(self):

@staticmethod
@logger.catch
def wechat(uid, title, message, sckey):
def sct_wechat(uid, title, message, sendkey):
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
url = 'https://sc.ftqq.com/{}.send'.format(sckey)
ps = """PS 「sc.ftqq.com」API将于2021年7月30日(暂定,可能延后)下线,届时「sc.ftqq.com」推送接口将无法使用。本项目将尽快上线「sct.ftqq.com」以便继续提供微信推送接口。[
(sc.ftqq.com 分期下线通知和常见问题解答)](https://mp.weixin.qq.com/s/KGQC1v5rsG_JKVRtN2DY4w)"""
url = 'https://sctapi.ftqq.com/{}.send'.format(sendkey)
ps = ""
msg = " " * 10 + title + "\n\n" + uid + ":\n" + " " * 7 + message + "\n{ps}\n\n{time}".format(ps=ps, time=now)
payload = {
"text": title,
"desp": uid + ":\n\n" + message + "\n\n{ps}\n\n`{time}`".format(ps=ps, time=now)
"title": title,
"desp": parse.quote(msg)
}
res = requests.get(url=url, params=payload)
logger.debug("URL:{url}. Status code:{code}".format(url=url, code=res.status_code))
res.encoding = "utf-8"
logger.debug("Response:{res}".format(res=res.text))
if res.status_code != 200:
logger.error("Failed to push the wechat message. Status code:{code}.".format(code=res.status_code))
return False
else:
logger.info("Successful to push the wechat message.")
return True

@staticmethod
@logger.catch
def go_scf_wechat(uid, title, message, api, sendkey, userid):
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
url = '{api}/{sendkey}'.format(api=api, sendkey=sendkey)
ps = ""
msg = " " * 10 + title + "\n\n" + uid + ":\n" + " " * 7 + message + "\n{ps}\n\n{time}".format(ps=ps, time=now)
payload = {
"sendkey": sendkey,
"msg_type": "text",
"msg": msg,
"to_user": userid
}
# go_scf post请求body必须为json。详见文档
res = requests.post(url=url, data=json.dumps(payload))
logger.debug("URL:{url}. Status code:{code}".format(url=url, code=res.status_code))
res.encoding = "utf-8"
logger.debug("Response:{res}".format(res=res.text))
dict_res = json.loads(res.text)
if res.status_code != 200:
logger.error("Failed to push the wechat message. Status code:{code}.".format(code=res.status_code))
return False
elif dict_res["errno"] != 0:
logger.error("Failed to push the wechat message. [{msg}].".format(msg=dict_res["errmsg"]))
elif dict_res["code"] != 0:
logger.error("Failed to push the wechat message. [{msg}].".format(msg=dict_res["msg"]))
return False
else:
logger.info("Successful to push the wechat message.")
return True

@logger.catch
def push(self, result, uid, wechat_push, email_push, sckey="", email_rxer=""):
def push(self, result, uid, wechat_push, email_push, wechat_type, api, userid, sendkey="", email_rxer=""):
status = result[0]
errno = result[1]
if status == 0:
Expand All @@ -136,7 +162,10 @@ def push(self, result, uid, wechat_push, email_push, sckey="", email_rxer=""):
logger.debug("Title:{title}#Message:{msg}#Error code:{errno}".format(title=title, msg=message, errno=errno))
if self._global_wechat != "off":
if wechat_push == "1" or wechat_push == "on":
self.wechat(uid, title, message, sckey)
if str(wechat_type) == "1":
self.sct_wechat(uid, title, message, sendkey)
else:
self.go_scf_wechat(uid, title, message, api, sendkey, userid)
if self._global_email != "off":
if email_push == "1" or email_push == "on":
self.bot_email.send(uid, title, message, [email_rxer])
Expand Down
13 changes: 9 additions & 4 deletions common/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ def __init__(self):
self._password = global_config.getRaw('account', 'password')
self._wechat_push = global_config.getRaw('config', 'wechat_enable')
self._email_push = global_config.getRaw('config', 'email_enable')
self._sckey = global_config.getRaw('messenger', 'sckey')
self._sendkey = global_config.getRaw('messenger', 'sendkey')
self._email_rxer = global_config.getRaw('messenger', 'email')
self._wechat_type = global_config.getRaw('config', 'wechat_type')
self._api = global_config.getRaw('config', 'api')
self._userid = global_config.getRaw('messenger', 'userid')
self._report = Report()

@logger.catch
Expand All @@ -33,7 +36,8 @@ def _single_mode(self):
logger.info("Report ID:{uid}".format(uid=self._uid).center(46, '-'))
ret = self._report.main(uid=self._uid, password=self._password)
global_push.push(ret, uid=self._uid, wechat_push=self._wechat_push, email_push=self._email_push,
sckey=self._sckey, email_rxer=self._email_rxer)
sendkey=self._sendkey, email_rxer=self._email_rxer, wechat_type=self._wechat_type,
api=self._api, userid=self._userid)

@logger.catch
def _multiple_mode(self):
Expand All @@ -44,8 +48,9 @@ def _multiple_mode(self):
logger.info(log_info)
ret = self._report.main(uid=global_account.studentID[i], password=global_account.password[i])
global_push.push(ret, uid=global_account.studentID[i], wechat_push=global_account.wechat_push[i],
email_push=global_account.email_push[i], sckey=global_account.sckey[i],
email_rxer=global_account.email[i])
email_push=global_account.email_push[i], sendkey=self._sendkey,
email_rxer=global_account.email[i], wechat_type=self._wechat_type,
api=self._api, userid=global_account.userid[i])
sleep(1.5)

@logger.catch
Expand Down
8 changes: 4 additions & 4 deletions config/account_example.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
studentID,password,wechat_push,email_push,SCKEY,email
201800000000,abc123456,1,1,SCUXXXXXXXXX,[email protected]
201800000000,abc123456,1,0,SCUXXXXXXXXX,
201800000000,abc123456,0,1,,[email protected]
studentID,password,wechat_push,email_push,sendkey,userid,email
201800000000,abc123456,1,1,XXXXXXXXXXXXX,XXXXX,[email protected]
201800000000,abc123456,1,0,XXXUXXXXXXXXX,XXXX,
201800000000,abc123456,0,1,,,[email protected]
16 changes: 13 additions & 3 deletions config/config_example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ wechat_enable = off
; 如果需要邮件推送打卡结果,则将 email_enable 设置为 on,默认为 off 不开启
email_enable = off

;提供多种推送通道,目前仅支持sct企业微信和腾讯云自建企业微信(go-scf)
;如果需选择企业微信通道,则将 wechat_type 设置为 1,否则为 2 。默认为 1 sct企业微信通道
wechat_type = 1

; 如果wechat_type = 2 ,必须填入 api (请求路径:https://xxxxxxxxxx)
api =

[bot_email]
; 负责推送的邮箱

Expand All @@ -45,9 +52,12 @@ studentID =
password =

[messenger]
; 如果开启微信推送必须填入sckey
; 如何获取请参考 http://sc.ftqq.com/3.version。感谢Server酱~
sckey =
; 任意一种通道均需填入 sendkey
; 如果wechat_type = 1 ,sendkey获取请参考 https://sct.ftqq.com 。感谢Server酱~
sendkey =

; 企业微信的userid,若不填写将会推送给企业所有人
userid =

; 如果开启邮件推送,必须填入email
; 接收打卡结果的邮箱
Expand Down

0 comments on commit bcd6e83

Please sign in to comment.