upsert_customer
Create or update the conversation's customer as soon as your code learns who they are.
upsert_customer(context: dict, email: str = None, name: str = None, phone: str = None) -> dictSaves the customer's email, name or phone number on the customer linked to the current conversation. Use it when an action learns who the customer is. For example, when an anonymous web chat visitor gives their email address, saving it means later lookups, the help desk sidebar and context["customer"] all see it.
Parameters
| Name | Type | Description |
|---|---|---|
context | dict | The execution context. It must contain the conversation. |
email | str or None | The customer's email address. Must be a valid address |
name | str or None | The customer's full name, 1 to 200 characters |
phone | str or None | The customer's phone number, 1 to 50 characters |
Pass at least one field. Fields left as None are not changed.
Returns
{
"success": True,
"customer": {"id": str, "email": str | None, "name": str | None, "phone": str | None},
}Behavior
The helper looks for the customer already linked to the conversation and for customers of the same business with the given email or phone number.
- No match: a new customer is created with the given fields and linked to the conversation.
- One match: the given fields are saved on that customer, and the conversation is linked to it.
- Several matches: for example, the conversation is linked to a customer without an email, and the email you pass belongs to another customer. The matches are merged into the oldest one: all their conversations move to it, and the given fields are saved on it.
- Emails are stored in lowercase.
- In test conversations, the helper links an existing matching customer but changes no customer profile.
- Invalid values (for example a malformed email) raise
requests.HTTPError. - Without
context["conversation"](for example in an editor test without a conversation), nothing is saved and the helper returns the values you passed with"id": None.
Normalize phone numbers first with normalize_phone_number so the same customer is always stored the same way.
Examples
Save the email a web chat visitor gives
def execute_action(context):
email = context["args"]["email"].strip()
result = upsert_customer(context, email=email)
return {"saved": result["success"], "customerId": result["customer"]["id"]}Store a normalized phone number with the name
def execute_action(context):
phone = normalize_phone_number(context["args"]["phone"], "GB")
if phone is None:
return {"success": False, "message": "Ask the customer to check the phone number."}
upsert_customer(context, name=context["args"].get("name"), phone=phone)
return {"success": True}The helper trusts the values you pass. Linking a conversation to the customer
who owns an email address or phone number does not verify that the person in
the chat owns it, and afterwards context["customer"] holds that customer.
Don't treat a linked customer as proof of identity: keep your own verification
rules before showing order or account details.