lita-wit 0.0.1 → 0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d738b1713071dc4020098e21eb2432215b9df56a
4
- data.tar.gz: 0a001dc11e33eadec21eef5ceecf6e7b457205a5
3
+ metadata.gz: 2a6396a10176ffd117f4288900d89f9e721e08ba
4
+ data.tar.gz: 513bdf40c3b8d6913ad28272280d6df24e0c1bd5
5
5
  SHA512:
6
- metadata.gz: 608f03e1c4076e7d938b640720e50fc03d0afd6f6bf813b84dc8b947ffc27fb198225a45f7b654e0cbd8741cf8bbe105e7ee6d387a0350e02b78f205e18206ad
7
- data.tar.gz: 3650ecd01bbdaa00b25f3aae1f66df928414ef9b9e1b03989461b78fc8bb395f7789b7e3910b13787cff638c780a9f6b5a9111892e8a3c750ee29542554d00da
6
+ metadata.gz: b094722f4f49fec03c6640024265b1249fb7aba044c44c118d47be43d00f34ccd35b46dae80989408df8a9220b781e99c9cb046442bf9e49be20a1a615af61dc
7
+ data.tar.gz: a5f15301b5966e2cb334028cdf301bd1f657ba5b01d02a4f7a836c40878949f57cb7002d4cc89ae21390d6666a819a75627ea4d686c550290e5ee28fb1831b8c
data/README.md CHANGED
@@ -24,3 +24,8 @@ config.wit_token = token-here
24
24
  ## Usage
25
25
 
26
26
  Rock!
