Skip to content

Commit ac801a3

Browse files
author
Martin Raison
committed
upgrade for bot engine v1
1 parent 52fdbac commit ac801a3

File tree

8 files changed

+229
-171
lines changed

8 files changed

+229
-171
lines changed

CHANGES.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
## v4.0.0
2+
3+
After a lot of internal dogfooding and bot building, we decided to change the API in a backwards-incompatible way. The changes are described below and aim to simplify user code and accommodate upcoming features.
4+
5+
See `./examples` to see how to use the new API.
6+
7+
### Breaking changes
8+
9+
- `say` renamed to `send` to reflect that it deals with more than just text
10+
- Removed built-in actions `merge` and `error`
11+
- Actions signature simplified with `request` and `response` arguments
12+
- INFO level replaces LOG level
13+
114
## v3.4
215

316
- allows for overriding API version, by setting `WIT_API_VERSION`

README.md

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -38,50 +38,30 @@ See the `examples` folder for more examples.
3838

3939
### Wit class
4040

41-
The Wit constructor takes the following parameters:
42-
* `access_token` - the access token of your Wit instance
43-
* `actions` - the `Hash` with your actions
41+
The Wit constructor takes a `Hash` with the following symbol keys:
42+
* `:access_token` - the access token of your Wit instance
43+
* `:actions` - the `Hash` with your actions
4444

4545
The `actions` `Hash` has action names as keys, and action implementations as values.
4646
Action names are symbols, and action implementations are lambda functions (not `Proc`).
47-
You need to provide at least an implementation for the special actions `:say`, `:merge` and `:error`.
4847

49-
A minimal `actions` `Hash` looks like this:
48+
A minimal example looks like this:
5049
```ruby
50+
require 'wit'
51+
5152
actions = {
52-
:say => -> (session_id, context, msg) {
53-
p msg
53+
send: -> (request, response) {
54+
puts("sending... #{response['text']}")
5455
},
55-
:merge => -> (session_id, context, entities, msg) {
56-
return context
57-
},
58-
:error => -> (session_id, context, error) {
59-
p error.message
56+
my_action: -> (request) {
57+
return request['context']
6058
},
6159
}
62-
```
63-
64-
A custom action takes the following parameters:
65-
* `session_id` - a unique identifier describing the user session
66-
* `context` - the `Hash` representing the session state
67-
68-
Example:
69-
```ruby
70-
require 'wit'
71-
client = Wit.new access_token, actions
72-
```
73-
74-
### Logging
75-
76-
Default logging is to `STDOUT` with `INFO` level.
7760

