Skip to content

Commit e6238f1

Browse files
galz10parthea
andauthored
docs(samples): add update intent code sample (#357)
* Added Update Intent Snippet and Test * Deleted Setting Env Vars * Fixed Lint Issues * Fixed Lint and Build Issue * Fixed Build Issue * Changed tests to pytests * Removed delete and create agent from test * Fixed Import Order * Deleted unused import * Removed Language from update_intent Snippet * Added copyright * Changed intent name to random name * delete intent after testing * fix test * remove contains * Added Create Intent * fix lint Co-authored-by: Anthonios Partheniou <[email protected]>
1 parent 599b972 commit e6238f1

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

dialogflow/update_intent.py

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

dialogflow/update_intent_test.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import uuid
17+
18+
from google.cloud.dialogflow_v2.services.agents.client import AgentsClient
19+
from google.cloud.dialogflow_v2.services.intents.client import IntentsClient
20+
from google.cloud.dialogflow_v2.types.intent import 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+
29+
30+
def create_intent(project_id):
31+
intents_client = IntentsClient()
32+
33+
parent = AgentsClient.agent_path(project_id)
34+
35+
intent = Intent()
36+
37+
intent.display_name = "fake_intent"
38+
39+
intents = intents_client.create_intent(request={"parent": parent, "intent": intent})
40+
41+
return intents.name.split("/")[4]
42+
43+
44+
@pytest.fixture(scope="function", autouse=True)
45+
def setup_teardown():
46+
pytest.INTENT_ID = create_intent(project_id=PROJECT_ID)
47+
print("Created Intent in setUp")
48+
49+
50+
def test_update_intent():
51+
52+
fake_intent = "fake_intent_{}".format(uuid.uuid4())
53+
54+
actualResponse = update_intent(PROJECT_ID, pytest.INTENT_ID, fake_intent)
55+
expectedResponse = fake_intent
56+
57+
intents_client = IntentsClient()
58+
59+
intents_client.delete_intent(name=actualResponse.name)
60+
61+
assert actualResponse.display_name == expectedResponse

0 commit comments

Comments
 (0)