This repository has been archived by the owner on Aug 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnotify.py
131 lines (115 loc) · 5.73 KB
/
notify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import os
from discord_webhook import DiscordWebhook, DiscordEmbed
from settings import log, req
class Notify(object):
"""
:param PUSH_CONFIG: JSON-formatted parameters for creating a web request to a user-specified social media API.
Format:
{"method":"post","url":"","data":{},"text":"","code":200,"data_type":"data","show_title_and_desp":false,
"set_data_title":"","set_data_sub_title":"","set_data_desp":""}
Details:
method: REQUIRED, HTTP method e.g. post
url: REQUIRED, address of request.
data: OPTIONAL, parameters sent in the body of the request.
text: REQUIRED, key of expected response code
code: REQUIRED, expected response code, e.g. 0
data_type: OPTIONAL,format of parameters sent, CHOOSE one of: params|json|data
## no idea what any of the following is meant to do, don't ask
show_title_and_desp: OPTIONAL, 是否将标题(应用名+运行状态)和运行结果合并.默认: false.
set_data_title: REQUIRED,填写推送方式data中消息标题的key.例如: server酱的为text.
set_data_sub_title: OPTIONAL,填写推送方式data中消息正文的key.有的推送方式正文的key有次级结构,
需配合set_data_title构造子级,与set_data_desp互斥.
例如: 企业微信中,set_data_title填text,set_data_sub_title填content.
set_data_desp: OPTIONAL,填写推送方式data中消息正文的key.例如: server酱的为desp.
与set_data_sub_title互斥,两者都填则本项不生效.
:param DISCORD_WEBHOOK:
## https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks
"""
# Github Actions -> Settings -> Secrets
# Ensure that the Name exactly matches the parameter names required here
# And the Value contains the data to be used
def __init__(self):
# Custom Push Config
self.PUSH_CONFIG = ''
if 'PUSH_CONFIG' in os.environ:
self.PUSH_CONFIG = os.environ['PUSH_CONFIG']
# Discord Webhook
self.DISCORD_WEBHOOK = ''
if 'DISCORD_WEBHOOK' in os.environ:
self.DISCORD_WEBHOOK = os.environ['DISCORD_WEBHOOK']
def pushTemplate(self, method, url, params=None, data=None, json=None, headers=None, **kwargs):
name = kwargs.get('name')
# needs = kwargs.get('needs')
token = kwargs.get('token')
text = kwargs.get('text')
code = kwargs.get('code')
if not token:
log.info(f'{name} SKIPPED')
return False
try:
response = req.to_python(req.request(
method, url, 2, params, data, json, headers).text)
rspcode = response[text]
except Exception as e:
log.error(f'{name} FAILED\n{e}')
else:
if rspcode == code:
log.info(f'{name} SUCCESS')
else:
log.error(f'{name} FAILED\n{response}')
return True
def custPush(self, text, status, desp):
PUSH_CONFIG = self.PUSH_CONFIG
if not PUSH_CONFIG:
log.info(f'Custom Notifications SKIPPED')
return False
cust = req.to_python(PUSH_CONFIG)
title = f'{text} {status}'
if cust['show_title_and_desp']:
title = f'{text} {status}\n\n{desp}'
if cust['set_data_title'] and cust['set_data_sub_title']:
cust['data'][cust['set_data_title']] = {
cust['set_data_sub_title']: title
}
elif cust['set_data_title'] and cust['set_data_desp']:
cust['data'][cust['set_data_title']] = title
cust['data'][cust['set_data_desp']] = desp
elif cust['set_data_title']:
cust['data'][cust['set_data_title']] = title
conf = [cust['url'], cust['data'], 'Custom Notifications', cust['text'], cust['code']]
url, data, name, text, code = conf
if cust['method'].upper() == 'GET':
return self.pushTemplate('get', url, params=data, name=name, token='token', text=text, code=code)
elif cust['method'].upper() == 'POST' and cust['data_type'].lower() == 'json':
return self.pushTemplate('post', url, json=data, name=name, token='token', text=text, code=code)
else:
return self.pushTemplate('post', url, data=data, name=name, token='token', text=text, code=code)
def discordWebhook(self, text, status, desp):
DISCORD_WEBHOOK = self.DISCORD_WEBHOOK
if not DISCORD_WEBHOOK:
log.info(f'Discord SKIPPED')
return False
webhook = DiscordWebhook(url=DISCORD_WEBHOOK)
embed = DiscordEmbed(title=f'{text} {status}', description=desp, color='03b2f8')
webhook.add_embed(embed)
response = webhook.execute()
if (response.status_code == 200):
log.info(f'Discord SUCCESS')
else:
log.error(f'Discord FAILED\n{response}')
return True
def send(self, app='Honkai Impact 3rd Daily Sign-In', status='', msg='', **kwargs):
hide = kwargs.get('hide', '')
if isinstance(msg, list) or isinstance(msg, dict):
# msg = self.to_json(msg)
msg = '\n\n'.join(msg)
if not hide:
log.info(f'Sign-In result: {status}\n\n{msg}')
if self.PUSH_CONFIG or self.DISCORD_WEBHOOK:
log.info('Sending push notifications...')
self.custPush(app, status, msg)
self.discordWebhook(app, status, msg)
else:
log.info('No social media notifications configured to be sent.')
if __name__ == '__main__':
Notify().send(app='Honkai Impact 3rd Check-In Helper', status='Test Run', msg='Testing integration with social media APIs')