wit 3.0.0 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/CHANGES.md +5 -0
- data/Gemfile +1 -0
- data/README.md +98 -2
- data/lib/wit.rb +25 -7
- data/wit.gemspec +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8346c258aff676710321e7452750f3ea6eff3a56
|
4
|
+
data.tar.gz: 625b24dc2223fef2500303d98c409deea91b3f43
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fe28aba17bdb04e4a4614edc99bad441434c7c6f5056273e9c0092e6f1d38783202d74e7c040a199cc47314cfcd98fd4b8291b0395a4f7f17dd4dd98979147a4
|
7
|
+
data.tar.gz: d4b6ee270ca08b5f0f367bd06dae05e9e4eb3d5aa4b9d8e35cb085ab184dd9a12069e5baaeab50d9cf97f18ade9e7042cf4ee2bc501231339acce9c5a6974263
|
data/.gitignore
CHANGED
data/CHANGES.md
CHANGED
data/Gemfile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
gemspec
|
data/README.md
CHANGED
@@ -22,13 +22,109 @@ See the `examples` folder for examples.
|
|
22
22
|
|
23
23
|
## API
|
24
24
|
|
25
|
+
### Overview
|
26
|
+
|
25
27
|
`wit-ruby` provides a Wit class with the following methods:
|
26
|
-
* `message` - the Wit message API
|
27
|
-
* `converse` - the low-level Wit converse API
|
28
|
+
* `message` - the Wit [message API](https://wit.ai/docs/http/20160330#get-intent-via-text-link)
|
29
|
+
* `converse` - the low-level Wit [converse API](https://wit.ai/docs/http/20160330#converse-link)
|
28
30
|
* `run_actions` - a higher-level method to the Wit converse API
|
29
31
|
|
32
|
+
### Wit class
|
33
|
+
|
34
|
+
The Wit constructor takes the following parameters:
|
35
|
+
* `access_token` - the access token of your Wit instance
|
36
|
+
* `actions` - the `Hash` with your actions
|
37
|
+
|
38
|
+
The `actions` `Hash` has action names as keys, and action implementations as values.
|
39
|
+
Action names are symbols, and action implementations are lambda functions (not `Proc`).
|
40
|
+
You need to provide at least an implementation for the special actions `:say`, `:merge` and `:error`.
|
41
|
+
|
42
|
+
A minimal `actions` `Hash` looks like this:
|
43
|
+
```ruby
|
44
|
+
actions = {
|
45
|
+
:say => -> (session_id, msg) {
|
46
|
+
p msg
|
47
|
+
},
|
48
|
+
:merge => -> (context, entities) {
|
49
|
+
return context
|
50
|
+
},
|
51
|
+
:error => -> (session_id, msg) {
|
52
|
+
p 'Oops I don\'t know what to do.'
|
53
|
+
},
|
54
|
+
}
|
55
|
+
```
|
56
|
+
|
57
|
+
A custom action takes one parameter:
|
58
|
+
* `context` - the `Hash` representing the session state
|
59
|
+
|
60
|
+
Example:
|
61
|
+
```ruby
|
62
|
+
require 'wit'
|
63
|
+
client = Wit.new access_token, actions
|
64
|
+
```
|
65
|
+
|
66
|
+
### Logging
|
67
|
+
|
68
|
+
Default logging is to `STDOUT` with `INFO` level.
|
69
|
+
|
70
|
+
You can setup your logging level as follows:
|
71
|
+
```ruby
|
72
|
+
Wit.logger.level = Logger::WARN
|
73
|
+
```
|
74
|
+
See the [Logger class](http://ruby-doc.org/stdlib-2.1.0/libdoc/logger/rdoc/Logger.html) docs for more information.
|
75
|
+
|
76
|
+
### message
|
77
|
+
|
78
|
+
The Wit [message API](https://wit.ai/docs/http/20160330#get-intent-via-text-link).
|
79
|
+
|
80
|
+
Takes the following parameters:
|
81
|
+
* `msg` - the text you want Wit.ai to extract the information from
|
82
|
+
|
83
|
+
Example:
|
84
|
+
```ruby
|
85
|
+
resp = client.message 'what is the weather in London?'
|
86
|
+
p "Yay, got Wit.ai response: #{resp}"
|
87
|
+
```
|
88
|
+
|
89
|
+
### run_actions
|
90
|
+
|
91
|
+
A higher-level method to the Wit converse API.
|
92
|
+
|
93
|
+
Takes the following parameters:
|
94
|
+
* `session_id` - a unique identifier describing the user session
|
95
|
+
* `message` - the text received from the user
|
96
|
+
* `context` - the `Hash` representing the session state
|
97
|
+
* `max_steps` - (optional) the maximum number of actions to execute (defaults to 5)
|
98
|
+
|
99
|
+
Example:
|
100
|
+
```ruby
|
101
|
+
session = 'my-user-session-42'
|
102
|
+
context0 = {}
|
103
|
+
context1 = client.run_actions session, 'what is the weather in London?', context0
|
104
|
+
p "The session state is now: #{context1}"
|
105
|
+
context2 = client.run_actions session, 'and in Brussels?', context1
|
106
|
+
p "The session state is now: #{context2}"
|
107
|
+
```
|
108
|
+
|
109
|
+
### converse
|
110
|
+
|
111
|
+
The low-level Wit [converse API](https://wit.ai/docs/http/20160330#converse-link).
|
112
|
+
|
113
|
+
Takes the following parameters:
|
114
|
+
* `session_id` - a unique identifier describing the user session
|
115
|
+
* `msg` - the text received from the user
|
116
|
+
* `context` - the `Hash` representing the session state
|
117
|
+
|
118
|
+
Example:
|
119
|
+
```ruby
|
120
|
+
resp = client.converse 'my-user-session-42', 'what is the weather in London?', {}
|
121
|
+
p "Yay, got Wit.ai response: #{resp}"
|
122
|
+
```
|
123
|
+
|
124
|
+
|
30
125
|
See the [docs](https://wit.ai/docs) for more information.
|
31
126
|
|
127
|
+
|
32
128
|
## Thanks
|
33
129
|
|
34
130
|
Thanks to [Justin Workman](http://github.com/xtagon) for releasing a first version in October 2013. We really appreciate!
|
data/lib/wit.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'json'
|
2
|
+
require 'logger'
|
2
3
|
require 'net/http'
|
3
4
|
|
4
5
|
WIT_API_HOST = ENV['WIT_URL'] || 'https://api.wit.ai'
|
@@ -20,7 +21,9 @@ def req(access_token, meth_class, path, params={}, payload={})
|
|
20
21
|
Net::HTTP.start uri.host, uri.port, {:use_ssl => uri.scheme == 'https'} do |http|
|
21
22
|
rsp = http.request request
|
22
23
|
raise WitException.new "HTTP error code=#{rsp.code}" unless rsp.code.to_i <= 200
|
23
|
-
JSON.parse rsp.body
|
24
|
+
json = JSON.parse rsp.body
|
25
|
+
raise WitException.new "Wit responded with an error: #{json['error']}" if json.has_key? 'error'
|
26
|
+
json
|
24
27
|
end
|
25
28
|
end
|
26
29
|
|
@@ -42,11 +45,26 @@ def validate_actions(actions)
|
|
42
45
|
end
|
43
46
|
|
44
47
|
class Wit
|
48
|
+
class << self
|
49
|
+
attr_writer :logger
|
50
|
+
|
51
|
+
def logger
|
52
|
+
@logger ||= begin
|
53
|
+
$stdout.sync = true
|
54
|
+
Logger.new(STDOUT)
|
55
|
+
end.tap { |logger| logger.level = Logger::INFO }
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
45
59
|
def initialize(access_token, actions)
|
46
60
|
@access_token = access_token
|
47
61
|
@actions = validate_actions actions
|
48
62
|
end
|
49
63
|
|
64
|
+
def logger
|
65
|
+
self.class.logger
|
66
|
+
end
|
67
|
+
|
50
68
|
def message(msg)
|
51
69
|
params = {}
|
52
70
|
params[:q] = msg unless msg.nil?
|
@@ -72,28 +90,28 @@ class Wit
|
|
72
90
|
if type == 'msg'
|
73
91
|
raise WitException.new 'unknown action: say' unless @actions.has_key? :say
|
74
92
|
msg = rst['msg']
|
75
|
-
|
93
|
+
logger.info "Executing say with: #{msg}"
|
76
94
|
@actions[:say].call session_id, msg
|
77
95
|
elsif type == 'merge'
|
78
96
|
raise WitException.new 'unknown action: merge' unless @actions.has_key? :merge
|
79
|
-
|
97
|
+
logger.info 'Executing merge'
|
80
98
|
context = @actions[:merge].call context, rst['entities']
|
81
99
|
if context.nil?
|
82
|
-
|
100
|
+
logger.warn 'missing context - did you forget to return it?'
|
83
101
|
context = {}
|
84
102
|
end
|
85
103
|
elsif type == 'action'
|
86
104
|
action = rst['action'].to_sym
|
87
105
|
raise WitException.new "unknown action: #{action}" unless @actions.has_key? action
|
88
|
-
|
106
|
+
logger.info "Executing action #{action}"
|
89
107
|
context = @actions[action].call context
|
90
108
|
if context.nil?
|
91
|
-
|
109
|
+
logger.warn 'missing context - did you forget to return it?'
|
92
110
|
context = {}
|
93
111
|
end
|
94
112
|
elsif type == 'error'
|
95
113
|
raise WitException.new 'unknown action: error' unless @actions.has_key? :error
|
96
|
-
|
114
|
+
logger.info 'Executing error'
|
97
115
|
@actions[:error].call session_id, 'unknown action: error'
|
98
116
|
else
|
99
117
|
raise WitException.new "unknown type: #{type}"
|
data/wit.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- The Wit Team
|
@@ -18,6 +18,7 @@ extra_rdoc_files: []
|
|
18
18
|
files:
|
19
19
|
- ".gitignore"
|
20
20
|
- CHANGES.md
|
21
|
+
- Gemfile
|
21
22
|
- LICENSE
|
22
23
|
- README.md
|
23
24
|
- examples/joke.rb
|