luojunhui 6 месяцев назад
Родитель
Сommit
cf43f2690e
1 измененных файлов с 81 добавлено и 17 удалено
  1. 81 17
      applications/feishuBotApi.py

+ 81 - 17
applications/feishuBotApi.py

@@ -6,10 +6,64 @@ import requests
 
 from applications.decoratorApi import retryOnTimeout
 
+mention_all = {
+    "content": "<at id=all></at>\n",
+    "tag": "lark_md",
+}
 
-def create_feishu_table(title, columns, rows):
+
+def create_feishu_columns_sheet(sheet_type, sheet_name, display_name, width="auto",
+                                vertical_align="top", horizontal_align="left", number_format=None):
+    """
+    create feishu sheet
+    :param number_format:
+    :param horizontal_align:
+    :param vertical_align:
+    :param width:
+    :param display_name:
+    :param sheet_type:
+    :param sheet_name:
+    """
+    match sheet_type:
+        case "plain_text":
+            return {
+                "name": sheet_name,
+                "display_name": display_name,
+                "width": width,
+                "data_type": "text",
+                "vertical_align": vertical_align,
+                "horizontal_align": horizontal_align
+            }
+
+        case "lark_md":
+            return {
+                "name": sheet_name,
+                "display_name": display_name,
+                "data_type": "lark_md"
+            }
+
+        case "number":
+            return {
+                "name": sheet_name,
+                "display_name": display_name,
+                "data_type": "number",
+                "format": number_format,
+                "width": width
+            }
+
+        case "date":
+            return {
+                "name": sheet_name,
+                "display_name": display_name,
+                "data_type": "date",
+                "date_format": "YYYY/MM/DD"
+            }
+
+
+def create_feishu_table(title, columns, rows, mention):
     """
     create feishu table
+    :param mention:
     :param title:
     :param columns:
     :param rows:
@@ -23,6 +77,10 @@ def create_feishu_table(title, columns, rows):
             }
         },
         "elements": [
+            {
+                "tag": "div",
+                "text": mention_all,
+            } if mention else {},
             {
                 "tag": "table",
                 "page_size": len(rows) + 1,
@@ -30,8 +88,8 @@ def create_feishu_table(title, columns, rows):
                 "header_style": {
                     "text_align": "left",
                     "text_size": "normal",
-                    "background_style": "none",
-                    "text_color": "grey",
+                    "background_style": "grey",
+                    "text_color": "default",
                     "bold": True,
                     "lines": 1
                 },
@@ -44,24 +102,26 @@ def create_feishu_table(title, columns, rows):
 
 
 @retryOnTimeout()
-def bot(title, detail, mention=True):
+def bot(title, detail, mention=True, table=False):
     """
-    机器人
+    报警机器人
     """
-    title_obj = {
-        "content": "{}<at id=all></at>\n".format(title) if mention else "{}\n".format(title),
-        "tag": "lark_md",
-    }
-    head_title = "【重点关注】" if mention else "【普通通知】"
     url = "https://open.feishu.cn/open-apis/bot/v2/hook/b44333f2-16c0-4cb1-af01-d135f8704410"
+    # dev_url = "https://open.feishu.cn/open-apis/bot/v2/hook/f32c0456-847f-41f3-97db-33fcc1616bcd"
     headers = {"Content-Type": "application/json"}
-    payload = {
-        "msg_type": "interactive",
-        "card": {
+    if table:
+        card = create_feishu_table(
+            title=title,
+            columns=detail["columns"],
+            rows=detail["rows"],
+            mention=mention
+        )
+    else:
+        card = {
             "elements": [
                 {
                     "tag": "div",
-                    "text": title_obj,
+                    "text": mention_all if mention else {},
                 },
                 {
                     "tag": "div",
@@ -73,7 +133,11 @@ def bot(title, detail, mention=True):
                     },
                 },
             ],
-            "header": {"title": {"content": head_title, "tag": "plain_text"}},
-        },
+            "header": {"title": {"content": title, "tag": "plain_text"}},
+        }
+    payload = {
+        "msg_type": "interactive",
+        "card": card
     }
-    requests.request("POST", url=url, headers=headers, data=json.dumps(payload), timeout=10)
+    res = requests.request("POST", url=url, headers=headers, data=json.dumps(payload), timeout=10)
+    return res