Explorar o código

Merge branch '2024-12-13-add-feishu-table' of luojunhui/LongArticlesJob into master

luojunhui hai 4 meses
pai
achega
698b019829
Modificáronse 1 ficheiros con 114 adicións e 14 borrados
  1. 114 14
      applications/feishuBotApi.py

+ 114 - 14
applications/feishuBotApi.py

@@ -6,26 +6,122 @@ import requests
 
 from applications.decoratorApi import retryOnTimeout
 
+mention_all = {
+    "content": "<at id=all></at>\n",
+    "tag": "lark_md",
+}
 
-@retryOnTimeout()
-def bot(title, detail, mention=True):
+
+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:
     """
-    title_obj = {
-        "content": "{}<at id=all></at>\n".format(title) if mention else "{}\n".format(title),
-        "tag": "lark_md",
+    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:
+    """
+    table_base = {
+        "header": {
+            "template": "blue",
+            "title": {
+                "content": title,
+                "tag": "plain_text"
+            }
+        },
+        "elements": [
+            {
+                "tag": "div",
+                "text": mention_all,
+            } if mention else {},
+            {
+                "tag": "table",
+                "page_size": len(rows) + 1,
+                "row_height": "low",
+                "header_style": {
+                    "text_align": "left",
+                    "text_size": "normal",
+                    "background_style": "grey",
+                    "text_color": "default",
+                    "bold": True,
+                    "lines": 1
+                },
+                "columns": columns,
+                "rows": rows
+            }
+        ]
     }
-    head_title = "【重点关注】" if mention else "【普通通知】"
+    return table_base
+
+
+@retryOnTimeout()
+def bot(title, detail, mention=True, table=False):
+    """
+    报警机器人
+    """
     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",
@@ -37,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