fix: align reaction mapping across platforms (#139)

解决 TG 无法贴表情的 bug
This commit is contained in:
clown145
2026-03-31 00:04:13 +08:00
committed by GitHub
parent b041a7ca9e
commit 697213826c
4 changed files with 66 additions and 26 deletions
+4 -4
View File
@@ -522,8 +522,8 @@ class GroupDailyAnalysis(Star):
yield event.plain_result("🔍 正在启动分析引擎,正在拉取最近消息...")
elif adapter and orig_msg_id:
await adapter.set_reaction(
event.get_group_id(), orig_msg_id, "🔍"
) # 🔍
event.get_group_id(), orig_msg_id, "analysis_started"
)
# 调用 DDD 应用级服务
result = await self.analysis_service.execute_daily_analysis(
@@ -540,8 +540,8 @@ class GroupDailyAnalysis(Star):
if not use_text_reply and adapter and orig_msg_id:
await adapter.set_reaction(
event.get_group_id(), orig_msg_id, "📊"
) # 📊
event.get_group_id(), orig_msg_id, "analysis_done"
)
async for res in self._send_analysis_report(event, result):
yield res
@@ -764,11 +764,14 @@ class DiscordAdapter(PlatformAdapter):
return False
try:
# 映射常见的表情 ID 为文字表情,使分析状态在跨平台保持一致
mapping = {289: "🔍", 424: "📊", 124: ""}
emoji_to_use = emoji
if isinstance(emoji, int) or (isinstance(emoji, str) and emoji.isdigit()):
emoji_to_use = mapping.get(int(emoji), emoji)
reaction_key = str(emoji)
emoji_to_use = {
"analysis_started": "🔍",
"analysis_done": "📊",
"289": "🔍",
"124": "📊",
"424": "📊",
}.get(reaction_key, reaction_key)
channel_id = int(group_id)
channel = self._discord_client.get_channel(channel_id)
@@ -1377,12 +1377,13 @@ class OneBotAdapter(PlatformAdapter):
支持 Go-CQHTTP, NapCat, Lagrange 等 OneBot 实现。
"""
try:
# 语义化映射:根据用户喜好精细化 OneBot 端的降级
emoji_id = str(emoji)
if str(emoji) == "🔍":
emoji_id = "289" # 🫣 表情 (表示任务已接收)
elif str(emoji) == "📊":
emoji_id = "124" # 👌 表情 (表示任务处理完成)
reaction_key = str(emoji)
emoji_id = {
"analysis_started": "289", # 🫣 表情 (表示任务已接收)
"analysis_done": "124", # 👌 表情 (表示任务处理完成)
"🔍": "289",
"📊": "124",
}.get(reaction_key, reaction_key)
await self.bot.call_action(
"set_msg_emoji_like",
@@ -909,34 +909,70 @@ class TelegramAdapter(PlatformAdapter):
return False
try:
# 映射常见的表情 ID 为文字表情
mapping = {289: "🔍", 424: "📊", 124: ""}
emoji_to_use = emoji
if isinstance(emoji, int) or (isinstance(emoji, str) and emoji.isdigit()):
emoji_to_use = mapping.get(int(emoji), emoji)
chat_id, _ = self._parse_group_id(group_id)
# 只有开启了库支持且版本符合时才尝试。set_message_reaction 是 Bot API 7.0 (PTB 20.8+) 特性。
if hasattr(client, "set_message_reaction"):
if not hasattr(client, "set_message_reaction"):
return False
if not is_add:
try:
from telegram import ReactionTypeEmoji
reaction = [ReactionTypeEmoji(emoji=emoji_to_use)] if is_add else []
await client.set_message_reaction(
chat_id=chat_id,
message_id=int(message_id),
reaction=reaction,
reaction=[],
)
return True
except ImportError:
# 如果版本太低没有 ReactionTypeEmoji,尝试直接传字符串 (有些实现支持)
await client.set_message_reaction(
chat_id=chat_id,
message_id=int(message_id),
reaction=emoji_to_use if is_add else None,
reaction=None,
)
return True
reaction_key = str(emoji)
candidates = {
"analysis_started": ("👀", "🤔", "👍"),
"analysis_done": ("👌", "👍", "🎉"),
"🔍": ("👀", "🤔", "👍"),
"📊": ("👌", "👍", "🎉"),
"289": ("👀", "🤔", "👍"),
"124": ("👌", "👍", "🎉"),
"424": ("👌", "👍", "🎉"),
"": ("👌", "👍", "🎉"),
}.get(reaction_key, (reaction_key,))
try:
from telegram import ReactionTypeEmoji
for candidate in candidates:
try:
await client.set_message_reaction(
chat_id=chat_id,
message_id=int(message_id),
reaction=[ReactionTypeEmoji(emoji=candidate)],
)
return True
except Exception:
continue
except ImportError:
for candidate in candidates:
try:
await client.set_message_reaction(
chat_id=chat_id,
message_id=int(message_id),
reaction=candidate,
)
return True
except Exception:
continue
logger.debug(
f"[Telegram] set_reaction 未匹配到可用表情: emoji={emoji}, candidates={candidates}"
)
return False
except Exception as e:
logger.debug(f"[Telegram] set_reaction 失败: {e}")