Reputation: 507
I am lost. I have a currently existing Intent in my project, and I am trying to update ALL fields programmatically because that is what my project requires.
I read this documentation and checked this source file on GitHub, and I think the reason I am getting an error is because I don't understand this part of the source:
Args: intent (Union[dict, ~google.cloud.dialogflow_v2beta1.types.Intent]): Required. The intent to update. Format:
projects/<Project ID>/agent/intents/<Intent ID>
. If a dict is provided, it must be of the same form as the protobuf message :class:~google.cloud.dialogflow_v2beta1.types.Intent
(Line 484 for reference)
The platform works great I just don't know what I am missing here..
My code
from constants import *
from google.oauth2 import service_account
import dialogflow_v2beta1
cred = service_account.Credentials.from_service_account_file(AUTH_JSON)
client = dialogflow_v2beta1.IntentsClient(credentials=cred)
params = dialogflow_v2beta1.types.Intent.Parameter(name='test', display_name='test', value='test', is_list=True)
t = dialogflow_v2beta1.types.Intent.Message.Text(text='TEST TEXT')
m = dialogflow_v2beta1.types.Intent.Message(text=t)
p = dialogflow_v2beta1.types.Intent.TrainingPhrase.Part(text='test',entity_type='@test_type', alias='test_alias', user_defined=True)
t = dialogflow_v2beta1.types.Intent.TrainingPhrase(name='test',type=2, parts=[p])
modified_intent = dialogflow_v2beta1.types.Intent(
display_name='test',
messages=[m],
webhook_state=1,
is_fallback=False,
ml_disabled=False,
input_context_names=PROJECT_DIR+'agent/sessions/-/contexts/' + 'TEST_CONTEXT',
events='TESTING EVENT',
training_phrases=[t],
action='TESTING ACTION',
reset_contexts=False,
parameters=[params]
)
name = client.intent_path(PROJECT_NAME, '7b8f2105-53d4-4724-8d4c-0170b8db7028')
intent = client.get_intent(name)
client.update_intent(intent=modified_intent, language_code=LANGUAGE_CODE, intent_view=0)
Full error message
Traceback (most recent call last):
File "/anaconda/envs/data/lib/python3.6/site-packages/google/api_core/grpc_helpers.py", line 57, in error_remapped_callable
return callable_(*args, **kwargs)
File "/anaconda/envs/data/lib/python3.6/site-packages/grpc/_channel.py", line 550, in __call__
return _end_unary_response_blocking(state, call, False, None)
File "/anaconda/envs/data/lib/python3.6/site-packages/grpc/_channel.py", line 467, in _end_unary_response_blocking
raise _Rendezvous(state, None, None, deadline)
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with:
status = StatusCode.INVALID_ARGUMENT
details = "Resource name '' does not match 'projects/*/agent/intents/*'."
debug_error_string = "{"created":"@1552461629.958860000","description":"Error received from peer","file":"src/core/lib/surface/call.cc","file_line":1036,"grpc_message":"Resource name '' does not match 'projects/*/agent/intents/*'.","grpc_status":3}"
>
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "test.py", line 26, in <module>
client.update_intent(intent=modified_intent, language_code=LANGUAGE_CODE, intent_view=0)
File "/anaconda/envs/data/lib/python3.6/site-packages/dialogflow_v2beta1/gapic/intents_client.py", line 535, in update_intent
request, retry=retry, timeout=timeout, metadata=metadata)
File "/anaconda/envs/data/lib/python3.6/site-packages/google/api_core/gapic_v1/method.py", line 143, in __call__
return wrapped_func(*args, **kwargs)
File "/anaconda/envs/data/lib/python3.6/site-packages/google/api_core/retry.py", line 270, in retry_wrapped_func
on_error=on_error,
File "/anaconda/envs/data/lib/python3.6/site-packages/google/api_core/retry.py", line 179, in retry_target
return target()
File "/anaconda/envs/data/lib/python3.6/site-packages/google/api_core/timeout.py", line 214, in func_with_timeout
return func(*args, **kwargs)
File "/anaconda/envs/data/lib/python3.6/site-packages/google/api_core/grpc_helpers.py", line 59, in error_remapped_callable
six.raise_from(exceptions.from_grpc_error(exc), exc)
File "<string>", line 3, in raise_from
google.api_core.exceptions.InvalidArgument: 400 Resource name '' does not match 'projects/*/agent/intents/*'.
Upvotes: 1
Views: 2204
Reputation: 6800
You are able to get the intent which you want to modify correctly by using
name = client.intent_path(PROJECT_NAME, your_intent_id)
You will get complete definition of your intent.
Then you need to change the values of this intent by accessing them and assigning your values.
After that, you need to pass the same intent in your update_intent()
function.
It is also advised to use update_mask
to avoid changing any other field or setting rest of the fields None.
Here is an example of updating intent display_name from greet to hello:
client = dialogflow.IntentsClient()
intent_name = client.intent_path(project_id, intent_id)
intent = client.get_intent(intent_name, intent_view=dialogflow.enums.IntentView.INTENT_VIEW_FULL)
intent.display_name = 'hello'
update_mask = field_mask_pb2.FieldMask(paths=['display_name'])
print(response)
You will need ope extra import as well in your code:
from google.protobuf import field_mask_pb2
This way, intent's display_name will be changed.
You can do same for the rest of your properties as well. Just remember to pass the value which the property is expecting by following this documentation and you can take help from this issue as well.
Hope it helps.
Upvotes: 1