wit_bot 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c493abbbb038f58cbea34d66f5922f11841580e4
4
- data.tar.gz: 2a09ae8130ade22b6c5f95c03a017ed6a9987556
3
+ metadata.gz: 549a0b5878b37d6cad5a60ce08cf73d24e6c00f5
4
+ data.tar.gz: f0a17fab1335c1df10d6f1613bf82ae8e423e29b
5
5
  SHA512:
6
- metadata.gz: c9fb38619aec37a9c3cca4f65ae046a8753cb7c9bc5a8d1716cfa4bc6f14fe314ec0927f0ca255f031218cb4cae3d06d801d9ceb89c1333753ba69fe7fe92db2
7
- data.tar.gz: 8fe3523ab7b36bfe08cbefd181401c24ff8f6ddce7cccef214eb675a642a5ee13025cf85be5e7fcda676b31f7d4644d9cc9a26717a4c398e08c57bd8934cdb3c
6
+ metadata.gz: 98de64370554a72a83b26f0600512c2e80e165885b61a5238da82b7406bc0be094ed3284d597d0043cb9570eb4a1e9b02f46c44ce09abfe9e257cdd90e26388c
7
+ data.tar.gz: f18fddda7d520c9999b5236e1c896a8a7fb3a90b9c05d6f17ffb44840bb77dfca8df39fc5c56d9863cad30e232c9b6e1f11209204845e82900ecb1c7e0f322b9
@@ -0,0 +1,20 @@
1
+ #require 'wit_bot'
2
+
3
+ require_relative '../lib/wit_bot'
4
+
5
+ require_relative 'bot/bot'
6
+
7
+ WitBot.configure do |c|
8
+ c.token = ENV['WIT_ACCESS_TOKEN'] # Create a wit bot and set the token here
9
+ end
10
+
11
+ WitBot.config
12
+
13
+ conversation = WitBot::Bot::Conversation::Base.new
14
+
15
+ bot = Bot.new conversation
16
+
17
+ loop do
18
+ print 'Input: '
19
+ conversation.send_message gets.chomp
20
+ end
@@ -0,0 +1,38 @@
1
+ class Bot < WitBot::Bot::Base
2
+ intents :Test, :login, :missing_pin
3
+
4
+ protected
5
+
6
+ def on_output(message)
7
+ puts "Output: #{message.text}"
8
+ end
9
+
10
+ def on_missing_pin(message)
11
+ puts 'Pin used to be missing.'
12
+ login message.outcome
13
+ end
14
+
15
+ def on_login(message)
16
+ login message.outcome
17
+ end
18
+
19
+ def login(outcome)
20
+ if (pin_code = outcome.entities[:pin_code])
21
+ output "Logged you in with the pin code: #{pin_code}"
22
+ else
23
+ conversation.thread.context.state << :missing_pin
24
+ output 'Please include a pin code.'
25
+ end
26
+ end
27
+
28
+ def on_Test(message)
29
+ outcome = message.outcome
30
+
31
+ thing = outcome.entities["thing"]
32
+ adjective = outcome.entities["adjective"]
33
+
34
+ msg = "Yup, that #{thing || 'thing'} is #{adjective || 'a word'}."
35
+
36
+ output msg
37
+ end
38
+ end
@@ -1,7 +1,10 @@
1
1
  require 'active_support/core_ext/hash/indifferent_access'
2
2
  require 'active_support/core_ext/module/attribute_accessors'
3
+ require 'active_support/ordered_hash'
4
+ require 'active_support/concern'
3
5
  require 'http'
4
6
  require 'require_all'
7
+ require 'observer'
5
8
 
6
9
  module WitBot
7
10
  class << self
@@ -0,0 +1,45 @@
1
+ module WitBot
2
+ module Bot
3
+ class Base < WitBot::Bot::Conversation::Participant
4
+ def initialize(conversation)
5
+ super conversation
6
+ end
7
+
8
+ class << self
9
+ @@intents = []
10
+ def intents(*args)
11
+ if args.empty?
12
+ @@intents.map! do |intent|
13
+ if intent.is_a? WitBot::WitModel::Intent
14
+ intent
15
+ else
16
+ WitBot::WitModel::Intent.find intent
17
+ end
18
+ end
19
+ else
20
+ @@intents.concat args
21
+ end
22
+ end
23
+ end
24
+
25
+ def intents
26
+ self.class.intents
27
+ end
28
+
29
+ def intent_names
30
+ intents.map { |intent| intent.name }
31
+ end
32
+
33
+ def on_input(message)
34
+ outcome = message.outcome
35
+ intent = outcome.intent
36
+ intent_name = intent.name.to_sym
37
+
38
+ if intent_names.include? intent_name
39
+ method_name = "on_#{intent_name}".to_sym
40
+ self.send method_name, message if self.respond_to? method_name, true
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,44 @@
1
+ module WitBot
2
+ module Bot
3
+ module Conversation
4
+ class Base
5
+ include Observable
6
+
7
+ def metadata
8
+ thread.metadata
9
+ end
10
+
11
+ def thread
12
+ @thread ||= MessageThread.new("#{self.class.name}-#{SecureRandom.uuid}")
13
+ end
14
+
15
+ def messages(user: true, bot: true)
16
+ thread.messages_list user, bot
17
+ end
18
+
19
+ def send_message(text, sender=nil)
20
+ sender && sender.bot? ? output(text) : input(text)
21
+ end
22
+
23
+ def input(input)
24
+ message = thread.create_message input
25
+ message.send 1, false
26
+ changed
27
+ notify_observers :input, message
28
+ message
29
+ end
30
+
31
+ def listen_with(listener_class)
32
+ listener_class.new self
33
+ end
34
+
35
+ def output(text)
36
+ message = thread.create_bot_message text
37
+ changed
38
+ notify_observers :output, message
39
+ message
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,28 @@
1
+ module WitBot
2
+ module Bot
3
+ module Conversation
4
+ class Participant
5
+ attr_reader :conversation
6
+
7
+ def initialize(conversation)
8
+ @conversation = conversation
9
+ @conversation.add_observer self
10
+ end
11
+ def bot?
12
+ true
13
+ end
14
+ def output(text)
15
+ @conversation.send_message text, self
16
+ end
17
+ def update(type, message)
18
+ case type
19
+ when :input
20
+ self.on_input message if self.respond_to? :on_input, true
21
+ when :output
22
+ self.on_output message if self.respond_to? :on_output, true
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,12 @@
1
+ module WitBot
2
+ module Bot
3
+ class Message < WitBot::Message
4
+ def bot?
5
+ true
6
+ end
7
+ def send
8
+ throw "You can't send a bot message."
9
+ end
10
+ end
11
+ end
12
+ end
@@ -1,7 +1,11 @@
1
1
  module WitBot