27
+
28
+ ## Thanks Go To
29
+
30
+ [Tom Beynon](https://github.com/tombeynon) - Great handler for Cleverbot [lita-ai](https://github.com/tombeynon/lita-ai)
31
+
@@ -1,7 +1,30 @@
1
1
  module Lita
2
2
  module Handlers
3
3
  class Wit < Handler
4
- # insert handler code here
4
+ on :unhandled_message, :handle
5
+ config :server_access_token, type: String, required: true
6
+
7
+ def initialize(robot)
8
+ super
9
+ @bickle = Lita::Utils::Bickle.new(robot)
10
+ @client = Lita::Services::WitClient.new(robot)
11
+ end
12
+
13
+ def handle(payload)
14
+ message = payload[:message]
15
+ return unless @bickle.you_talking_to_me?(message)
16
+ respond(message)
17
+ end
18
+
19
+ private
20
+
21
+ def respond(message)
22
+ @client.run_actions session_id, message
23
+ end
24
+
25
+ def session_id
26
+ 'unique-1234'
27
+ end
5
28
 
6
29
  Lita.register_handler(self)
7
30
  end
@@ -0,0 +1,48 @@
1
+ module Lita
2
+ module Services
3
+ class WitClient
4
+
5
+ def initialize(robot)
6
+ @robot = robot
7
+ token = @robot.config.handlers.wit.server_access_token
8
+ @wit = ::Wit.new token, actions
9
+ end
10
+
11
+ def run_actions(session_id, message, context={}, max_steps=DEFAULT_MAX_STEPS)
12
+ @source = message.source
13
+ stripped = Lita::Utils::AliasStripper.strip(@robot, message)
14
+ @wit.run_actions(session_id, stripped.body, context, max_steps)
15
+ end
16
+
17
+ private
18
+
19
+ def actions
20
+ {
21
+ :say => -> (session_id, context, msg) {
22
+ @robot.send_message(@source, msg)
23
+ },
24
+ :merge => -> (session_id, context, entities, msg) {
25
+ pipe(context, entities, 'intent', 'intent')
26
+ pipe(context, entities, 'location', 'loc')
27
+ context
28
+ },
29
+ :error => -> (session_id, context, error) {
30
+ # Required, but is never ever called
31
+ },
32
+ }
33
+ end
34
+
35
+ def first_entity_value(entities, entity)
36
+ return nil unless entities.has_key? entity
37
+ val = entities[entity][0]['value']
38
+ return nil if val.nil?
39
+ val.is_a?(Hash) ? val['value'] : val
40
+ end
41
+
42
+ def pipe(context, entities, input, output)
43
+ e = first_entity_value entities, input
44
+ context[output] = e unless e.nil?
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,10 @@
1
+ module Lita
2
+ module Utils
3
+ class AliasStripper
4
+ def self.strip(robot, message)
5
+ body = message.body.sub(/#{Aliases.values(robot).join('|')}/i, '').strip
6
+ Message.new(robot, body, message.source)
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ module Lita
2
+ module Utils
3
+ class Aliases
4
+ def self.values(robot)
5
+ [robot.mention_name, robot.alias].map { |a| a unless a == '' }.compact
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ module Lita
2
+ module Utils
3
+ class Bickle
4
+
5
+ def initialize(robot)
6
+ @robot = robot
7
+ end
8
+
9
+ def you_talking_to_me?(message)
10
+ message.command? || message.body =~ /#{Aliases.values(@robot).join('|')}/i
11
+ end
12
+ end
13
+ end
14
+ end
data/lib/lita-wit.rb CHANGED
@@ -1,12 +1,17 @@
1
- require "lita"
1
+ require 'lita'
2
+ require 'wit'
2
3
 
3
4
  Lita.load_locales Dir[File.expand_path(
4
- File.join("..", "..", "locales", "*.yml"), __FILE__
5
+ File.join('..', '..', 'locales', '*.yml'), __FILE__
5
6
  )]
6
7
 
7
- require "lita/handlers/wit"
8
+ require 'lita/handlers/wit'
9
+ require 'lita/utils/bickle'
10
+ require 'lita/utils/aliases'
11
+ require 'lita/utils/alias_stripper'
12
+ require 'lita/services/wit_client'
8
13
 
9
14
  Lita::Handlers::Wit.template_root File.expand_path(
10
- File.join("..", "..", "templates"),
11
- __FILE__
15
+ File.join('..', '..', 'templates'),
16
+ __FILE__
12
17
  )
data/lita-wit.gemspec CHANGED
@@ -1,9 +1,9 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "lita-wit"
3
- spec.version = "0.0.1"
3
+ spec.version = "0.1.0"
4
4
  spec.authors = ["Damien Bastin"]
5
5
  spec.email = ["damien.bastin@gmail.com"]
6
- spec.description = "A Lita handler for Wit"
6
+ spec.description = "A Lita handler for Wit."
7
7
  spec.summary = "Receive structured intentions from unstructured sentences using a Lita bot and Wit."
8
8
  spec.homepage = "https://github.com/dbastin/lita-wit"
9
9
  spec.license = "MIT"
@@ -15,6 +15,7 @@ Gem::Specification.new do |spec|
15
15
  spec.require_paths = ["lib"]
16
16
 
17
17
  spec.add_runtime_dependency "lita", ">= 4.7"
18
+ spec.add_runtime_dependency "wit", ">= 3.3.1"
18
19
 
19
20
  spec.add_development_dependency "bundler", ">= 1.3"
20
21
  spec.add_development_dependency "pry-byebug"
@@ -23,4 +24,6 @@ Gem::Specification.new do |spec|
23
24
  spec.add_development_dependency "rspec", ">= 3.0.0"
24
25
  spec.add_development_dependency "simplecov"
25
26
  spec.add_development_dependency "coveralls"
27
+ spec.add_development_dependency "webmock"
28
+ spec.add_development_dependency "vcr"
26
29
  end
@@ -1,4 +1,48 @@
1
- require "spec_helper"
1
+ require 'spec_helper'
2
2
 
3
3
  describe Lita::Handlers::Wit, lita_handler: true do
4
+
5
+ describe 'handling unhandled_message' do
6
+ let(:source) { double(:source) }
7
+
8
+ context 'unhandled directed at lita' do
9
+ it 'handles something' do
10
+ expect_any_instance_of(::Wit).to receive(:run_actions).with(SESSION_ID, 'Hi', {}, DEFAULT_MAX_STEPS) {
11
+ robot.send_message(source, 'Howdy')
12
+ }
13
+ send_message('Hi Lita')
14
+ expect(replies.last).to eq('Howdy')
15
+ end
16
+
17
+ it 'recognizes aliases' do
18
+ expect_any_instance_of(::Wit).to receive(:run_actions).with(SESSION_ID, 'Hi', {}, DEFAULT_MAX_STEPS) {
19
+ robot.send_message(source, 'Hiya')
20
+ }
21
+ robot.alias = 'Rita'
22
+ send_message('Hi Rita')
23
+ expect(replies.last).to eq('Hiya')
24
+ end
25
+ end
26
+
27
+ context 'unhandled not directed at lita' do
28
+ it 'ignores the message' do
29
+ send_message('Hi')
30
+ expect(replies.last).to be_nil
31
+ end
32
+ end
33
+ end
34
+
35
+ describe 'handle' do
36
+ let(:source) { double(:source) }
37
+ let(:message) { double(:message, body: 'Hello', command?: true, source: source) }
38
+
39
+ context 'commanding lita' do
40
+ it 'calls wit.ai and responds to source' do
41
+ VCR.use_cassette('wit greeting') do
42
+ expect(subject.robot).to receive(:send_message).with(source, 'Hi. Ask me about the weather. Anywhere. Go on!')
43
+ subject.handle(message: message)
44
+ end
45
+ end
46
+ end
47
+ end
4
48
  end
@@ -0,0 +1,8 @@
1
+ Lita.configure do |config|
2
+ config.robot.name = 'Lita'
3
+ config.robot.log_level = :info
4
+ config.robot.adapter = :shell
5
+
6
+ # https://wit.ai/dbastin/Lita... Fork it!
7
+ config.handlers.wit.server_access_token = '72XXMP6VAKG2SAPLTXVQS6H5PBLVQIJW'
8
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,6 @@
1
- require "simplecov"
1
+ require 'simplecov'
2
2
 
3
- require "coveralls"
3
+ require 'coveralls'
4
4
  Coveralls.wear!
5
5
 
6
6
  SimpleCov.start 'rails' do
@@ -12,9 +12,24 @@ SimpleCov.start 'rails' do
12
12
  add_filter '/spec/'
13
13
  end
14
14
 
15
- require "lita-wit"
16
- require "lita/rspec"
15
+ require 'lita-wit'
16
+ Wit.logger.level = Logger::WARN
17
+
18
+ require 'lita/rspec'
19
+ require 'lita_config'
17
20
 
18
21
  # A compatibility mode is provided for older plugins upgrading from Lita 3. Since this plugin
19
22
  # was generated with Lita 4, the compatibility mode should be left disabled.
20
23
  Lita.version_3_compatibility_mode = false
24
+
25
+ require 'vcr'
26
+ require 'webmock/rspec'
27
+
28
+ VCR.configure do |config|
29
+ config.cassette_library_dir = 'spec/support/vcr_cassettes'
30
+ config.ignore_localhost = true
31
+ config.hook_into :webmock
32
+ config.configure_rspec_metadata!
33
+ end
34
+
35
+ SESSION_ID = 'unique-1234'
File without changes
@@ -0,0 +1,138 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: https://api.wit.ai/converse?q=Hello&session_id=unique-1234
6
+ body:
7
+ encoding: UTF-8
8
+ string: "{}"
9
+ headers:
10
+ Accept-Encoding:
11
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
12
+ Accept:
13
+ - application/vnd.wit.20160330+json
14
+ User-Agent:
15
+ - Ruby
16
+ Host:
17
+ - api.wit.ai
18
+ Authorization:
19
+ - Bearer 72XXMP6VAKG2SAPLTXVQS6H5PBLVQIJW
20
+ Content-Type:
21
+ - application/json
22
+ response:
23
+ status:
24
+ code: 200
25
+ message: OK
26
+ headers:
27
+ Server:
28
+ - nginx/1.8.0
29
+ Date:
30
+ - Thu, 28 Apr 2016 15:14:39 GMT
31
+ Content-Type:
32
+ - application/json
33
+ Content-Length:
34
+ - '118'
35
+ Connection:
36
+ - keep-alive
37
+ body:
38
+ encoding: UTF-8
39
+ string: |-
40
+ {
41
+ "confidence" : 1,
42
+ "type" : "merge",
43
+ "entities" : {
44
+ "intent" : [ {
45
+ "value" : "greeting"
46
+ } ]
47
+ }
48
+ }
49
+ http_version:
50
+ recorded_at: Thu, 28 Apr 2016 15:14:39 GMT
51
+ - request:
52
+ method: post
53
+ uri: https://api.wit.ai/converse?session_id=unique-1234
54
+ body:
55
+ encoding: UTF-8
56
+ string: '{"intent":"greeting"}'
57
+ headers:
58
+ Accept-Encoding:
59
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
60
+ Accept:
61
+ - application/vnd.wit.20160330+json
62
+ User-Agent:
63
+ - Ruby
64
+ Host:
65
+ - api.wit.ai
66
+ Authorization:
67
+ - Bearer 72XXMP6VAKG2SAPLTXVQS6H5PBLVQIJW
68
+ Content-Type:
69
+ - application/json
70
+ response:
71
+ status:
72
+ code: 200
73
+ message: OK
74
+ headers:
75
+ Server:
76
+ - nginx/1.8.0
77
+ Date:
78
+ - Thu, 28 Apr 2016 15:14:40 GMT
79
+ Content-Type:
80
+ - application/json
81
+ Content-Length:
82
+ - '117'
83
+ Connection:
84
+ - keep-alive
85
+ body:
86
+ encoding: UTF-8
87
+ string: |-
88
+ {
89
+ "confidence" : 0.5878773927688599,
90
+ "type" : "msg",
91
+ "msg" : "Hi. Ask me about the weather. Anywhere. Go on!"
92
+ }
93
+ http_version:
94
+ recorded_at: Thu, 28 Apr 2016 15:14:40 GMT
95
+ - request:
96
+ method: post
97
+ uri: https://api.wit.ai/converse?session_id=unique-1234
98
+ body:
99
+ encoding: UTF-8
100
+ string: '{"intent":"greeting"}'
101
+ headers:
102
+ Accept-Encoding:
103
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
104
+ Accept:
105
+ - application/vnd.wit.20160330+json
106
+ User-Agent:
107
+ - Ruby
108
+ Host:
109
+ - api.wit.ai
110
+ Authorization:
111
+ - Bearer 72XXMP6VAKG2SAPLTXVQS6H5PBLVQIJW
112
+ Content-Type:
113
+ - application/json
114
+ response:
115
+ status:
116
+ code: 200
117
+ message: OK
118
+ headers:
119
+ Server:
120
+ - nginx/1.8.0
121
+ Date:
122
+ - Thu, 28 Apr 2016 15:14:41 GMT
123
+ Content-Type:
124
+ - application/json
125
+ Content-Length:
126
+ - '58'
127
+ Connection:
128
+ - keep-alive
129
+ body:
130
+ encoding: UTF-8
131
+ string: |-
132
+ {
133
+ "confidence" : 0.9979153275489807,
134
+ "type" : "stop"
135
+ }
136
+ http_version:
137
+ recorded_at: Thu, 28 Apr 2016 15:14:41 GMT
138
+ recorded_with: VCR 3.0.1
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-wit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Damien Bastin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-22 00:00:00.000000000 Z
11
+ date: 2016-04-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lita
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '4.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: wit
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 3.3.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 3.3.1
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -122,7 +136,35 @@ dependencies:
122
136
  - - ">="
123
137
  - !ruby/object:Gem::Version
124
138
  version: '0'
125
- description: A Lita handler for Wit
139
+ - !ruby/object:Gem::Dependency
140
+ name: webmock
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: vcr
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ description: A Lita handler for Wit.
126
168
  email:
127
169
  - damien.bastin@gmail.com
128
170
  executables: []
@@ -137,10 +179,17 @@ files:
137
179
  - Rakefile
138
180
  - lib/lita-wit.rb
139
181
  - lib/lita/handlers/wit.rb
182
+ - lib/lita/services/wit_client.rb
183
+ - lib/lita/utils/alias_stripper.rb
184
+ - lib/lita/utils/aliases.rb
185
+ - lib/lita/utils/bickle.rb
140
186
  - lita-wit.gemspec
141
187
  - locales/en.yml
142
188
  - spec/lita/handlers/wit_spec.rb
189
+ - spec/lita_config.rb
143
190
  - spec/spec_helper.rb
191
+ - spec/support/vcr_cassettes/.gitkeep
192
+ - spec/support/vcr_cassettes/wit_greeting.yml
144
193
  - templates/.gitkeep
145
194
  homepage: https://github.com/dbastin/lita-wit
146
195
  licenses:
@@ -170,4 +219,7 @@ summary: Receive structured intentions from unstructured sentences using a Lita
170
219
  and Wit.
171
220
  test_files:
172
221
  - spec/lita/handlers/wit_spec.rb
222
+ - spec/lita_config.rb
173
223
  - spec/spec_helper.rb
224
+ - spec/support/vcr_cassettes/.gitkeep
225
+ - spec/support/vcr_cassettes/wit_greeting.yml