diff --git a/src/gradient_labs/_conversation_finish.py b/src/gradient_labs/_conversation_finish.py index 19a1329..89e603e 100644 --- a/src/gradient_labs/_conversation_finish.py +++ b/src/gradient_labs/_conversation_finish.py @@ -17,6 +17,10 @@ class FinishParams: # reason optionally allows you to describe why this conversation is finishing. reason: Optional[str] = None + # reason_code optionally categorises why this conversation is finishing. + # Valid values are "customer-ended-chat" and "customer-unresponsive". + reason_code: Optional[str] = None + def finish_conversation( *, client: HttpClient, conversation_id: str, params: FinishParams @@ -26,6 +30,8 @@ def finish_conversation( body["timestamp"] = client.localize(params.timestamp) if params.reason: body["reason"] = params.reason + if params.reason_code: + body["reason_code"] = params.reason_code _ = client.put( f"conversations/{conversation_id}/finish", diff --git a/tests/test_conversation_finish.py b/tests/test_conversation_finish.py new file mode 100644 index 0000000..c0157da --- /dev/null +++ b/tests/test_conversation_finish.py @@ -0,0 +1,56 @@ +from unittest.mock import MagicMock + +from gradient_labs import Client, FinishParams + +CONVERSATION_ID = "conv_01ham6bzcdeja9xzqhjf6daq30" + + +def _client(): + client = Client(api_key="test-key") + put = MagicMock(return_value=None) + client.http_client.put = put + return client, put + + +def test_finish_conversation(): + client, put = _client() + + client.finish_conversation( + conversation_id=CONVERSATION_ID, + params=FinishParams(), + ) + + args, kwargs = put.call_args + assert args[0] == f"conversations/{CONVERSATION_ID}/finish" + assert kwargs["body"] == {} + + +def test_finish_conversation_includes_reason_code(): + client, put = _client() + + client.finish_conversation( + conversation_id=CONVERSATION_ID, + params=FinishParams( + reason="Customer stopped replying", + reason_code="customer-unresponsive", + ), + ) + + _, kwargs = put.call_args + body = kwargs["body"] + assert body["reason"] == "Customer stopped replying" + assert body["reason_code"] == "customer-unresponsive" + + +def test_finish_conversation_omits_reason_code_when_unset(): + client, put = _client() + + client.finish_conversation( + conversation_id=CONVERSATION_ID, + params=FinishParams(reason="Resolved"), + ) + + _, kwargs = put.call_args + body = kwargs["body"] + assert body["reason"] == "Resolved" + assert "reason_code" not in body