Spaces:
Sleeping
Sleeping
Create bot.py
Browse files
bot.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import json
|
| 3 |
+
import socket
|
| 4 |
+
from loguru import logger
|
| 5 |
+
from enum import Enum
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class MSGTYPE(Enum):
|
| 9 |
+
INCIDENT = 1
|
| 10 |
+
ERROR = 2
|
| 11 |
+
REPORT = 3
|
| 12 |
+
INFO = 4
|
| 13 |
+
SUCCESS = 0
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
icons = {
|
| 17 |
+
MSGTYPE.SUCCESS: "https://img.icons8.com/ios-glyphs/30/000000/rocket.png",
|
| 18 |
+
MSGTYPE.INCIDENT: "https://img.icons8.com/ios-filled/30/car-accident.png",
|
| 19 |
+
MSGTYPE.ERROR: "https://img.icons8.com/ios-glyphs/30/000000/error--v2.png",
|
| 20 |
+
MSGTYPE.REPORT: "https://img.icons8.com/ios-glyphs/90/000000/checkmark--v2.png",
|
| 21 |
+
MSGTYPE.INFO: "https://img.icons8.com/ios-glyphs/30/000000/info--v2.png",
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
class Message:
|
| 26 |
+
host_name = socket.gethostname()
|
| 27 |
+
host_ip = socket.gethostbyname(socket.gethostname())
|
| 28 |
+
type = MSGTYPE.INCIDENT
|
| 29 |
+
type_hint = ""
|
| 30 |
+
content = ""
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
class dummyRequest:
|
| 34 |
+
def __init__(self) -> None:
|
| 35 |
+
self.status_code = 200
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
class Bot:
|
| 39 |
+
def __init__(
|
| 40 |
+
self,
|
| 41 |
+
app_name="",
|
| 42 |
+
bot_key="",
|
| 43 |
+
enabled=True,
|
| 44 |
+
):
|
| 45 |
+
self.Bot_URL = f"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={bot_key}"
|
| 46 |
+
self.template = {
|
| 47 |
+
|
| 48 |
+
"msgtype": "template_card",
|
| 49 |
+
"template_card": {
|
| 50 |
+
"card_type": "text_notice",
|
| 51 |
+
"source": {
|
| 52 |
+
"icon_url": "",
|
| 53 |
+
"desc": app_name,
|
| 54 |
+
"desc_color": 0
|
| 55 |
+
},
|
| 56 |
+
"main_title": {
|
| 57 |
+
"title": "",
|
| 58 |
+
"desc": ""
|
| 59 |
+
},
|
| 60 |
+
"emphasis_content": {
|
| 61 |
+
"title": "",
|
| 62 |
+
"desc": ""
|
| 63 |
+
},
|
| 64 |
+
"horizontal_content_list": [
|
| 65 |
+
],
|
| 66 |
+
"card_action": {
|
| 67 |
+
"type": 1,
|
| 68 |
+
"url": "https://huggingface.co/myscale"
|
| 69 |
+
},
|
| 70 |
+
}
|
| 71 |
+
}
|
| 72 |
+
self.app_name = app_name
|
| 73 |
+
self.enabled = enabled
|
| 74 |
+
|
| 75 |
+
def __constuct_msg(self, msg: Message):
|
| 76 |
+
_dict = self.template.copy()
|
| 77 |
+
_dict["template_card"]["source"]["icon_url"] = icons[msg.type]
|
| 78 |
+
_dict["template_card"]["main_title"]["title"] = msg.type.name[:13]
|
| 79 |
+
_dict["template_card"]["main_title"]["desc"] = msg.type_hint[:15]
|
| 80 |
+
_dict["template_card"]["horizontal_content_list"].extend(
|
| 81 |
+
self.__convert_dict2clist(msg)[:6])
|
| 82 |
+
_dict["template_card"]["sub_title_text"] = msg.content
|
| 83 |
+
return _dict
|
| 84 |
+
|
| 85 |
+
def __convert_dict2clist(self, msg: Message):
|
| 86 |
+
return [
|
| 87 |
+
{"keyname": "App Name", "value": self.app_name},
|
| 88 |
+
{"keyname": "Host Name", "value": msg.host_name},
|
| 89 |
+
{"keyname": "Host IP", "value": str(msg.host_ip)},
|
| 90 |
+
]
|
| 91 |
+
|
| 92 |
+
def __send_md_msg(self, msg: Message):
|
| 93 |
+
msg = json.dumps(self.__constuct_msg(msg), ensure_ascii=True)
|
| 94 |
+
return requests.post(
|
| 95 |
+
self.Bot_URL, data=msg, headers={
|
| 96 |
+
"Content-Type": "application/json"}
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
def error(self, msg: Message):
|
| 100 |
+
msg.type = MSGTYPE.ERROR
|
| 101 |
+
if self.enabled:
|
| 102 |
+
return self.__send_md_msg(msg)
|
| 103 |
+
else:
|
| 104 |
+
return dummyRequest()
|
| 105 |
+
|
| 106 |
+
def incident(self, msg: Message):
|
| 107 |
+
msg.type = MSGTYPE.INCIDENT
|
| 108 |
+
if self.enabled:
|
| 109 |
+
return self.__send_md_msg(msg)
|
| 110 |
+
else:
|
| 111 |
+
return dummyRequest()
|
| 112 |
+
|
| 113 |
+
def report(self, msg: Message):
|
| 114 |
+
msg.type = MSGTYPE.REPORT
|
| 115 |
+
if self.enabled:
|
| 116 |
+
return self.__send_md_msg(msg)
|
| 117 |
+
else:
|
| 118 |
+
return dummyRequest()
|