diff --git a/src/dfcx_scrapi/core/playbooks.py b/src/dfcx_scrapi/core/playbooks.py index d9ee4622..1a7a0a9f 100644 --- a/src/dfcx_scrapi/core/playbooks.py +++ b/src/dfcx_scrapi/core/playbooks.py @@ -62,15 +62,40 @@ def __init__( @staticmethod def build_instructions( instructions: List[str]) -> List[types.Playbook.Step]: + """Helper method to create the playbook instruction set protos.""" + final_instructions = types.Playbook.Instruction(steps=[]) + if not isinstance(instructions, list): raise TypeError( "Instructions must be provided as a List of strings.") - all_steps = [] - for instruction in instructions: - all_steps.append(types.Playbook.Step(text=instruction)) + else: + for instruction in instructions: + final_instructions.steps.append( + types.Playbook.Step(text=instruction) + ) + + return final_instructions + + def process_playbook_kwargs( + self, + playbook: types.Playbook, + **kwargs): + """Process incoming kwargs and create the proper update mask.""" + paths = [] + for key, value in kwargs.items(): + if key in ["instruction", "instructions"]: + instructions = self.build_instructions(value) + setattr(playbook, "instruction", instructions) + paths.append("instruction") + else: + setattr(playbook, key, value) + paths.append(key) + + # paths = kwargs.keys() + mask = field_mask_pb2.FieldMask(paths=paths) - return all_steps + return playbook, mask def set_default_playbook(self, playbook_id: str): """Sets the default Playbook for the Agent.""" @@ -164,7 +189,6 @@ def create_playbook( self, agent_id: str, obj: types.Playbook = None, - instructions: List[str] = None, **kwargs, ): """Create a Dialogflow CX Playbook with given display name. @@ -184,34 +208,34 @@ def create_playbook( request.parent = agent_id if obj: - playbook_obj = obj - playbook_obj.name = "" + playbook = obj + playbook.name = "" else: - playbook_obj = types.Playbook() + playbook = types.Playbook() # set optional args as playbook attributes - for key, value in kwargs.items(): - if key == "instructions": - instructions = self.build_instructions(instructions) - setattr(playbook_obj, "instructions", instructions) - else: - setattr(playbook_obj, key, value) - - request.playbook = playbook_obj + playbook, _ = self.process_playbook_kwargs(playbook, **kwargs) + request.playbook = playbook response = self.playbooks_client.create_playbook(request) return response @scrapi_base.api_call_counter_decorator def update_playbook( - self, playbook_id: str, obj: types.Playbook = None, **kwargs + self, + playbook_id: str, + obj: types.Playbook = None, + **kwargs ) -> types.Playbook: """Update a single specific CX Playbook object. Args: playbook_id: CX Playbook ID in the proper format obj: (Optional) a single CX Playbook object of types.Playbook + overwrite_instructions: if True this will overwrite all instructions + for the specific playbook. By default this is set to False and will + append new instructions. Returns: A copy of the updated Playbook object @@ -223,11 +247,8 @@ def update_playbook( else: playbook = self.get_playbook(playbook_id) - # set playbook attributes to args - for key, value in kwargs.items(): - setattr(playbook, key, value) - paths = kwargs.keys() - mask = field_mask_pb2.FieldMask(paths=paths) + # set optional args as playbook attributes + playbook, mask = self.process_playbook_kwargs(playbook, **kwargs) response = self.playbooks_client.update_playbook( playbook=playbook, update_mask=mask) diff --git a/tests/dfcx_scrapi/core/test_tools.py b/tests/dfcx_scrapi/core/test_tools.py index 4415c3a5..4a6ea5c1 100644 --- a/tests/dfcx_scrapi/core/test_tools.py +++ b/tests/dfcx_scrapi/core/test_tools.py @@ -21,8 +21,8 @@ import pytest from unittest.mock import patch from dfcx_scrapi.core.tools import Tools -from google.cloud.dialogflow_v3alpha1 import types -from google.cloud.dialogflow_v3alpha1 import services +from google.cloud.dialogflowcx_v3beta1 import types +from google.cloud.dialogflowcx_v3beta1 import services @pytest.fixture def test_config():