83 lines
2.4 KiB
Python
83 lines
2.4 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
发文本到卡若日志飞书群(webhook)。
|
||
用法:
|
||
python3 post_to_feishu.py "任意文本"
|
||
python3 post_to_feishu.py --release 9.23 --title "9.23 第110场|Soul变现逻辑全程公开"
|
||
"""
|
||
import argparse
|
||
import json
|
||
import os
|
||
from pathlib import Path
|
||
from urllib.request import Request, urlopen
|
||
|
||
CONFIG_PATH = Path(__file__).resolve().parent / "feishu_publish_config.json"
|
||
WEBHOOK_ENV = "FEISHU_KARUO_LOG_WEBHOOK"
|
||
DEFAULT_WEBHOOK = "https://open.feishu.cn/open-apis/bot/v2/hook/8b7f996e-2892-4075-989f-aa5593ea4fbc"
|
||
WIKI_URL = "https://cunkebao.feishu.cn/wiki/FNP6wdvNKij7yMkb3xCce0CYnpd"
|
||
MINIPROGRAM_BASE = "https://soul.quwanzhi.com/read"
|
||
MATERIAL_HINT = "材料找卡若AI拿"
|
||
|
||
|
||
def load_webhook():
|
||
if os.environ.get(WEBHOOK_ENV):
|
||
return os.environ.get(WEBHOOK_ENV)
|
||
if CONFIG_PATH.exists():
|
||
with open(CONFIG_PATH, "r", encoding="utf-8") as f:
|
||
data = json.load(f)
|
||
return data.get("feishu_karuo_log_webhook") or data.get("webhook")
|
||
return DEFAULT_WEBHOOK
|
||
|
||
|
||
def send_text(text: str) -> bool:
|
||
webhook = load_webhook()
|
||
if not webhook:
|
||
print("未配置 webhook,请设置 feishu_publish_config.json 或 FEISHU_KARUO_LOG_WEBHOOK")
|
||
return False
|
||
payload = {"msg_type": "text", "content": {"text": text}}
|
||
req = Request(
|
||
webhook,
|
||
data=json.dumps(payload).encode("utf-8"),
|
||
headers={"Content-Type": "application/json"},
|
||
method="POST",
|
||
)
|
||
try:
|
||
with urlopen(req, timeout=10) as resp:
|
||
r = json.loads(resp.read().decode())
|
||
if r.get("code") != 0:
|
||
print("飞书返回:", r)
|
||
return False
|
||
print("已发送到卡若日志飞书群")
|
||
return True
|
||
except Exception as e:
|
||
print("发送失败:", e)
|
||
return False
|
||
|
||
|
||
def main():
|
||
parser = argparse.ArgumentParser()
|
||
parser.add_argument("text", nargs="?", help="要发送的文本")
|
||
parser.add_argument("--release", help="章节 id,如 9.23")
|
||
parser.add_argument("--title", help="文章标题")
|
||
args = parser.parse_args()
|
||
|
||
if args.release:
|
||
title = args.title or args.release
|
||
text = f"""📢 新发布
|
||
|
||
{title}
|
||
|
||
📱 小程序:{MINIPROGRAM_BASE}/{args.release}
|
||
📚 飞书目录:{WIKI_URL}
|
||
|
||
{MATERIAL_HINT}"""
|
||
send_text(text)
|
||
elif args.text:
|
||
send_text(args.text)
|
||
else:
|
||
parser.print_help()
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|