diff --git a/README.md b/README.md index dfa2abf..4862f79 100644 --- a/README.md +++ b/README.md @@ -22,35 +22,14 @@ After that you can send text requests to the https://api.ai with command response = client.text_request 'hello!' ``` -And voice requests with file stream +Or try to invocate intent via defined '[event](https://docs.api.ai/docs/concept-events)': ```ruby -file = File.new 'hello.wav' -response = client.voice_request(file) -``` -Example answer: -``` -{ - :id => "6daf5ab7-276c-43ad-a32d-bf6831918492", - :timestamp => "2015-12-22T08:42:15.785Z", - :result => { - :source => "agent", - :resolvedQuery => "Hello", - :speech => "Hi! How are you?", - :action => "greeting", - :parameters => {}, - :contexts => [], - :metadata => { - :intentId => "a5d685ab-1f19-46b0-9478-69f794553668", - :intentName => "hello" - } - }, - :status => { - :code => 200, - :errorType => "success" - } -} +response_zero = client.event_request 'MY_CUSTOM_EVENT_NAME'; +response_one = client.event_request 'MY_EVENT_WITH_DATA_TO_STORE', {:param1 => 'value'} +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. @@ -73,7 +52,7 @@ And you also can send additional data to server during request, use second param ```ruby response = client.text_request 'Hello', :contexts => ['firstContext'], :resetContexts => true - response = client.voice_request file, :timezone => "America/New_York" + 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 @@ -162,6 +141,7 @@ Please see the [httprb wiki on timeouts](https://github.com/httprb/http/wiki/Tim #Changelog +* 1.2.3 - events support * 1.2.2 - added configurable timeouts for requests (thanks [bramski](https://github.com/bramski)) * 1.2.1 - fixed UTF-8 in text-requests * 1.2.0 - added configurable session_id and full userEntities support diff --git a/lib/api-ai-ruby.rb b/lib/api-ai-ruby.rb index 687b82c..0f3cfa5 100644 --- a/lib/api-ai-ruby.rb +++ b/lib/api-ai-ruby.rb @@ -4,8 +4,8 @@ require 'api-ai-ruby/client_error' require 'api-ai-ruby/request/request_query' require 'api-ai-ruby/request/text_request' +require 'api-ai-ruby/request/event_request' require 'api-ai-ruby/request/voice_request' require 'api-ai-ruby/models/entry' require 'api-ai-ruby/models/entity' require 'api-ai-ruby/crud/user_entity_request' - diff --git a/lib/api-ai-ruby/client.rb b/lib/api-ai-ruby/client.rb index c37c06e..4c9429c 100644 --- a/lib/api-ai-ruby/client.rb +++ b/lib/api-ai-ruby/client.rb @@ -59,6 +59,18 @@ def text_request (query = '', options = {}) ApiAiRuby::TextRequest.new(self, options).perform end + # @param event_name [String] + # @param data [Object] + # @param options [Object] + def event_request (event_name = '', data = {}, options = {}) + raise ApiAiRuby::ClientError.new('Credentials missing') if !credentials? + options[:event] = { + name: event_name, + data: data + } + ApiAiRuby::EventRequest.new(self, options).perform + end + def voice_request(file_stream, options = {}) raise ApiAiRuby::ClientError.new('Credentials missing') if !credentials? options[:file] = file_stream diff --git a/lib/api-ai-ruby/constants.rb b/lib/api-ai-ruby/constants.rb index 6a1047a..59ca4ba 100644 --- a/lib/api-ai-ruby/constants.rb +++ b/lib/api-ai-ruby/constants.rb @@ -1,6 +1,6 @@ module ApiAiRuby class Constants - VERSION = '1.2.2' + VERSION = '1.2.3' DEFAULT_BASE_URL = 'https://api.api.ai/v1/' DEFAULT_API_VERSION = '20150910' DEFAULT_CLIENT_LANG = 'en' diff --git a/lib/api-ai-ruby/request/event_request.rb b/lib/api-ai-ruby/request/event_request.rb new file mode 100644 index 0000000..0da8dd1 --- /dev/null +++ b/lib/api-ai-ruby/request/event_request.rb @@ -0,0 +1,13 @@ +module ApiAiRuby + class EventRequest < ApiAiRuby::RequestQuery + + # @param client [ApiAiRuby::Client] + # @param options [Hash] + # @return [ApiAiRuby::EventRequest] + def initialize (client, options={}) + options[:lang] = client.api_lang + super client, options + @headers['Content-Type'] = 'application/json; charset=UTF-8' + end + end +end diff --git a/lib/api-ai-ruby/request/request_query.rb b/lib/api-ai-ruby/request/request_query.rb index 6aec444..2a6f691 100644 --- a/lib/api-ai-ruby/request/request_query.rb +++ b/lib/api-ai-ruby/request/request_query.rb @@ -7,7 +7,7 @@ class RequestQuery # @param client [ApiAiRuby::Client] # @param options [Hash] - # @return [ApiAiRuby::TextRequest] + # @return [ApiAiRuby::RequestQuery] def initialize(client, options = {}) @client = client diff --git a/spec/api-ai-ruby/api_spec.rb b/spec/api-ai-ruby/api_spec.rb index f9fcfc6..5322947 100644 --- a/spec/api-ai-ruby/api_spec.rb +++ b/spec/api-ai-ruby/api_spec.rb @@ -104,5 +104,15 @@ @uer.delete('dwarfs') expect{@uer.retrieve('dwarfs')}.to raise_error(ApiAiRuby::RequestError) end + + # + # commented until test agent update + # + # it 'should invoke event' do + # response = @client.event_request 'WELCOME' + # expect(response[:result][:action]).to eq 'input.welcome' + # puts(response) + # end + end end