forked from yokinanya/MaaNotify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotify.py
73 lines (66 loc) · 2.64 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
import logging
import os
import yaml
from onepush import get_notifier
from onepush.core import Provider
from onepush.exceptions import OnePushException
from onepush.providers.custom import Custom
from requests import Response
configfile = logfile = os.path.join(os.path.dirname(__file__),"config.yaml")
def handle_notify(**kwargs) -> bool:
try:
with open(configfile, "r", encoding="utf-8") as _config:
config = {}
for item in yaml.safe_load_all(_config):
config.update(item)
except Exception:
logging.error("Fail to load onepush config, skip sending")
return False
try:
provider_name: str = config.pop("provider", None)
if provider_name is None:
logging.info("No provider specified, skip sending")
return False
notifier: Provider = get_notifier(provider_name)
required: list[str] = notifier.params["required"]
config.update(kwargs)
# pre check
for key in required:
if key not in config:
logging.warning(
f"Notifier {notifier.name} require param '{key}' but not provided"
)
if isinstance(notifier, Custom):
if "method" not in config or config["method"] == "post":
config["datatype"] = "json"
if not ("data" in config or isinstance(config["data"], dict)):
config["data"] = {}
if "title" in kwargs:
config["data"]["title"] = kwargs["title"]
if "content" in kwargs:
config["data"]["content"] = kwargs["content"]
if provider_name.lower() == "gocqhttp":
access_token = config.get("access_token")
if access_token:
config["token"] = access_token
resp = notifier.notify(**config)
if isinstance(resp, Response):
if resp.status_code != 200:
logging.warning("Push notify failed!")
logging.warning(f"HTTP Code:{resp.status_code}")
return False
else:
if provider_name.lower() == "gocqhttp":
return_data: dict = resp.json()
if return_data["status"] == "failed":
logging.warning("Push notify failed!")
logging.warning(f"Return message:{return_data['wording']}")
return False
except OnePushException:
logging.exception("Push notify failed")
return False
except Exception as e:
logging.exception(e)
return False
logging.info("Push notify success")
return True