meta api fix

This commit is contained in:
debu-2 2026-10-03 14:35:23 +05:30
parent 6810f392cc
commit e98036cd31
5 changed files with 33 additions and 50 deletions

View file

@ -24,7 +24,6 @@ class SendAgentMessagePayload(BaseModel):
customer_name: str = Field(min_length=1, max_length=120) customer_name: str = Field(min_length=1, max_length=120)
report_group: str = Field(min_length=3, max_length=50) report_group: str = Field(min_length=3, max_length=50)
report_id: str | None = Field(default=None, max_length=120) 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: def serialize_agent_template(template: AgentMessageTemplate, *, subscribed: bool = False) -> dict:
@ -98,14 +97,7 @@ def _validate_report_id(report_id: str) -> str:
return cleaned return cleaned
def _build_report_url( def _report_url(template: AgentMessageTemplate, report_group: str, report_id: str) -> str:
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("/") base = (template.button_url_base or "https://urmiai.com/reports/").rstrip("/")
return f"{base}/{report_group}/{report_id}" return f"{base}/{report_group}/{report_id}"
@ -113,12 +105,13 @@ def _build_report_url(
def _build_template_components( def _build_template_components(
template: AgentMessageTemplate, template: AgentMessageTemplate,
customer_name: str, customer_name: str,
report_url: str, report_group: str,
report_id: str,
) -> list[dict]: ) -> list[dict]:
"""Build Meta template components for agent_run_completion. """Build Meta components for agent_run_completion_v2.
Meta template uses parameter_format=NAMED for the body (customer_name) and a Body uses named customer_name. Button suffix is report_group/report_id only —
positional URL-button suffix for the View Report link. never derived from a posted report_url.
""" """
components: list[dict] = [ components: list[dict] = [
{ {
@ -133,14 +126,12 @@ def _build_template_components(
} }
] ]
if template.button_label and template.button_url_base: if template.button_label and template.button_url_base:
base = template.button_url_base.rstrip("/")
button_param = report_url[len(base) + 1 :] if report_url.startswith(f"{base}/") else report_url
components.append( components.append(
{ {
"type": "button", "type": "button",
"sub_type": "url", "sub_type": "url",
"index": "0", "index": "0",
"parameters": [{"type": "text", "text": button_param}], "parameters": [{"type": "text", "text": f"{report_group}/{report_id}"}],
} }
) )
return components return components
@ -167,8 +158,8 @@ async def send_agent_template_message(
report_group = _validate_report_group(payload.report_group) report_group = _validate_report_group(payload.report_group)
report_id = _validate_report_id(payload.report_id or f"run-{secrets.token_hex(3)}") report_id = _validate_report_id(payload.report_id or f"run-{secrets.token_hex(3)}")
report_url = _build_report_url(template, report_group, report_id, payload.report_url) components = _build_template_components(template, payload.customer_name, report_group, report_id)
components = _build_template_components(template, payload.customer_name, report_url) report_url = _report_url(template, report_group, report_id)
client = _whatsapp_client(db) client = _whatsapp_client(db)
try: try:
@ -295,7 +286,6 @@ def build_agent_message_docs(template: AgentMessageTemplate) -> dict:
"customer_name": "Recipient name inserted into the template body", "customer_name": "Recipient name inserted into the template body",
"report_group": "Urmiport report group slug used in the hosted report URL", "report_group": "Urmiport report group slug used in the hosted report URL",
"report_id": "Optional report postfix; auto-generated when omitted", "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": { "example": {
"template": template.slug, "template": template.slug,
@ -303,7 +293,6 @@ def build_agent_message_docs(template: AgentMessageTemplate) -> dict:
"customer_name": "Suyash", "customer_name": "Suyash",
"report_group": "desi-clans-test", "report_group": "desi-clans-test",
"report_id": "2026-10-03", "report_id": "2026-10-03",
"report_url": "https://urmiai.com/reports/desi-clans-test/2026-10-03",
}, },
"response_example": { "response_example": {
"success": True, "success": True,
@ -322,6 +311,6 @@ def build_agent_message_docs(template: AgentMessageTemplate) -> dict:
"Messages are sent from the Urmi AI WhatsApp business number on your behalf.", "Messages are sent from the Urmi AI WhatsApp business number on your behalf.",
"Create the report group in Console → Agent → Urmiport before sending.", "Create the report group in Console → Agent → Urmiport before sending.",
"If report_id is omitted, Urmi generates a unique postfix automatically.", "If report_id is omitted, Urmi generates a unique postfix automatically.",
"Body uses named parameter customer_name; button URL suffix is report_group/report_id.", "Uses Meta template agent_run_completion_v2. Button suffix is built from report_group/report_id only.",
], ],
} }

View file

@ -11,29 +11,36 @@ DEFAULT_AGENT_TEMPLATES = [
"Notify end users when an Urmi AI agent run completes, with a dynamic " "Notify end users when an Urmi AI agent run completes, with a dynamic "
"View Report button linking to the completion report." "View Report button linking to the completion report."
), ),
"header_text": "Agent Run Complete", "header_text": "Agent Run Successful",
"body_preview": ( "body_preview": (
"Hello {{customer_name}}, your agent run is complete, you can access " "Hello {{customer_name}}, your agent run is complete, you can access "
"the completion report at your URMI AI Portal." "the completion report at your URMI AI Portal."
), ),
"footer_text": "Thanks and regards, team - URMI AI", "footer_text": "Thanks and regards, team - URMI AI",
"meta_template_name": "agent_run_completion", "meta_template_name": "agent_run_completion_v2",
"language": "en", "language": "en",
"category": "utility", "category": "utility",
"parameters": [ "parameters": [
{ {
"name": "{{1}}", "name": "customer_name",
"type": "string", "type": "string",
"required": True, "required": True,
"description": "Positional body variable — recipient display name", "description": "Recipient display name (template body variable)",
"example": "Suyash", "example": "Suyash",
}, },
{ {
"name": "{{2}}", "name": "report_group",
"type": "string", "type": "string",
"required": True, "required": True,
"description": "Positional button variable — report path suffix (report_group/report_id)", "description": "Urmiport report group slug",
"example": "desi-clans-test/2026-10-03", "example": "desi-clans-test",
},
{
"name": "report_id",
"type": "string",
"required": True,
"description": "Report postfix; combined with report_group for the button URL suffix",
"example": "2026-10-03",
}, },
], ],
"button_label": "View Report", "button_label": "View Report",

View file

@ -57,12 +57,6 @@ def _explorer_doc_for_template(template: AgentMessageTemplate) -> dict:
"description": "Report postfix; auto-generated when omitted", "description": "Report postfix; auto-generated when omitted",
"example": "2026-10-03", "example": "2026-10-03",
}, },
"report_url": {
"type": "string",
"required": False,
"description": "Full hosted report URL for the WhatsApp button; auto-built when omitted",
"example": "https://urmiai.com/reports/desi-clans-test/2026-10-03",
},
}, },
"responses": { "responses": {
"200": { "200": {

View file

@ -66,7 +66,7 @@ https://urmiai.com/reports/{report_group}/{report_id}
**Console → Agent → UrmiAI** **Console → Agent → UrmiAI**
Subscribe to **`agent_run_completion`** (display name: *Agent Run Success*). Subscribe to **`agent_run_completion`** (Meta template: `agent_run_completion_v2`, display name: *Agent Run Success*).
Messages are sent from the Urmi AI WhatsApp Business number on your behalf. Messages are sent from the Urmi AI WhatsApp Business number on your behalf.
@ -220,30 +220,25 @@ Authorization: Bearer urmi_live_<your_api_key>
| `customer_name` | Yes | string | Name inserted into the template body greeting | | `customer_name` | Yes | string | Name inserted into the template body greeting |
| `report_group` | Yes | string | Urmiport report group slug (same as in publish API) | | `report_group` | Yes | string | Urmiport report group slug (same as in publish API) |
| `report_id` | No | string | Report postfix. If omitted, Urmi auto-generates one (e.g. `run-a1b2c3`) | | `report_id` | No | string | Report postfix. If omitted, Urmi auto-generates one (e.g. `run-a1b2c3`) |
| `report_url` | No | string | Full hosted report URL override. Auto-built as `https://urmiai.com/reports/{report_group}/{report_id}` when omitted |
### Template details — `agent_run_completion` ### Template details — `agent_run_completion` → Meta `agent_run_completion_v2`
| Property | Value | | Property | Value |
|----------|-------| |----------|-------|
| **Slug** | `agent_run_completion` | | **API slug** | `agent_run_completion` |
| **Meta template name** | `agent_run_completion` | | **Meta template name** | `agent_run_completion_v2` |
| **Header** | Agent Run Complete | | **Header** | Agent Run Successful |
| **Body** | Hello {{customer_name}}, your agent run is complete… | | **Body** | Hello {{customer_name}}, your agent run is complete… |
| **Button** | View Report → `https://urmiai.com/reports/{{1}}` | | **Button** | View Report → `https://urmiai.com/reports/{{1}}` |
| Component | Meta format | API field | Example | | Component | Meta format | API field | Example |
|-----------|-------------|-----------|---------| |-----------|-------------|-----------|---------|
| Body | **Named** `customer_name` | `customer_name` | `Team Desi Clans` | | Body | **Named** `customer_name` | `customer_name` | `Team Desi Clans` |
| Button URL | Positional `{{1}}` suffix | `report_group`/`report_id` | `desi-clans-test/2026-10-03` | | Button URL | Positional `{{1}}` suffix | `report_group` + `report_id` | `desi-clans-test/2026-10-03` |
Urmi sends the button suffix so the final link should be: Urmi builds the button suffix as `{report_group}/{report_id}` from the API request — **not** from any posted URL. The response includes `report_url` for convenience only.
`https://urmiai.com/reports/desi-clans-test/2026-10-03` Final link: `https://urmiai.com/reports/desi-clans-test/2026-10-03`
> **400 Invalid parameter:** Body must use named `parameter_name: "customer_name"` — positional body params fail on this template.
>
> **Broken button URL:** If the approved Meta template button URL contains a literal `{{1}}` prefix (e.g. `https://urmiai.com/reports/{{1}}{{1}}`), fix it in WhatsApp Manager → Message templates → edit button URL to `https://urmiai.com/reports/{{1}}` only.
### Example ### Example
@ -256,8 +251,7 @@ curl -X POST 'https://urmiai.com/api/v1/agent/messages/send' \
"to": "919149154337", "to": "919149154337",
"customer_name": "Suyash", "customer_name": "Suyash",
"report_group": "desi-clans-test", "report_group": "desi-clans-test",
"report_id": "2026-10-03", "report_id": "2026-10-03"
"report_url": "https://urmiai.com/reports/desi-clans-test/2026-10-03"
}' }'
``` ```

View file

@ -529,7 +529,6 @@ export const agentMessagesApi = {
customer_name: string customer_name: string
report_group: string report_group: string
report_id?: string report_id?: string
report_url?: string
}, },
) => ) =>
fetch(`${API_BASE_URL}/api/v1/agent/messages/send`, { fetch(`${API_BASE_URL}/api/v1/agent/messages/send`, {