项目作者: dialogflow

项目描述 :
Ruby SDK for Dialogflow
高级语言: Ruby
项目地址: git://github.com/dialogflow/dialogflow-ruby-client.git
创建时间: 2014-11-24T18:51:25Z
项目社区:https://github.com/dialogflow/dialogflow-ruby-client

开源协议:Apache License 2.0

下载


DEPRECATED The API.AI ruby gem

Deprecated
This Dialogflow client library and Dialogflow API V1 have been deprecated and will be shut down on October 23th, 2019. Please migrate to Dialogflow API V2 and the v2 client library

Gem Version

A Ruby SDK to the https://api.ai natural language processing service.

Installation

  1. gem install api-ai-ruby

Basic Usage

Just pass correct credentials to ApiAiRuby::Client constructor

  1. client = ApiAiRuby::Client.new(
  2. :client_access_token => 'YOUR_CLIENT_ACCESS_TOKEN'
  3. )

After that you can send text requests to the https://api.ai with command

  1. response = client.text_request 'hello!'

Or try to invocate intent via defined ‘event‘:

  1. response_zero = client.event_request 'MY_CUSTOM_EVENT_NAME';
  2. response_one = client.event_request 'MY_EVENT_WITH_DATA_TO_STORE', {:param1 => 'value'}
  3. response_two = client.event_request 'MY_EVENT_WITH_DATA_TO_STORE', {:some_param => 'some_value'}, :resetContexts => true

voice_request and text_request methods returns symbolized https://api.ai response. Structure of response can be found at https://docs.api.ai/docs/query#response.

Advanced usage

During client instantiating you can additionally set parameters like api url, request language and version (more info at https://docs.api.ai/docs/versioning, https://docs.api.ai/docs/languages)

  1. ApiAiRuby::Client.new(
  2. client_access_token: 'YOUR_ACCESS_TOKEN',
  3. api_lang: 'FR',
  4. api_base_url: 'http://example.com/v1/',
  5. api_version: 'YYYYMMDD',
  6. api_session_id: 'some_uuid_or_whatever'
  7. )

And you also can send additional data to server during request, use second parameter of text_request and voice_request methods to do that

  1. response = client.text_request 'Hello', :contexts => ['firstContext'], :resetContexts => true
  2. response = client.voice_request file, :timezone => 'America/New_York'

More information about possible parameters can be found at https://docs.api.ai/docs/query page

User Entities

Another possibility is to send and retrieve custom entities to the server.

You can do it along with query request

  1. client.text_request 'call Mozart', entities: [
  2. {
  3. name: 'contacts',
  4. entries: [
  5. ApiAiRuby::Entry.new('Mozart', %w(Mozart Wolfgang)),
  6. ApiAiRuby::Entry.new('Salieri', %w(Salieri Antonio))
  7. ]
  8. }
  9. ]
  10. # the same without ApiAiRuby::Entry wrapper
  11. client.text_request 'call Mozart', entities: [
  12. {
  13. name: 'contacts',
  14. entries: [
  15. {value: 'Mozart', synonyms: %w(Mozart Wolfgang)},
  16. {value: 'Salieri', synonyms: %w(Salieri Antonio)}
  17. ]
  18. }
  19. ]

Or with separate create_user_entities_request object with full CRUD support:

  1. # preparations
  2. entries_composers = [
  3. ApiAiRuby::Entry.new('Mozart', %w(Mozart Wolfgang)),
  4. ApiAiRuby::Entry.new('Salieri', %w(Salieri Antonio))
  5. ]
  6. entries_unknown = [
  7. ApiAiRuby::Entry.new('John Doe', %w(John Unknown)),
  8. ApiAiRuby::Entry.new('Jane Doe', %w(Jane))
  9. ]
  10. entity_contacts = ApiAiRuby::Entity.new('contacts', entries_composers)
  11. # let's go
  12. uer = client.create_user_entities_request
  13. uer.create(entity_contacts) # or uer.create([entity1, entity2...])
  14. client.text_request 'call Mozart' # will work
  15. uer.update('contacts', entries_unknown)
  16. client.text_request 'call Mozart' # will NOT work
  17. client.text_request 'call John' # will work
  18. uer.retrieve('contacts') # will return current state of user entity
  19. uer.delete('contacts') # will remove user entities for given session

Context

Also SDK has full support of contexts API.AI endpoint with special object, called contexts_request
Usage is simple:

  1. # some preparations
  2. lifespan = 5
  3. parameters = {
  4. :param_name => 'param_value'
  5. }
  6. name = 'test_context'
  7. # you can create context using built-in model ApiAiRuby::Context
  8. test_context = ApiAiRuby::Context.new(name, lifespan, parameters)
  9. another_test_context = ApiAiRuby::Context.new('another_test_context')
  10. one_more_test_context = ApiAiRuby::Context.new('one_more_test_context', 4)
  11. # ok, we are ready
  12. context_request = @client.create_contexts_request
  13. # there are different options to be used with .create
  14. context_request.create(test_context)
  15. context_request.create([another_test_context, one_more_test_context])
  16. context_request.create('one_more_super_final_mega_context')
  17. context_request.retrieve('test_context') # will return you single context or nothing
  18. context_request.list() # will return you list of all contexts used in current session
  19. context_request.delete('test_context') # will remove single context
  20. context_request.delete() # will remove all context in session

Timeouts

ApiAiRuby::Client uses the http gem under the hood. You can use timeout_options on the client to set these.

  1. ApiAiRuby::Client.new(
  2. client_access_token: 'YOUR_ACCESS_TOKEN',
  3. api_lang: 'FR',
  4. api_base_url: 'http://example.com/v1/',
  5. api_version: 'YYYYMMDD',
  6. api_session_id: 'some_uuid_or_whatever',
  7. timeout_options: [:global, { write: 1, connect: 1, read: 1 }]
  8. )

Please see the httprb wiki on timeouts for more information.

Error handling

ApiAiRuby::Client currently able to raise two kind of errors: ApiAiRuby::ClientError (due to configuration mismatch) and ApiAiRuby::RequestError in case of something goes wrong during request. For both kind of errors you can get error.message (as usual) and ApiAiRuby::RequestError can additionally give you code of server error (you can get it with error.code)

Changelog

2.0.0

Breaking:

  • http gem dependency updated to 2.0, it does no longer raise Errno::ETIMEDOUT. Thanks to @tak1n

1.3.0

Non-breaking:

Breaking:

  • ApiAiRuby::Client::user_entities_request renamed to ApiAiRuby::Client::create_user_entities_request
  • ApiAiRuby::Entity::addEntry renamed to ApiAiRuby::Entity::add_entry

Previous

  • 1.2.3 - events support
  • 1.2.2 - added configurable timeouts for requests (thanks bramski)
  • 1.2.1 - fixed UTF-8 in text-requests
  • 1.2.0 - added configurable session_id and full userEntities support
  • 1.1.4 - removed unused dependency and updated default API version
  • 1.1.3 - fixed non-correctly serialized parameters in new contexts during query send process
  • 1.1.2 - fixed compatibility with ruby version less then 2.1.6

How to make contributions?

Please read and follow the steps in the CONTRIBUTING.md.

License

See LICENSE.

Terms

Your use of this sample is subject to, and by using or downloading the sample files you agree to comply with, the Google APIs Terms of Service.

This is not an official Google product.