whatsapp fixes
This commit is contained in:
parent
ff01efe4bb
commit
248acba741
5 changed files with 425 additions and 38 deletions
|
|
@ -24,6 +24,7 @@ class SendAgentMessagePayload(BaseModel):
|
|||
customer_name: str = Field(min_length=1, max_length=120)
|
||||
report_group: str = Field(min_length=3, max_length=50)
|
||||
report_id: str | None = Field(default=None, max_length=120)
|
||||
report_url: str | None = Field(default=None, max_length=500)
|
||||
|
||||
|
||||
def serialize_agent_template(template: AgentMessageTemplate, *, subscribed: bool = False) -> dict:
|
||||
|
|
@ -97,31 +98,46 @@ def _validate_report_id(report_id: str) -> str:
|
|||
return cleaned
|
||||
|
||||
|
||||
def _build_report_url(
|
||||
template: AgentMessageTemplate,
|
||||
report_group: str,
|
||||
report_id: str,
|
||||
override: str | None = None,
|
||||
) -> str:
|
||||
if override:
|
||||
return override.strip()
|
||||
base = (template.button_url_base or "https://urmiai.com/reports/").rstrip("/")
|
||||
return f"{base}/{report_group}/{report_id}"
|
||||
|
||||
|
||||
def _build_template_components(
|
||||
template: AgentMessageTemplate,
|
||||
customer_name: str,
|
||||
report_group: str,
|
||||
report_id: str,
|
||||
report_url: str,
|
||||
) -> list[dict]:
|
||||
"""Build Meta template components using positional variables.
|
||||
|
||||
agent_run_completion uses:
|
||||
body {{1}} -> customer name
|
||||
button {{1}} -> report URL path suffix OR full URL depending on Meta template config.
|
||||
We pass the full hosted report URL for the button so the View Report link resolves correctly.
|
||||
"""
|
||||
components: list[dict] = [
|
||||
{
|
||||
"type": "body",
|
||||
"parameters": [
|
||||
{
|
||||
"type": "text",
|
||||
"parameter_name": "customer_name",
|
||||
"text": customer_name.strip(),
|
||||
}
|
||||
],
|
||||
"parameters": [{"type": "text", "text": customer_name.strip()}],
|
||||
}
|
||||
]
|
||||
if template.button_label and template.button_url_base:
|
||||
# Meta URL button: pass path suffix relative to button_url_base (e.g. desi-clans-test/2026-10-03)
|
||||
base = template.button_url_base.rstrip("/")
|
||||
button_param = report_url[len(base) + 1 :] if report_url.startswith(f"{base}/") else report_url
|
||||
components.append(
|
||||
{
|
||||
"type": "button",
|
||||
"sub_type": "url",
|
||||
"index": "0",
|
||||
"parameters": [{"type": "text", "text": f"{report_group}/{report_id}"}],
|
||||
"parameters": [{"type": "text", "text": button_param}],
|
||||
}
|
||||
)
|
||||
return components
|
||||
|
|
@ -148,7 +164,8 @@ async def send_agent_template_message(
|
|||
|
||||
report_group = _validate_report_group(payload.report_group)
|
||||
report_id = _validate_report_id(payload.report_id or f"run-{secrets.token_hex(3)}")
|
||||
components = _build_template_components(template, payload.customer_name, report_group, report_id)
|
||||
report_url = _build_report_url(template, report_group, report_id, payload.report_url)
|
||||
components = _build_template_components(template, payload.customer_name, report_url)
|
||||
|
||||
client = _whatsapp_client(db)
|
||||
try:
|
||||
|
|
@ -170,6 +187,7 @@ async def send_agent_template_message(
|
|||
"customer_name": payload.customer_name.strip(),
|
||||
"report_group": report_group,
|
||||
"report_id": report_id,
|
||||
"report_url": report_url,
|
||||
},
|
||||
wa_message_id=wa_message_id,
|
||||
status="sent",
|
||||
|
|
@ -179,11 +197,6 @@ async def send_agent_template_message(
|
|||
db.commit()
|
||||
db.refresh(log)
|
||||
|
||||
report_url = (
|
||||
f"{template.button_url_base.rstrip('/')}/{report_group}/{report_id}"
|
||||
if template.button_url_base
|
||||
else None
|
||||
)
|
||||
return {
|
||||
"success": True,
|
||||
"template": template.slug,
|
||||
|
|
@ -279,22 +292,24 @@ def build_agent_message_docs(template: AgentMessageTemplate) -> dict:
|
|||
"customer_name": "Recipient name inserted into the template body",
|
||||
"report_group": "Urmiport report group slug used in the hosted report URL",
|
||||
"report_id": "Optional report postfix; auto-generated when omitted",
|
||||
"report_url": "Optional full report URL override for the WhatsApp button (auto-built when omitted)",
|
||||
},
|
||||
"example": {
|
||||
"template": template.slug,
|
||||
"to": "919149154337",
|
||||
"customer_name": "Suyash",
|
||||
"report_group": "acme-agent-runs",
|
||||
"report_id": "run-abc123",
|
||||
"report_group": "desi-clans-test",
|
||||
"report_id": "2026-10-03",
|
||||
"report_url": "https://urmiai.com/reports/desi-clans-test/2026-10-03",
|
||||
},
|
||||
"response_example": {
|
||||
"success": True,
|
||||
"template": template.slug,
|
||||
"to": "919149154337",
|
||||
"customer_name": "Suyash",
|
||||
"report_group": "acme-agent-runs",
|
||||
"report_id": "run-abc123",
|
||||
"report_url": f"{template.button_url_base or ''}acme-agent-runs/run-abc123",
|
||||
"report_group": "desi-clans-test",
|
||||
"report_id": "2026-10-03",
|
||||
"report_url": "https://urmiai.com/reports/desi-clans-test/2026-10-03",
|
||||
"wa_message_id": "wamid.HBg...",
|
||||
"dispatch_id": "00000000-0000-0000-0000-000000000001",
|
||||
},
|
||||
|
|
@ -304,5 +319,6 @@ def build_agent_message_docs(template: AgentMessageTemplate) -> dict:
|
|||
"Messages are sent from the Urmi AI WhatsApp business number on your behalf.",
|
||||
"Create the report group in Console → Agent → Urmiport before sending.",
|
||||
"If report_id is omitted, Urmi generates a unique postfix automatically.",
|
||||
"The WhatsApp button uses positional template variables: body {{1}} = customer_name, button suffix = report_group/report_id.",
|
||||
],
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue