Skip to content

Commit 4bb21da

Browse files
committed
feat: modify online bot
1 parent 0ca7b67 commit 4bb21da

File tree

1 file changed

+65
-34
lines changed

1 file changed

+65
-34
lines changed

src/memos/memos_tools/dinding_report_bot.py

Lines changed: 65 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@
77
import json
88
import os
99
import time
10+
import traceback
1011
import urllib.parse
1112

1213
from datetime import datetime
1314
from uuid import uuid4
1415

1516
from dotenv import load_dotenv
1617

18+
from memos.log import get_logger
19+
20+
21+
logger = get_logger(__name__)
22+
1723

1824
load_dotenv()
1925

@@ -57,6 +63,20 @@
5763
ROBOT_CODE = os.getenv("DINGDING_ROBOT_CODE")
5864
DING_APP_KEY = os.getenv("DINGDING_APP_KEY")
5965
DING_APP_SECRET = os.getenv("DINGDING_APP_SECRET")
66+
ENV_NAME = os.getenv("ENV_NAME", "PLAYGROUND_OFFLINE")
67+
68+
theme_map = {
69+
"ONLINE": {
70+
"color": "#2196F3", # 天空蓝
71+
"grad": ("#E3F2FD", "#BBDEFB"),
72+
"emoji": "🩵", # 冷静、明亮
73+
},
74+
"OFFLINE": {
75+
"color": "#FFC107", # 柔黄色
76+
"grad": ("#FFF8E1", "#FFECB3"),
77+
"emoji": "🤍", # 轻微提示
78+
},
79+
}
6080

6181

6282
# Get access_token
@@ -311,7 +331,7 @@ def error_bot(
311331
)
312332

313333
# ---------- Markdown ----------
314-
colored_title = f"<font color='{title_color}' size='4'><b>{title}</b></font>"
334+
colored_title = f"<font color='{title_color}' size='4'><b>{ENV_NAME}</b></font>"
315335
at_suffix = ""
316336
if user_ids:
317337
at_suffix = "\n\n" + " ".join([f"@{m}" for m in user_ids])
@@ -367,41 +387,52 @@ def online_bot(
367387
other_data2: dict,
368388
emoji: dict,
369389
):
370-
heading_color = "#00956D" # Green for subtitle
371-
372-
# 0) Banner
373-
banner_bytes = make_header(header_name, sub_title_name)
374-
banner_url = upload_bytes_to_oss(banner_bytes, filename="online_report.png")
375-
376-
# 1) Colored main title
377-
colored_title = f"<font color='{title_color}' size='4'><b>{header_name}</b></font>"
378-
379-
# 3) Markdown
380-
md = "\n\n".join(
381-
filter(
382-
None,
383-
[
384-
f"![banner]({banner_url})",
385-
f"### 🙄 <font color='{heading_color}' size='4'><b>{colored_title}</b></font>\n\n",
386-
_kv_lines(
387-
other_data1,
388-
next(iter(emoji.keys())),
389-
next(iter(emoji.values())),
390-
heading_color=heading_color,
391-
),
392-
_kv_lines(
393-
other_data2,
394-
list(emoji.keys())[1],
395-
list(emoji.values())[1],
396-
heading_color=heading_color,
397-
),
398-
f"<font color='#9E9E9E' size='1'>Time: "
399-
f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}</font>\n",
400-
],
390+
try:
391+
logger.info("in online bot")
392+
theme = "OFFLINE" if "OFFLINE" in ENV_NAME or "TEST" in ENV_NAME else "ONLINE"
393+
style = theme_map.get(theme, theme_map["OFFLINE"])
394+
heading_color = style["color"] # Use theme color for subtitle
395+
396+
# 0) Banner
397+
banner_bytes = make_header(
398+
header_name,
399+
sub_title_name,
400+
colors=style["grad"],
401+
fg=style["color"],
402+
)
403+
banner_url = upload_bytes_to_oss(banner_bytes, filename=f"{ENV_NAME}_online_report.png")
404+
405+
# 1) Colored main title
406+
colored_title = f"<font color='{style['color']}' size='4'><b>{ENV_NAME}</b></font>"
407+
408+
# 3) Markdown
409+
md = "\n\n".join(
410+
filter(
411+
None,
412+
[
413+
f"![banner]({banner_url})",
414+
f"### {style['emoji']} <font color='{heading_color}' size='4'><b>{colored_title}</b></font>\n\n",
415+
_kv_lines(
416+
other_data1,
417+
next(iter(emoji.keys())),
418+
next(iter(emoji.values())),
419+
heading_color=heading_color,
420+
),
421+
_kv_lines(
422+
other_data2,
423+
list(emoji.keys())[1],
424+
list(emoji.values())[1],
425+
heading_color=heading_color,
426+
),
427+
f"<font color='#9E9E9E' size='1'>Time: "
428+
f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}</font>\n",
429+
],
430+
)
401431
)
402-
)
403432

404-
_send_md(colored_title, md, type="user")
433+
_send_md(colored_title, md, type="user")
434+
except Exception:
435+
logger.error(traceback.format_exc())
405436

406437

407438
if __name__ == "__main__":

0 commit comments

Comments
 (0)