wit-ruby
is the Ruby SDK for Wit.ai.
From RubyGems:
gem install wit
From source:
git clone https://github.com/wit-ai/wit-ruby
gem build wit.gemspec
gem install wit-*.gem
Run in your terminal:
ruby examples/quickstart.rb <your_token>
See the examples
folder for more examples.
wit-ruby
provides a Wit class with the following methods:
message
- the Wit message APIconverse
- the low-level Wit converse APIrun_actions
- a higher-level method to the Wit converse APIinteractive
- starts an interactive conversation with your bot
The Wit constructor takes a Hash
with the following symbol keys:
:access_token
- the access token of your Wit instance:actions
- theHash
with your actions
The actions
Hash
has action names as keys, and action implementations as values.
Action names are symbols, and action implementations are lambda functions (not Proc
).
A minimal example looks like this:
require 'wit'
actions = {
send: -> (request, response) {
puts("sending... #{response['text']}")
},
my_action: -> (request) {
return request['context']
},
}
client = Wit.new(access_token: access_token, actions: actions)
The Wit message API.
Takes the following parameters:
msg
- the text you want Wit.ai to extract the information from
Example:
rsp = client.message('what is the weather in London?')
puts("Yay, got Wit.ai response: #{rsp}")
A higher-level method to the Wit converse API.
run_actions
resets the last turn on new messages and errors.
Takes the following parameters:
session_id
- a unique identifier describing the user sessionmessage
- the text received from the usercontext
- theHash
representing the session statemax_steps
- (optional) the maximum number of actions to execute (defaults to 5)
Example:
session = 'my-user-session-42'
context0 = {}
context1 = client.run_actions(session, 'what is the weather in London?', context0)
p "The session state is now: #{context1}"
context2 = client.run_actions(session, 'and in Brussels?', context1)
p "The session state is now: #{context2}"
The low-level Wit converse API.
Takes the following parameters:
session_id
- a unique identifier describing the user sessionmsg
- the text received from the usercontext
- theHash
representing the session statereset
- (optional) whether to reset the last turn
Example:
rsp = client.converse('my-user-session-42', 'what is the weather in London?', {})
puts("Yay, got Wit.ai response: #{rsp}")
Starts an interactive conversation with your bot.
Example:
client.interactive
payload in the parameters is a hash containing API arguments
Returns a list of available entities for the app.
See GET /entities
Creates a new entity with the given attributes.
See POST /entities
Returns all the expressions validated for an entity.
See GET /entities/:entity-id
Updates an entity with the given attributes.
See PUT /entities/:entity-id
Permanently remove the entity.
See DELETE /entities/:entity-id
Add a possible value into the list of values for the entity.
See POST /entities/:entity-id/values
Delete a canonical value from the entity.
See DELETE /entities/:entity-id/values/:value
Create a new expression of the canonical value of the entity.
See POST /entities/:entity-id/values/:value/expressions
Delete an expression of the canonical value of the entity.
See DELETE /entities/:entity-id/values/:value/expressions/:expression
See the docs for more information.
Default logging is to STDOUT
with INFO
level.
You can setup your logging level as follows:
Wit.logger.level = Logger::WARN
See the Logger class docs for more information.
Thanks to Justin Workman for releasing a first version in October 2013. We really appreciate!