get_conversation_metadata
Read a metadata value from the current conversation.
get_conversation_metadata(context: dict, key: str) -> str | NoneRetrieves a metadata value previously stored on the conversation. Returns None if the key doesn't exist.
Parameters
| Name | Type | Description |
|---|---|---|
context | dict | The context object passed to your action |
key | str | The metadata key to retrieve |
Returns
str | None — The stored value, or None if the key is not found.
Behavior
- Returns
Nonein test/mock environments (whencontext["conversation"]is absent). - Metadata keys are set via
set_conversation_metadataor through Chat Custom Data (keys prefixed withchat-widget:).
Examples
Read a user ID from chat custom data
def execute_action(context):
user_id = get_conversation_metadata(context, "chat-widget:user_id")
if not user_id:
return {"error": "No user ID found. The customer may not be logged in."}
response = requests.get(
"https://api.example.com/users",
params={"id": user_id},
headers={"Authorization": "Bearer API_KEY"},
timeout=30,
)
response.raise_for_status()
return response.json()Check if a previous action already ran
def execute_action(context):
already_refunded = get_conversation_metadata(context, "refund_processed")
if already_refunded:
return {"error": "A refund has already been processed in this conversation."}
# ... process refund ...
set_conversation_metadata(context, "refund_processed", "true")
return {"success": True}