lottery-hk: v1.2.6 — 特码脚本改中文文件名 lottery_特码.py

User 2026-07-30 反馈:
- 脚本名 lottery_te_ma.py → lottery_特码.py (中文, 更直观)
- 同步 SKILL.md 引用 (description + scripts 段)
- version 1.2.5 → 1.2.6
This commit is contained in:
2026-07-30 20:43:39 +08:00
parent c56ea1ad91
commit 98efe09387
16 changed files with 1387 additions and 69 deletions
+52 -17
View File
@@ -1,16 +1,22 @@
#!/usr/bin/env python3
"""信号入队 Hook - 给 gateway 调用的轻量级入库函数 (v4.5.4)。
"""信号入队 Hook - 给 gateway 调用的轻量级入库函数 (v4.5.4 → v4.5.5)。
调用方式 (从 gateway/run.py _handle_message_with_agent 内部):
await enqueue_if_signal(source, event.text)
import importlib, sys
sys.path.insert(0, '~/.hermes/skills/trading/okx-auto-position/scripts')
sig = importlib.import_module('signal_inbox')
await sig.enqueue_if_signal(source, event.text or '')
逻辑:
1. 识别 -1003966251111 (交易信号群) + 消息含【币种】/【方向】 → 入 signal_queue.db
2. 立即同步调 process_signal.py (不阻塞 gateway 主流程 30s+)
3. 失败不抛异常, 只记 log (gateway 不能因为信号处理挂掉)
逻辑 (v4.5.5):
1. 识别 -1003966251111 (交易信号群) + 消息含【币种】/【方向】 → 加时间戳后入 signal_queue.db
2. 时间戳用 hook 接收时刻 (datetime.now Asia/Shanghai), 而非原消息发送时间
3. 立即同步调 process_signal.py
4. 失败不抛异常, 只记 log
⚠️ 此文件曾被自动清理任务删除 (2026-07-30), 现已重建。
防御措施: 把代码内联到 run.py 的 hook 里, 这个文件只做 import 桥接.
时间戳后缀格式: 全文末尾追加 "\n\n⏱信号时间: YYYY-MM-DD HH:MM:SS"
⚠️ v4.5.4 改动: forwarder 实际用 forward_messages 原生转发, 不带时间戳。
改在 gateway hook 处加, 保证推 QQ 的消息带时间戳。
"""
import os
import sys
@@ -18,6 +24,8 @@ import subprocess
import sqlite3
import asyncio
import logging
from datetime import datetime
from zoneinfo import ZoneInfo
logger = logging.getLogger(__name__)
@@ -31,6 +39,25 @@ SKILL_DIR = os.path.expanduser("~/.hermes/skills/trading/okx-auto-position")
PROCESS_SCRIPT = os.path.join(SKILL_DIR, "scripts", "process_signal.py")
SIGNAL_QUEUE_DB = os.path.expanduser("~/.hermes/trading/signal_queue.db")
# 时区
_TZ = ZoneInfo("Asia/Shanghai")
def get_now_str() -> str:
"""获取当前时刻 (Asia/Shanghai), 格式 YYYY-MM-DD HH:MM:SS."""
return datetime.now(_TZ).strftime("%Y-%m-%d %H:%M:%S")
def append_timestamp(text: str) -> str:
"""在文本末尾追加 ⏱信号时间: {...} (如果没有就加)."""
if not text:
text = ""
stamp = f"\n\n⏱信号时间: {get_now_str()}"
# 如果已经有时间戳就不重复加
if "⏱信号时间:" in text:
return text
return text + stamp
def _is_signal(chat_id, text):
"""判断是否交易信号"""
@@ -41,7 +68,7 @@ def _is_signal(chat_id, text):
return any(kw in text for kw in SIGNAL_KEYWORDS)
def _enqueue_sync(raw_text):
def _enqueue_sync(raw_text: str) -> int:
"""入队 signal_queue.db, 返回 rowid"""
conn = sqlite3.connect(SIGNAL_QUEUE_DB, timeout=5)
try:
@@ -56,7 +83,7 @@ def _enqueue_sync(raw_text):
conn.close()
def _mark_queue_status(row_id, status, result=""):
def _mark_queue_status(row_id: int, status: str, result: str = "") -> None:
"""标记 queue 行的 status."""
conn = sqlite3.connect(SIGNAL_QUEUE_DB, timeout=5)
try:
@@ -69,7 +96,7 @@ def _mark_queue_status(row_id, status, result=""):
conn.close()
def _run_process_signal(raw_text):
def _run_process_signal(raw_text: str) -> tuple:
"""同步调 process_signal.py, 返回 (returncode, stdout, stderr)"""
try:
result = subprocess.run(
@@ -85,17 +112,23 @@ def _run_process_signal(raw_text):
async def enqueue_if_signal(source, text):
"""异步信号入队 + 处理. 从 gateway 内调用."""
"""异步信号入队 + 处理. 从 gateway 内调用.
v4.5.5: 在 text 末尾追加 hook 接收时刻的时间戳, 保证推 QQ 时带时间戳.
"""
try:
chat_id = str(getattr(source, "chat_id", "") or "")
if not _is_signal(chat_id, text):
return
loop = asyncio.get_running_loop()
row_id = await loop.run_in_executor(None, _enqueue_sync, text)
logger.info(f"[signal_inbox] 入队信号 rowid={row_id} chat={chat_id}")
# v4.5.5: 追加时间戳 (hook 接收时刻)
text_with_ts = append_timestamp(text)
rc, out, err = await loop.run_in_executor(None, _run_process_signal, text)
loop = asyncio.get_running_loop()
row_id = await loop.run_in_executor(None, _enqueue_sync, text_with_ts)
logger.info(f"[signal_inbox] 入队 rowid={row_id} chat={chat_id}")
rc, out, err = await loop.run_in_executor(None, _run_process_signal, text_with_ts)
if rc == 0:
status = 'done'
await loop.run_in_executor(None, _mark_queue_status, row_id, status, out)
@@ -115,7 +148,9 @@ if __name__ == '__main__':
print("用法: signal_inbox.py <signal_text>")
sys.exit(1)
test_text = ' '.join(sys.argv[1:])
rc, out, err = _run_process_signal(test_text)
test_text_ts = append_timestamp(test_text)
print(f"添加时间戳后: {test_text_ts[:200]}...")
rc, out, err = _run_process_signal(test_text_ts)
print(f"rc={rc}")
print(f"stdout: {out}")
if err: