forked from wit-ai/wit-ruby
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjoke.rb
More file actions
53 lines (47 loc) · 1.53 KB
/
joke.rb
File metadata and controls
53 lines (47 loc) · 1.53 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
require 'wit'
# Joke example
# See https://wit.ai/patapizza/example-joke
access_token = 'YOUR_ACCESS_TOKEN'
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 = {
:say => -> (session_id, context, msg) {
p msg
},
:merge => -> (session_id, context, entities, msg) {
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
},
:error => -> (session_id, context, error) {
p error.message
},
:'select-joke' => -> (session_id, context) {
context['joke'] = all_jokes[context['cat'] || 'default'].sample
return context
},
}
client = Wit.new access_token, actions
session_id = 'my-user-id-42'
client.run_actions session_id, 'tell me a joke about tech', {}