forked from wit-ai/wit-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoke.rb
More file actions
60 lines (51 loc) · 1.58 KB
/
joke.rb
File metadata and controls
60 lines (51 loc) · 1.58 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
require 'wit'
if ARGV.length == 0
puts("usage: #{$0} <wit-access-token>")
exit 1
end
access_token = ARGV[0]
ARGV.shift
# Joke example
# See https://wit.ai/patapizza/example-joke
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
all_jokes = {
'chuck' => [
'Chuck Norris counted to infinity - twice.',
'Death once had a near-Chuck Norris experience.',
],
'tech' => [
'Did you hear about the two antennas that got married? The ceremony was long and boring, but the reception was great!',
'Why do geeks mistake Halloween and Christmas? Because Oct 31 === Dec 25.',
],
'default' => [
'Why was the Math book sad? Because it had so many problems.',
],
}
actions = {
send: -> (request, response) {
puts("sending... #{response['text']}")
},
merge: -> (request) {
context = request['context']
entities = request['entities']
context.delete 'joke'
context.delete 'ack'
category = first_entity_value(entities, 'category')
context['category'] = category unless category.nil?
sentiment = first_entity_value(entities, 'sentiment')
context['ack'] = sentiment == 'positive' ? 'Glad you liked it.' : 'Hmm.' unless sentiment.nil?
return context
},
:'select-joke' => -> (request) {
context = request['context']
context['joke'] = all_jokes[context['category'] || 'default'].sample
return context
},
}
client = Wit.new(access_token: access_token, actions: actions)
client.interactive