forked from wit-ai/wit-ruby
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquickstart.rb
More file actions
37 lines (33 loc) · 865 Bytes
/
quickstart.rb
File metadata and controls
37 lines (33 loc) · 865 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
require 'wit'
# Quickstart example
# See https://wit.ai/l5t/Quickstart
access_token = ARGV.shift
unless access_token
puts 'usage: ruby examples/quickstart.rb <access-token>'
exit
end
def first_entity_value(entities, entity)
return nil unless entities.has_key? entity
val = entities[entity][0]['value']
return nil if val.nil?
return val.is_a?(Hash) ? val['value'] : val
end
actions = {
:say => -> (session_id, context, msg) {
p msg
},
:merge => -> (session_id, context, entities, msg) {
loc = first_entity_value entities, 'location'
context['loc'] = loc unless loc.nil?
return context
},
:error => -> (session_id, context, error) {
p error.message
},
:'fetch-weather' => -> (session_id, context) {
context['forecast'] = 'sunny'
return context
},
}
client = Wit.new access_token, actions
client.interactive