Skip to content

Commit e49d192

Browse files
galz10leahecole
andauthored
docs(samples): add update intent sample (#142)
* add update intent sample * fixed lint * added agent creation * fixed lint * added randomized agent name * Failing test fix * Fixed failing test * Change intent request * removed self from test * lint fix * Update samples/snippets/update_intent_test.py Co-authored-by: Leah E. Cole <[email protected]> Co-authored-by: Leah E. Cole <[email protected]>
1 parent f9043ab commit e49d192

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

Dialogflow-CX/update_intent.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2021, Google LLC
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
from google.cloud.dialogflowcx_v3.services.intents import IntentsClient
15+
from google.protobuf import field_mask_pb2
16+
17+
18+
def update_intent(project_id, agent_id, intent_id, location, displayName):
19+
20+
intents_client = IntentsClient()
21+
22+
intent_name = intents_client.intent_path(project_id, location, agent_id, intent_id)
23+
24+
intent = intents_client.get_intent(request={"name": intent_name})
25+
26+
intent.display_name = displayName
27+
update_mask = field_mask_pb2.FieldMask(paths=["display_name"])
28+
response = intents_client.update_intent(intent=intent, update_mask=update_mask)
29+
return response

Dialogflow-CX/update_intent_test.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Copyright 2021, Google LLC
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
import os
15+
import uuid
16+
17+
from google.cloud.dialogflowcx_v3.services.agents.client import AgentsClient
18+
from google.cloud.dialogflowcx_v3.services.intents.client import IntentsClient
19+
from google.cloud.dialogflowcx_v3.types.agent import Agent, DeleteAgentRequest
20+
from google.cloud.dialogflowcx_v3.types.intent import CreateIntentRequest, Intent
21+
22+
import pytest
23+
24+
from update_intent import update_intent
25+
26+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
27+
pytest.INTENT_ID = None
28+
pytest.AGENT_ID = None
29+
pytest.PARENT = None
30+
31+
32+
def create_agent(project_id, display_name):
33+
parent = "projects/" + project_id + "/locations/global"
34+
35+
agents_client = AgentsClient()
36+
37+
agent = Agent(
38+
display_name=display_name,
39+
default_language_code="en",
40+
time_zone="America/Los_Angeles",
41+
)
42+
43+
response = agents_client.create_agent(request={"agent": agent, "parent": parent})
44+
45+
return response
46+
47+
48+
def delete_agent(name):
49+
agents_client = AgentsClient()
50+
agent = DeleteAgentRequest(name=name)
51+
agents_client.delete_agent(request=agent)
52+
53+
54+
@pytest.fixture(scope="function", autouse=True)
55+
def setup_teardown():
56+
agentName = "temp_agent_" + str(uuid.uuid4())
57+
pytest.PARENT = create_agent(PROJECT_ID, agentName).name
58+
pytest.AGENT_ID = pytest.PARENT.split("/")[5]
59+
print("Created Agent in setUp")
60+
intentClient = IntentsClient()
61+
intent = Intent()
62+
63+
intent.display_name = "fake_intent"
64+
65+
req = CreateIntentRequest()
66+
req.parent = pytest.PARENT
67+
req.intent = intent
68+
69+
pytest.INTENT_ID = intentClient.create_intent(request=req).name.split("/")[7]
70+
71+
yield
72+
73+
delete_agent(pytest.PARENT)
74+
75+
76+
def test_fieldmaskTest():
77+
fake_intent = f"fake_intent_{uuid.uuid4()}"
78+
actualResponse = update_intent(
79+
PROJECT_ID, pytest.AGENT_ID, pytest.INTENT_ID, "global", fake_intent
80+
)
81+
82+
assert actualResponse.display_name == fake_intent

0 commit comments

Comments
 (0)