test_send.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env python3
  2. """发送 Discord 消息到 RobotDaily 频道."""
  3. import requests
  4. import os
  5. def send_discord_message(channel_id: str, message: str) -> bool:
  6. """通过 Discord API 发送消息."""
  7. # 读取配置文件中的 Discord 令牌
  8. env_file = "/home/zhn/.openclaw/workspace/skills/robdaily/arxiv-digest/.env"
  9. if not os.path.exists(env_file):
  10. print(f"❌ 配置文件不存在:{env_file}")
  11. return False
  12. discord_token = None
  13. with open(env_file, 'r') as f:
  14. for line in f:
  15. if line.startswith('DISCORD_BOT_TOKEN='):
  16. discord_token = line.split('=', 1)[1].strip()
  17. break
  18. if not discord_token or discord_token == "your_bot_token_here":
  19. print("❌ 未配置 Discord 令牌,请编辑 .env 文件")
  20. return False
  21. url = f"https://discord.com/api/v10/channels/{channel_id}/messages"
  22. headers = {
  23. "Authorization": f"Bot {discord_token}",
  24. "Content-Type": "application/json",
  25. }
  26. data = {"content": message}
  27. try:
  28. response = requests.post(url, json=data, headers=headers, timeout=10)
  29. response.raise_for_status()
  30. print(f"✅ 消息已发送到 Discord 频道 {channel_id}")
  31. return True
  32. except requests.exceptions.RequestException as e:
  33. print(f"❌ Discord 消息发送失败:{e}")
  34. return False
  35. if __name__ == "__main__":
  36. channel_id = "1481632217930141697"
  37. message = """📰 RobotDaily 2026-03-16
  38. 今日精选 7 篇论文:
  39. • 具身智能:2 篇
  40. • 表征学习:3 篇
  41. • 强化学习:2 篇
  42. 🔗 查看完整简报:
  43. https://indigofloyd.space/ai-daily/2026-03-16/
  44. 祝有充实的一天!✨"""
  45. send_discord_message(channel_id, message)