78-
You can setup your logging level as follows:
79-
```ruby
80-
Wit.logger.level = Logger::WARN
61+
client = Wit.new(access_token: access_token, actions: actions)
8162
```
82-
See the [Logger class](http://ruby-doc.org/stdlib-2.1.0/libdoc/logger/rdoc/Logger.html) docs for more information.
8363

84-
### message
64+
### .message()
8565

8666
The Wit [message API](https://wit.ai/docs/http/20160330#get-intent-via-text-link).
8767

@@ -90,11 +70,11 @@ Takes the following parameters:
9070

9171
Example:
9272
```ruby
93-
resp = client.message 'what is the weather in London?'
94-
p "Yay, got Wit.ai response: #{resp}"
73+
rsp = client.message('what is the weather in London?')
74+
puts("Yay, got Wit.ai response: #{rsp}")
9575
```
9676

97-
### run_actions
77+
### .run_actions()
9878

9979
A higher-level method to the Wit converse API.
10080

@@ -108,13 +88,13 @@ Example:
10888
```ruby
10989
session = 'my-user-session-42'
11090
context0 = {}
111-
context1 = client.run_actions session, 'what is the weather in London?', context0
91+
context1 = client.run_actions(session, 'what is the weather in London?', context0)
11292
p "The session state is now: #{context1}"
113-
context2 = client.run_actions session, 'and in Brussels?', context1
93+
context2 = client.run_actions(session, 'and in Brussels?', context1)
11494
p "The session state is now: #{context2}"
11595
```
11696

117-
### converse
97+
### .converse()
11898

11999
The low-level Wit [converse API](https://wit.ai/docs/http/20160330#converse-link).
120100

@@ -125,12 +105,11 @@ Takes the following parameters:
125105

126106
Example:
127107
```ruby
128-
resp = client.converse 'my-user-session-42', 'what is the weather in London?', {}
129-
p "Yay, got Wit.ai response: #{resp}"
108+
rsp = client.converse('my-user-session-42', 'what is the weather in London?', {})
109+
puts("Yay, got Wit.ai response: #{rsp}")
130110
```
131111

132-
133-
### interactive
112+
### .interactive()
134113

135114
Starts an interactive conversation with your bot.
136115

@@ -141,6 +120,15 @@ client.interactive
141120

142121
See the [docs](https://wit.ai/docs) for more information.
143122

123+
### Logging
124+
125+
Default logging is to `STDOUT` with `INFO` level.
126+
127+
You can setup your logging level as follows:
128+
```ruby
129+
Wit.logger.level = Logger::WARN
130+
```
131+
See the [Logger class](http://ruby-doc.org/stdlib-2.1.0/libdoc/logger/rdoc/Logger.html) docs for more information.
144132

145133
## Thanks
146134

examples/basic.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require 'wit'
2+
3+
if ARGV.length == 0
4+
puts("usage: #{$0} <wit-access-token>")
5+
exit 1
6+
end
7+
8+
access_token = ARGV[0]
9+
ARGV.shift
10+
11+
actions = {
12+
send: -> (request, response) {
13+
puts("sending... #{response['text']}")
14+
},
15+
}
16+
17+
client = Wit.new(access_token: access_token, actions: actions)
18+
client.interactive

examples/joke.rb

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
require 'wit'
22

3+
if ARGV.length == 0
4+
puts("usage: #{$0} <wit-access-token>")
5+
exit 1
6+
end
7+
8+
access_token = ARGV[0]
9+
ARGV.shift
10+
311
# Joke example
412
# See https://wit.ai/patapizza/example-joke
513

6-
access_token = 'YOUR_ACCESS_TOKEN'
7-
814
def first_entity_value(entities, entity)
915
return nil unless entities.has_key? entity
1016
val = entities[entity][0]['value']
@@ -27,27 +33,28 @@ def first_entity_value(entities, entity)
2733
}
2834

2935
actions = {
30-
:say => -> (session_id, context, msg) {
31-
p msg
36+
send: -> (request, response) {
37+
puts("sending... #{response['text']}")
3238
},
33-
:merge => -> (session_id, context, entities, msg) {
39+
merge: -> (request) {
40+
context = request['context']
41+
entities = request['entities']
42+
3443
context.delete 'joke'
3544
context.delete 'ack'
36-
category = first_entity_value entities, 'category'
45+
category = first_entity_value(entities, 'category')
3746
context['category'] = category unless category.nil?
38-
sentiment = first_entity_value entities, 'sentiment'
47+
sentiment = first_entity_value(entities, 'sentiment')
3948
context['ack'] = sentiment == 'positive' ? 'Glad you liked it.' : 'Hmm.' unless sentiment.nil?
4049
return context
4150
},
42-
:error => -> (session_id, context, error) {
43-
p error.message
44-
},
45-
:'select-joke' => -> (session_id, context) {
46-
context['joke'] = all_jokes[context['cat'] || 'default'].sample
51+
:'select-joke' => -> (request) {
52+
context = request['context']
53+
54+
context['joke'] = all_jokes[context['category'] || 'default'].sample
4755
return context
4856
},
4957
}
50-
client = Wit.new access_token, actions
5158

52-
session_id = 'my-user-id-42'
53-
client.run_actions session_id, 'tell me a joke about tech', {}
59+
client = Wit.new(access_token: access_token, actions: actions)
60+
client.interactive

examples/quickstart.rb

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
require 'wit'
22

3-
# Quickstart example
4-
# See https://wit.ai/l5t/Quickstart
5-
6-
access_token = ARGV.shift
7-
unless access_token
8-
puts 'usage: ruby examples/quickstart.rb <access-token>'
9-
exit
3+
if ARGV.length == 0
4+
puts("usage: #{$0} <wit-access-token>")
5+
exit 1
106
end
117

8+
access_token = ARGV[0]
9+
ARGV.shift
10+
11+
# Quickstart example
12+
# See https://wit.ai/ar7hur/Quickstart
13+
1214
def first_entity_value(entities, entity)
1315
return nil unless entities.has_key? entity
1416
val = entities[entity][0]['value']
@@ -17,21 +19,24 @@ def first_entity_value(entities, entity)
1719
end
1820

1921
actions = {
20-
:say => -> (session_id, context, msg) {
21-
p msg
22+
send: -> (request, response) {
23+
puts("sending... #{response['text']}")
2224
},
23-
:merge => -> (session_id, context, entities, msg) {
24-
loc = first_entity_value entities, 'location'
25-
context['loc'] = loc unless loc.nil?
26-
return context
27-
},
28-
:error => -> (session_id, context, error) {
29-
p error.message
30-
},
31-
:'fetch-weather' => -> (session_id, context) {
32-
context['forecast'] = 'sunny'
25+
getForecast: -> (request) {
26+
context = request['context']
27+
entities = request['entities']
28+
29+
loc = first_entity_value(entities, 'location')
30+
if loc
31+
context['forecast'] = 'sunny'
32+
else
33+
context['missingLocation'] = true
34+
context.delete('forecast')
35+
end
36+
3337
return context
3438
},
3539
}
36-
client = Wit.new access_token, actions
40+
41+
client = Wit.new(access_token: access_token, actions: actions)
3742
client.interactive

examples/template.rb

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)