send_discord_link.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env python3
  2. """Send brief link message to Discord RobotDaily channel."""
  3. from __future__ import annotations
  4. import argparse
  5. import os
  6. import sys
  7. from pathlib import Path
  8. from utils import load_env, log
  9. def send_digest_link(enriched: dict, hugo_url: str, dry_run: bool = False) -> str:
  10. """Send brief link message to Discord RobotDaily channel."""
  11. counts = enriched.get("counts", {})
  12. embodied = counts.get("embodied", 0)
  13. representation = counts.get("representation", 0)
  14. reinforcement = counts.get("reinforcement", 0)
  15. total = embodied + representation + reinforcement
  16. message = f"""📰 RobotDaily {enriched.get('date', 'today')}
  17. 今日精选 {total} 篇论文:
  18. • 具身智能:{embodied} 篇
  19. • 表征学习:{representation} 篇
  20. • 强化学习:{reinforcement} 篇
  21. 🔗 查看完整简报:
  22. {hugo_url}
  23. 祝有充实的一天!✨"""
  24. if dry_run:
  25. log(f"[DRY RUN] 将要发送的消息:")
  26. log(message)
  27. return message
  28. # 读取环境配置
  29. env = load_env()
  30. discord_token = env.get("DISCORD_BOT_TOKEN", "")
  31. channel_id = env.get("DISCORD_ROBOTDAILY_CHANNEL_ID", "")
  32. if not discord_token:
  33. log("❌ 未设置 DISCORD_BOT_TOKEN")
  34. return ""
  35. if not channel_id:
  36. log("❌ 未设置 DISCORD_ROBOTDAILY_CHANNEL_ID")
  37. return ""
  38. # Discord API 发送消息
  39. import requests
  40. url = f"https://discord.com/api/v10/channels/{channel_id}/messages"
  41. headers = {
  42. "Authorization": f"Bot {discord_token}",
  43. "Content-Type": "application/json",
  44. }
  45. data = {"content": message}
  46. try:
  47. response = requests.post(url, json=data, headers=headers, timeout=10)
  48. response.raise_for_status()
  49. log(f"✅ 消息已发送到 Discord 频道 {channel_id}")
  50. return message
  51. except requests.exceptions.RequestException as e:
  52. log(f"❌ Discord 消息发送失败:{e}")
  53. return ""
  54. def main():
  55. parser = argparse.ArgumentParser(description="Send brief link message to Discord")
  56. parser.add_argument("--hugo-url", required=True, help="Hugo 网站链接")
  57. parser.add_argument("--embodied", type=int, required=True, help="具身智能论文数量")
  58. parser.add_argument("--representation", type=int, required=True, help="表征学习论文数量")
  59. parser.add_argument("--reinforcement", type=int, required=True, help="强化学习论文数量")
  60. parser.add_argument("--total", type=int, required=True, help="总论文数量")
  61. parser.add_argument("--date", required=True, help="日期")
  62. parser.add_argument("--dry-run", action="store_true", help="dry run 模式")
  63. args = parser.parse_args()
  64. enriched = {
  65. "date": args.date,
  66. "counts": {
  67. "embodied": args.embodied,
  68. "representation": args.representation,
  69. "reinforcement": args.reinforcement,
  70. }
  71. }
  72. send_digest_link(enriched, args.hugo_url, args.dry_run)
  73. if __name__ == "__main__":
  74. main()