chorus/bot/chorus_bot/ping.py
pyr0ball 9971632cd1 fix(bot): fix raw_content round-trip and parsing consistency
- Remove 'or (empty)' fallback in build_ping_embed to preserve empty strings in raw_content
- Fix parse_ping_embed to use direct indexing for all required fields (modality, raw_content, captured_at)
- Remove unused timezone import
- Add test case for empty raw_content round-trip to catch future regressions

Fixes code review findings: empty raw_content was being converted to literal "(empty)" string,
breaking lossless round-trip requirement needed by Task 7's thread-open handler.
2026-07-13 13:56:53 -07:00

38 lines
1.3 KiB
Python

from datetime import datetime
import discord
MODALITY_LABELS = {
"bh_email": "BH Email",
"personal_email": "Personal Email",
"bh_text": "BH Text",
"personal_text": "Personal Text",
"voice": "Voice",
"fb_messenger": "FB Messenger",
"nd_messenger": "ND Messenger",
}
def build_ping_embed(
*, modality: str, sender_id: str | None, snippet: str, raw_content: str,
captured_at: datetime,
) -> discord.Embed:
label = MODALITY_LABELS.get(modality, modality)
embed = discord.Embed(
title=f"{label}" + (f"{sender_id}" if sender_id else ""),
description=snippet,
)
embed.add_field(name="modality", value=modality, inline=True)
embed.add_field(name="sender_id", value=sender_id or "", inline=True)
embed.add_field(name="captured_at", value=captured_at.isoformat(), inline=True)
embed.add_field(name="raw_content", value=raw_content, inline=False)
return embed
def parse_ping_embed(embed: discord.Embed) -> dict:
values = {field.name: field.value for field in embed.fields}
return {
"modality": values["modality"],
"sender_id": values.get("sender_id") or None,
"raw_content": values["raw_content"],
"captured_at": datetime.fromisoformat(values["captured_at"]),
}