Skip to content

Commit

Permalink
Sunset stories (#26)
Browse files Browse the repository at this point in the history
* sunsetting stories: removing converse and run_actions
  • Loading branch information
Aleka C authored and blandinw committed Feb 8, 2018
1 parent c9cc5fc commit c590b81
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 263 deletions.
8 changes: 8 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## v6.0.0
The most important change is the removal of `.converse()` and `.run_actions()`. Follow the migration tutorial [here](https://github.com/wit-ai/wit-stories-migration-tutorial), or [read more here](https://wit.ai/blog/2017/07/27/sunsetting-stories).

### Breaking changes

- `converse` and `run_actions` are removed
- updated and added new examples that leverage the /message API

## v5.0.0

- `converse` and `run_actions` are deprecated
Expand Down
43 changes: 1 addition & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ gem install wit-*.gem
Run in your terminal:

```bash
ruby examples/quickstart.rb <your_token>
ruby examples/basic.rb <your_token>
```

See the `examples` folder for more examples.
Expand Down Expand Up @@ -69,47 +69,6 @@ Example:
client.interactive
```

### .run_actions()

**DEPRECATED** See [our blog post](https://wit.ai/blog/2017/07/27/sunsetting-stories) for a migration plan.

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 session
* `message` - the text received from the user
* `context` - the `Hash` representing the session state
* `max_steps` - (optional) the maximum number of actions to execute (defaults to 5)

Example:
```ruby
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}"
```

### .converse()

**DEPRECATED** See [our blog post](https://wit.ai/blog/2017/07/27/sunsetting-stories) for a migration plan.

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

Takes the following parameters:
* `session_id` - a unique identifier describing the user session
* `msg` - the text received from the user
* `context` - the `Hash` representing the session state
* `reset` - (optional) whether to reset the last turn

Example:
```ruby
rsp = client.converse('my-user-session-42', 'what is the weather in London?', {})
puts("Yay, got Wit.ai response: #{rsp}")
```

### CRUD operations for entities
payload in the parameters is a hash containing API arguments

Expand Down
8 changes: 1 addition & 7 deletions examples/basic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,5 @@
access_token = ARGV[0]
ARGV.shift

actions = {
send: -> (request, response) {
puts("sending... #{response['text']}")
},
}

client = Wit.new(access_token: access_token, actions: actions)
client = Wit.new(access_token: access_token)
client.interactive
48 changes: 48 additions & 0 deletions examples/celebrities.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'wit'

if ARGV.length == 0
puts("usage: #{$0} <wit-access-token>")
exit 1
end

access_token = ARGV[0]
ARGV.shift

# Celebrities example
# See https://wit.ai/aforaleka/wit-example-celebrities/

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
end

def handle_message(response)
entities = response['entities']
greetings = first_entity_value(entities, 'greetings')
celebrity = first_entity_value(entities, 'notable_person')

case
when celebrity
return wikidata_description(celebrity)
when greetings
return "Hi! You can say something like 'Tell me about Beyonce'"
else
return "Um. I don't recognize that name. " \
"Which celebrity do you want to learn about?"
end
end

def wikidata_description(celebrity)
return "I recognize #{celebrity['name']}" unless celebrity.dig('external', 'wikidata')
wikidata_id = celebrity.fetch('external').fetch('wikidata')
api = URI("https://www.wikidata.org/w/api.php?action=wbgetentities&format=json&ids=#{wikidata_id}&props=descriptions&languages=en")
rsp = Net::HTTP.get_response(api)
wikidata = JSON.parse(rsp.body)
description = wikidata['entities'][wikidata_id]['descriptions']['en']['value']
return "ooo yes I know #{celebrity['name']} -- #{description}"
end

client = Wit.new(access_token: access_token)
client.interactive(method(:handle_message))
49 changes: 22 additions & 27 deletions examples/joke.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
ARGV.shift

# Joke example
# See https://wit.ai/patapizza/example-joke
# See https://wit.ai/aforaleka/wit-example-joke-bot/

def first_entity_value(entities, entity)
return nil unless entities.has_key? entity
Expand All @@ -18,7 +18,7 @@ def first_entity_value(entities, entity)
return val.is_a?(Hash) ? val['value'] : val
end

all_jokes = {
$all_jokes = {
'chuck' => [
'Chuck Norris counted to infinity - twice.',
'Death once had a near-Chuck Norris experience.',
Expand All @@ -32,29 +32,24 @@ def first_entity_value(entities, entity)
],
}

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
},
}
def handle_message(response)
entities = response['entities']
get_joke = first_entity_value(entities, 'getJoke')
greetings = first_entity_value(entities, 'greetings')
category = first_entity_value(entities, 'category')
sentiment = first_entity_value(entities, 'sentiment')

case
when get_joke
return $all_jokes[category || 'default'].sample
when sentiment
return sentiment == 'positive' ? 'Glad you liked it.' : 'Hmm.'
when greetings
return 'Hey this is joke bot :)'
else
return 'I can tell jokes! Say "tell me a joke about tech"!'
end
end

client = Wit.new(access_token: access_token, actions: actions)
client.interactive
client = Wit.new(access_token: access_token)
client.interactive(method(:handle_message))
43 changes: 0 additions & 43 deletions examples/quickstart.rb

This file was deleted.

Binary file added examples/wit-example-celebrities.zip
Binary file not shown.
Binary file added examples/wit-example-joke-bot.zip
Binary file not shown.
Loading

0 comments on commit c590b81

Please sign in to comment.