fix: always use MarkdownV2 for all message types
This commit is contained in:
26
_test_tg.py
Normal file
26
_test_tg.py
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
import yaml, requests, json
|
||||||
|
|
||||||
|
with open("config.yaml", "r", encoding="utf-8") as f:
|
||||||
|
cfg = yaml.safe_load(f)
|
||||||
|
|
||||||
|
token = cfg["telegram"]["bot_token"]
|
||||||
|
chat_id = cfg["telegram"]["chat_id"]
|
||||||
|
|
||||||
|
# 测试1: 纯文本无 parse_mode
|
||||||
|
payload = {
|
||||||
|
"chat_id": chat_id,
|
||||||
|
"text": "🔑 Kraken 701383\n账户: test@test.com",
|
||||||
|
"reply_markup": {"inline_keyboard": [[{"text": "查看原文", "callback_data": "v"}, {"text": "回复", "callback_data": "r"}]]},
|
||||||
|
}
|
||||||
|
print("=== Test 1: plain text, no parse_mode ===")
|
||||||
|
r = requests.post(f"https://api.telegram.org/bot{token}/sendMessage", json=payload, timeout=10)
|
||||||
|
print(r.status_code, r.text[:300])
|
||||||
|
|
||||||
|
# 测试2: 不带 reply_markup
|
||||||
|
payload2 = {
|
||||||
|
"chat_id": chat_id,
|
||||||
|
"text": "🔑 Kraken 701383\n账户: test@test.com",
|
||||||
|
}
|
||||||
|
print("\n=== Test 2: no keyboard ===")
|
||||||
|
r2 = requests.post(f"https://api.telegram.org/bot{token}/sendMessage", json=payload2, timeout=10)
|
||||||
|
print(r2.status_code, r2.text[:300])
|
||||||
23
_test_verify.py
Normal file
23
_test_verify.py
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import json
|
||||||
|
from src.tg_bot import format_summary, _summary_keyboard
|
||||||
|
|
||||||
|
data = {
|
||||||
|
'subject': '确认码',
|
||||||
|
'recipient': 'test@test.com',
|
||||||
|
'sender': 'Kraken <noreply@kraken.com>',
|
||||||
|
'category': 'notification',
|
||||||
|
'summary': 'Kraken确认码',
|
||||||
|
'verification_code': 'Kraken 701383',
|
||||||
|
'links': [],
|
||||||
|
'can_reply': False,
|
||||||
|
'reply_suggestions': []
|
||||||
|
}
|
||||||
|
|
||||||
|
text = format_summary(data, 'test@test.com')
|
||||||
|
kb = _summary_keyboard(False, False, [])
|
||||||
|
|
||||||
|
print('=== text ===')
|
||||||
|
print(repr(text))
|
||||||
|
print()
|
||||||
|
print('=== keyboard ===')
|
||||||
|
print(json.dumps(kb, ensure_ascii=False, indent=2))
|
||||||
@@ -23,17 +23,13 @@ def send_summary(tg_cfg: TelegramConfig, chat_id: str, summary_text: str,
|
|||||||
original_reply_to: str = "", account_email: str = "") -> int:
|
original_reply_to: str = "", account_email: str = "") -> int:
|
||||||
can_reply = summary_data.get("can_reply", True)
|
can_reply = summary_data.get("can_reply", True)
|
||||||
is_promotion = summary_data.get("category") == "promotion"
|
is_promotion = summary_data.get("category") == "promotion"
|
||||||
is_verification = summary_data.get("category") == "notification" and summary_data.get("verification_code")
|
|
||||||
plain_text = is_promotion or is_verification
|
|
||||||
links = summary_data.get("links", [])
|
links = summary_data.get("links", [])
|
||||||
msg_payload = {
|
result = _tg_req(tg_cfg, "sendMessage", {
|
||||||
"chat_id": chat_id,
|
"chat_id": chat_id,
|
||||||
"text": summary_text,
|
"text": summary_text,
|
||||||
|
"parse_mode": "MarkdownV2",
|
||||||
"reply_markup": _summary_keyboard(can_reply, is_promotion, links),
|
"reply_markup": _summary_keyboard(can_reply, is_promotion, links),
|
||||||
}
|
})
|
||||||
if not plain_text:
|
|
||||||
msg_payload["parse_mode"] = "MarkdownV2"
|
|
||||||
result = _tg_req(tg_cfg, "sendMessage", msg_payload)
|
|
||||||
msg_id = result["result"]["message_id"]
|
msg_id = result["result"]["message_id"]
|
||||||
_email_contexts[msg_id] = {
|
_email_contexts[msg_id] = {
|
||||||
"summary_text": summary_text,
|
"summary_text": summary_text,
|
||||||
|
|||||||
Reference in New Issue
Block a user