-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Expand file tree
/
Copy pathcontext_management.py
More file actions
133 lines (106 loc) · 4.27 KB
/
context_management.py
File metadata and controls
133 lines (106 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python
# Copyright 2017 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""DialogFlow API Context Python sample showing how to manage session
contexts.
Examples:
python context_management.py -h
python context_management.py --project-id PROJECT_ID \
list --session-id SESSION_ID
python context_management.py --project-id PROJECT_ID \
create --session-id SESSION_ID --context-id CONTEXT_ID
python context_management.py --project-id PROJECT_ID \
delete --session-id SESSION_ID --context-id CONTEXT_ID
"""
import argparse
# [START dialogflow_list_contexts]
def list_contexts(project_id, session_id):
import dialogflow_v2 as dialogflow
contexts_client = dialogflow.ContextsClient()
session_path = contexts_client.session_path(project_id, session_id)
contexts = contexts_client.list_contexts(session_path)
print('Contexts for session {}:\n'.format(session_path))
for context in contexts:
print('Context name: {}'.format(context.name))
print('Lifespan count: {}'.format(context.lifespan_count))
print('Fields:')
for field, value in context.parameters.fields.items():
if value.string_value:
print('\t{}: {}'.format(field, value))
# [END dialogflow_list_contexts]
# [START dialogflow_create_context]
def create_context(project_id, session_id, context_id, lifespan_count):
import dialogflow_v2 as dialogflow
contexts_client = dialogflow.ContextsClient()
session_path = contexts_client.session_path(project_id, session_id)
context_name = contexts_client.context_path(
project_id, session_id, context_id)
context = dialogflow.types.Context(
name=context_name, lifespan_count=lifespan_count)
response = contexts_client.create_context(session_path, context)
print('Context created: \n{}'.format(response))
# [END dialogflow_create_context]
# [START dialogflow_delete_context]
def delete_context(project_id, session_id, context_id):
import dialogflow_v2 as dialogflow
contexts_client = dialogflow.ContextsClient()
context_name = contexts_client.context_path(
project_id, session_id, context_id)
contexts_client.delete_context(context_name)
# [END dialogflow_delete_context]
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument(
'--project-id',
help='Project/agent id. Required.',
required=True)
subparsers = parser.add_subparsers(dest='command')
list_parser = subparsers.add_parser(
'list', help=list_contexts.__doc__)
list_parser.add_argument(
'--session-id',
required=True)
create_parser = subparsers.add_parser(
'create', help=create_context.__doc__)
create_parser.add_argument(
'--session-id',
required=True)
create_parser.add_argument(
'--context-id',
help='The id of the context.',
required=True)
create_parser.add_argument(
'--lifespan-count',
help='The lifespan_count of the context. Defaults to 1.',
default=1)
delete_parser = subparsers.add_parser(
'delete', help=delete_context.__doc__)
delete_parser.add_argument(
'--session-id',
required=True)
delete_parser.add_argument(
'--context-id',
help='The id of the context.',
required=True)
args = parser.parse_args()
if args.command == 'list':
list_contexts(args.project_id, args.session_id, )
elif args.command == 'create':
create_context(
args.project_id, args.session_id, args.context_id,
args.lifespan_count)
elif args.command == 'delete':
delete_context(args.project_id, args.session_id, args.context_id)