Skip to content

Commit f02528d

Browse files
committed
[+] version 1.2.3 - added events support
1 parent 2e99005 commit f02528d

File tree

7 files changed

+45
-30
lines changed

7 files changed

+45
-30
lines changed

README.md

+7-27
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,14 @@ After that you can send text requests to the https://api.ai with command
2222
response = client.text_request 'hello!'
2323
```
2424

25-
And voice requests with file stream
25+
Or try to invocate intent via defined '[event](https://docs.api.ai/docs/concept-events)':
2626

2727
```ruby
28-
file = File.new 'hello.wav'
29-
response = client.voice_request(file)
30-
```
3128

32-
Example answer:
33-
```
34-
{
35-
:id => "6daf5ab7-276c-43ad-a32d-bf6831918492",
36-
:timestamp => "2015-12-22T08:42:15.785Z",
37-
:result => {
38-
:source => "agent",
39-
:resolvedQuery => "Hello",
40-
:speech => "Hi! How are you?",
41-
:action => "greeting",
42-
:parameters => {},
43-
:contexts => [],
44-
:metadata => {
45-
:intentId => "a5d685ab-1f19-46b0-9478-69f794553668",
46-
:intentName => "hello"
47-
}
48-
},
49-
:status => {
50-
:code => 200,
51-
:errorType => "success"
52-
}
53-
}
29+
response_zero = client.event_request 'MY_CUSTOM_EVENT_NAME';
30+
response_one = client.event_request 'MY_EVENT_WITH_DATA_TO_STORE', {:param1 => 'value'}
31+
response_two = client.event_request 'MY_EVENT_WITH_DATA_TO_STORE', {:some_param => 'some_value'}, :resetContexts => true
32+
5433
```
5534

5635
**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
7352

7453
```ruby
7554
response = client.text_request 'Hello', :contexts => ['firstContext'], :resetContexts => true
76-
response = client.voice_request file, :timezone => "America/New_York"
55+
response = client.voice_request file, :timezone => 'America/New_York'
7756
```
7857

7958
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
162141

163142
#Changelog
164143

144+
* 1.2.3 - events support
165145
* 1.2.2 - added configurable timeouts for requests (thanks [bramski](https://github.com/bramski))
166146
* 1.2.1 - fixed UTF-8 in text-requests
167147
* 1.2.0 - added configurable session_id and full userEntities support

lib/api-ai-ruby.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
require 'api-ai-ruby/client_error'
55
require 'api-ai-ruby/request/request_query'
66
require 'api-ai-ruby/request/text_request'
7+
require 'api-ai-ruby/request/event_request'
78
require 'api-ai-ruby/request/voice_request'
89
require 'api-ai-ruby/models/entry'
910
require 'api-ai-ruby/models/entity'
1011
require 'api-ai-ruby/crud/user_entity_request'
11-

lib/api-ai-ruby/client.rb

+12
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ def text_request (query = '', options = {})
5959
ApiAiRuby::TextRequest.new(self, options).perform
6060
end
6161

62+
# @param event_name [String]
63+
# @param data [Object]
64+
# @param options [Object]
65+
def event_request (event_name = '', data = {}, options = {})
66+
raise ApiAiRuby::ClientError.new('Credentials missing') if !credentials?
67+
options[:event] = {
68+
name: event_name,
69+
data: data
70+
}
71+
ApiAiRuby::EventRequest.new(self, options).perform
72+
end
73+
6274
def voice_request(file_stream, options = {})
6375
raise ApiAiRuby::ClientError.new('Credentials missing') if !credentials?
6476
options[:file] = file_stream

lib/api-ai-ruby/constants.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module ApiAiRuby
22
class Constants
3-
VERSION = '1.2.2'
3+
VERSION = '1.2.3'
44
DEFAULT_BASE_URL = 'https://api.api.ai/v1/'
55
DEFAULT_API_VERSION = '20150910'
66
DEFAULT_CLIENT_LANG = 'en'
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module ApiAiRuby
2+
class EventRequest < ApiAiRuby::RequestQuery
3+
4+
# @param client [ApiAiRuby::Client]
5+
# @param options [Hash]
6+
# @return [ApiAiRuby::EventRequest]
7+
def initialize (client, options={})
8+
options[:lang] = client.api_lang
9+
super client, options
10+
@headers['Content-Type'] = 'application/json; charset=UTF-8'
11+
end
12+
end
13+
end

lib/api-ai-ruby/request/request_query.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class RequestQuery
77

88
# @param client [ApiAiRuby::Client]
99
# @param options [Hash]
10-
# @return [ApiAiRuby::TextRequest]
10+
# @return [ApiAiRuby::RequestQuery]
1111

1212
def initialize(client, options = {})
1313
@client = client

spec/api-ai-ruby/api_spec.rb

+10
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,15 @@
104104
@uer.delete('dwarfs')
105105
expect{@uer.retrieve('dwarfs')}.to raise_error(ApiAiRuby::RequestError)
106106
end
107+
108+
#
109+
# commented until test agent update
110+
#
111+
# it 'should invoke event' do
112+
# response = @client.event_request 'WELCOME'
113+
# expect(response[:result][:action]).to eq 'input.welcome'
114+
# puts(response)
115+
# end
116+
107117
end
108118
end

0 commit comments

Comments
 (0)