feat(bot): add inbox ping embed builder/parser
This commit is contained in:
parent
669bb3456a
commit
af547ab02c
2 changed files with 71 additions and 0 deletions
38
bot/chorus_bot/ping.py
Normal file
38
bot/chorus_bot/ping.py
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
from datetime import datetime, timezone
|
||||
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 or "(empty)", 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.get("modality"),
|
||||
"sender_id": values.get("sender_id") or None,
|
||||
"raw_content": values.get("raw_content"),
|
||||
"captured_at": datetime.fromisoformat(values["captured_at"]),
|
||||
}
|
||||
33
bot/tests/test_ping.py
Normal file
33
bot/tests/test_ping.py
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
from datetime import datetime, timezone
|
||||
from chorus_bot import ping
|
||||
|
||||
|
||||
def test_build_and_parse_round_trip():
|
||||
captured_at = datetime(2026, 7, 13, 7, 0, 0, tzinfo=timezone.utc)
|
||||
embed = ping.build_ping_embed(
|
||||
modality="bh_email",
|
||||
sender_id="jane@example.com",
|
||||
snippet="I have some books to donate...",
|
||||
raw_content="I have some books to donate. Full text goes here.",
|
||||
captured_at=captured_at,
|
||||
)
|
||||
assert "bh_email" in embed.title or "bh_email" in str(embed.fields)
|
||||
assert "I have some books to donate..." in (embed.description or "")
|
||||
|
||||
parsed = ping.parse_ping_embed(embed)
|
||||
assert parsed["modality"] == "bh_email"
|
||||
assert parsed["sender_id"] == "jane@example.com"
|
||||
assert parsed["raw_content"] == "I have some books to donate. Full text goes here."
|
||||
assert parsed["captured_at"] == captured_at
|
||||
|
||||
|
||||
def test_build_ping_embed_handles_missing_sender():
|
||||
embed = ping.build_ping_embed(
|
||||
modality="voice",
|
||||
sender_id=None,
|
||||
snippet="call about donation",
|
||||
raw_content="call about donation",
|
||||
captured_at=datetime(2026, 7, 13, 8, 0, 0, tzinfo=timezone.utc),
|
||||
)
|
||||
parsed = ping.parse_ping_embed(embed)
|
||||
assert parsed["sender_id"] is None
|
||||
Loading…
Reference in a new issue