2
2
  class Context
3
- attr_accessor :state, :reference_time, :location
4
- attr_writer :hash, :entities
3
+ attr_accessor :reference_time, :location
4
+ attr_writer :hash, :entities, :state
5
+
6
+ def state
7
+ @state ||= []
8
+ end
5
9
 
6
10
  def empty?
7
11
  as_json.empty?
@@ -13,7 +17,7 @@ module WitBot
13
17
 
14
18
  def as_json
15
19
  h = {}
16
- h[:state] = state if state
20
+ h[:state] = state if state && !state.empty?
17
21
  h[:reference_time] = reference_time.iso8601 if reference_time
18
22
  h[:entities] = entities if @entities
19
23
  h[:location] = location if location
@@ -11,6 +11,10 @@ module WitBot
11
11
  @outcomes = @_outcomes = nil
12
12
  end
13
13
 
14
+ def bot?
15
+ false
16
+ end
17
+
14
18
  def params(p=nil)
15
19
  params = {
16
20
  q: text,
@@ -25,7 +29,7 @@ module WitBot
25
29
  response = MessageRequest.new.request(self, n)
26
30
 
27
31
  @sent = true
28
- message.thread.reset_context unless keep_context
32
+ thread.reset_context unless keep_context
29
33
 
30
34
  @_text = response['_text']
31
35
  @_outcomes = response['outcomes']
@@ -1,22 +1,31 @@
1
1
  module WitBot
2
2
  class MessageThread
3
- attr_reader :id, :messages
3
+ attr_accessor :metadata
4
+
5
+ attr_reader :id, :messages, :bot_messages
4
6
 
5
7
  def initialize(id=SecureRandom.uuid)
6
8
  @id = id
7
- @messages = {}
9
+ @messages = ActiveSupport::OrderedHash.new
10
+ @bot_messages = ActiveSupport::OrderedHash.new
8
11
  end
9
12
 
10
13
  def message(id)
11
14
  messages[id]
12
15
  end
13
16
 
14
- def messages_list
15
- messages.values
17
+ def messages_list(user: true, bot: false)
18
+ bot_messages = bot ? @bot_messages.values : []
19
+ user_messages = user ? @messages.values : []
20
+ bot_messages + user_messages
16
21
  end
17
22
 
18
23
  def create_message(text, id=SecureRandom.uuid)
19
- WitBot::Message.new self, text, id: id
24
+ @messages[id] = WitBot::Message.new self, text, id: id
25
+ end
26
+
27
+ def create_bot_message(text, id=SecureRandom.uuid)
28
+ @bot_messages[id] = WitBot::Bot::Message.new self, text, id: id
20
29
  end
21
30
 
22
31
  def context
@@ -19,7 +19,7 @@ module WitBot
19
19
 
20
20
  entities[role] = data
21
21
  entities
22
- end
22
+ end.with_indifferent_access
23
23
 
24
24
  raise LowConfidenceError self if i == 0 && low_confidence?
25
25
  end
@@ -1,7 +1,7 @@
1
1
  module WitBot
2
2
  class EntityModel
3
3
  attr_accessor :others
4
- attr_reader :entity, :role, :raw
4
+ attr_reader :entity, :role, :raw, :value
5
5
 
6
6
  def initialize(entity, role, raw)
7
7
  @entity = entity
@@ -10,12 +10,16 @@ module WitBot
10
10
 
11
11
  @value = case @raw[:type].to_sym
12
12
  when :value
13
-
13
+ @raw[:value].to_s
14
14
  else
15
15
  @raw[:value]
16
16
  end
17
17
 
18
18
  @others = []
19
19
  end
20
+
21
+ def to_s
22
+ value.to_s
23
+ end
20
24
  end
21
25
  end
@@ -1,3 +1,3 @@
1
1
  module WitBot
2
- VERSION = '0.1.2'
2
+ VERSION = '0.2.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wit_bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben (@penne12_)
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-14 00:00:00.000000000 Z
11
+ date: 2016-03-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -206,9 +206,15 @@ files:
206
206
  - bin/console
207
207
  - bin/setup
208
208
  - bin/wit
209
+ - examples/bot.rb
210
+ - examples/bot/bot.rb
209
211
  - examples/simple.rb
210
212
  - examples/thread.rb
211
213
  - lib/wit_bot.rb
214
+ - lib/wit_bot/bot/base.rb
215
+ - lib/wit_bot/bot/conversation/base.rb
216
+ - lib/wit_bot/bot/conversation/participant.rb
217
+ - lib/wit_bot/bot/models/message.rb
212
218
  - lib/wit_bot/configuration.rb
213
219
  - lib/wit_bot/errors/low_confidence_error.rb
214
220
  - lib/wit_bot/errors/wit_bot_error.rb