Skip to content

Commit 0a42402

Browse files
authored
docs(samples): add agent creation code snippet (#146)
* add agent creation code snippet * update test * Updated test * moved delete agent * lint fix * lint fix * Fixed Copyright
1 parent 9d4da7f commit 0a42402

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

Dialogflow-CX/create_agent.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
16+
"""DialogFlow API Create Agent Sample"""
17+
18+
from google.cloud.dialogflowcx_v3.services.agents.client import AgentsClient
19+
from google.cloud.dialogflowcx_v3.types.agent import Agent
20+
21+
22+
def create_agent(project_id, display_name):
23+
24+
parent = "projects/" + project_id + "/locations/global"
25+
26+
agents_client = AgentsClient()
27+
28+
agent = Agent(
29+
display_name=display_name,
30+
default_language_code="en",
31+
time_zone="America/Los_Angeles",
32+
)
33+
34+
response = agents_client.create_agent(request={"agent": agent, "parent": parent})
35+
36+
return response

Dialogflow-CX/create_agent_test.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2020, 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+
"""Test for create_agent"""
15+
16+
from datetime import date
17+
import os
18+
19+
from google.cloud.dialogflowcx_v3.services.agents.client import AgentsClient
20+
from google.cloud.dialogflowcx_v3.types.agent import DeleteAgentRequest
21+
22+
import pytest
23+
24+
from create_agent import create_agent
25+
26+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
27+
pytest.AGENT_PATH = ""
28+
29+
30+
def delete_agent(name):
31+
agents_client = AgentsClient()
32+
request = DeleteAgentRequest(name=name)
33+
agents_client.delete_agent(request=request)
34+
35+
36+
def test_create_agent():
37+
today = date.today()
38+
agentName = "tempAgent." + today.strftime("%d.%m.%Y")
39+
response = create_agent(PROJECT_ID, agentName)
40+
delete_agent(response.name)
41+
42+
assert response.display_name == agentName

0 commit comments

Comments
 (0)