Skip to content

Commit

Permalink
[+] version 1.2.3 - added events support
Browse files Browse the repository at this point in the history
  • Loading branch information
Gugic committed Dec 5, 2016
1 parent 2e99005 commit f02528d
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 30 deletions.
34 changes: 7 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/api-ai-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'

12 changes: 12 additions & 0 deletions lib/api-ai-ruby/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/api-ai-ruby/constants.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
13 changes: 13 additions & 0 deletions lib/api-ai-ruby/request/event_request.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/api-ai-ruby/request/request_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions spec/api-ai-ruby/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f02528d

Please sign in to comment.