Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Intent API issue: TypeError: wrong argument type Symbol #2839

Closed
joamaral opened this issue Jan 23, 2019 · 3 comments
Closed

Create Intent API issue: TypeError: wrong argument type Symbol #2839

joamaral opened this issue Jan 23, 2019 · 3 comments
Assignees
Labels
api: dialogflow Issues related to the Dialogflow API. type: question Request for information or clarification. Not an issue.

Comments

@joamaral
Copy link

joamaral commented Jan 23, 2019

Hello all, I'm getting an error while trying to create an Intent through the API with a custom payload:

TypeError: wrong argument type Symbol
from lib/google/cloud/dialogflow/v2/intents_client.rb:465

The code I'm using is the following:

dialogflow_api_request.create_intent("projects/#{params[:agent_id]}/agent", {
display_name: params[:display_name],
        training_phrases: training_phrases,
        messages: [
          {
            payload: {
              fields: {
                test: { string_value: 'foo' }
              }
            }
          }
        ]
      })

I don't have a clue why am I getting the error, since I'm able to create an intent providing text as a response instead of a custom payload:

text: {
    text: ["foo"]
  }

Can you please tell me if this is a gem issue, or if I'm doing something wrong?

Thanks in advance!
João

@blowmage
Copy link
Contributor

Using hashes to represent the values will only work so far. The issue you are running into is creating a Google::Protobuf::Struct object. The Google::Protobuf::Struct#fields attribute is not a Ruby Hash, but a Google::Protobuf::Map. Map acts like a statically typed Hash, and this one requires a String key and a Google::Protobuf::Value value. So the first error you are getting is because you are passing a Symbol instead of a String. ( The test in { test: { string_value: 'foo' } } is a Symbol.)

The second error you are likely to run into is that the value's conversion from a Ruby Hash to Google::Protobuf::Value.

dialogflow_api_request.create_intent(
  "projects/#{params[:agent_id]}/agent", 
  {
    display_name: params[:display_name],
    training_phrases: training_phrases,
    messages: [
      {
        payload: {
          fields: {
            'test' => { string_value: 'foo' }
          }
        }
      }
    ]
  }
)
# raises TypeError: Invalid type Hash to assign to submessage field.

To resolve this error you will need to create a Google::Protobuf::Value object and pass that in the fields hash:

dialogflow_api_request.create_intent(
  "projects/#{params[:agent_id]}/agent", 
  {
    display_name: params[:display_name],
    training_phrases: training_phrases,
    messages: [
      {
        payload: {
          fields: {
            'test' => Google::Protobuf::Value.new(string_value: 'foo')
          }
        }
      }
    ]
  }
)

@blowmage blowmage self-assigned this Jan 23, 2019
@blowmage blowmage added type: question Request for information or clarification. Not an issue. api: dialogflow Issues related to the Dialogflow API. labels Jan 23, 2019
@joamaral
Copy link
Author

@blowmage thank you so much for your help. Understood! One last question: How could I append another hash inside the fields hash?

Meaning:

payload: {
          fields: {
            'test' => Google::Protobuf::Value.new(string_value: 'foo'),
            'anotherHash' => {
                'test' => Google::Protobuf::Value.new(string_value: 'foo'),
                'another' => Google::Protobuf::Value.new(string_value: 'bar'),
          }
        }

I've been trying to do achieve this, and I believe I should use Google::Protobuf::Struct, am I right?

@blowmage
Copy link
Contributor

The nested hash still needs to be a Google::Protobuf::Value object. But, it can be a struct_value type of value object, with a Google::Protobuf::Struct value:

payload: Google::Protobuf::Struct.new(
  fields: {
    'test' => Google::Protobuf::Value.new(string_value: 'foo'),
    'anotherHash' => Google::Protobuf::Value.new(
      struct_value: Google::Protobuf::Struct.new(
        fields: {
          'test' => Google::Protobuf::Value.new(string_value: 'foo'),
          'another' => Google::Protobuf::Value.new(string_value: 'bar'),
        }
      )
    )
  }
)
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: dialogflow Issues related to the Dialogflow API. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

2 participants