responder_bot 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fe5cb293edddcfa00abc32ec573cb25a8b4f34f5072d9e0997e7cc5fd213781d
4
+ data.tar.gz: b30614a7b3b6b88b8fe4d5a1a02b3be07bcb67fedf64409c0e30ab8962c4009e
5
+ SHA512:
6
+ metadata.gz: 40b73583b1aab9c3f7d0b9b1fb585d3e1c621473c428c52e6c66d9cd08ced31bca0aabd03ae65250938ad1c623fbc0ee8a2061248c3965d23a550b1d24bd0bad
7
+ data.tar.gz: 74bbb17447a8b79e6560c1643776a1e809b96c573f661b84b48a46dc7843048446029f4294828d6f9de697f1b43f3901751044b5e25d184a2914a11ba5bc0466
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ gemspec
@@ -0,0 +1,20 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ responder_bot (0.0.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ rake (10.5.0)
10
+
11
+ PLATFORMS
12
+ ruby
13
+
14
+ DEPENDENCIES
15
+ bundler (~> 1.16)
16
+ rake (~> 10.0)
17
+ responder_bot!
18
+
19
+ BUNDLED WITH
20
+ 1.16.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Jacob Evan Shreve
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,71 @@
1
+ # ResponderBot
2
+
3
+ A simple framework for defining a text-based interface
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'responder_bot'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install responder_bot
20
+
21
+ ## Usage
22
+
23
+ Check out the examples directory for detailed usage examples. This gem is a bit
24
+ complicated to put the usage inline here.
25
+
26
+ Here's a simple example to illustrate the idea at it's core.
27
+
28
+ ```ruby
29
+ class SimpleResponder < ResponderBot::Handler
30
+ handle_response do |reply|
31
+ reply.quit { "Ok, thanks for playing!" }
32
+ reply.likert { "I'm glad you feel that way." }
33
+ reply.yes { "Awesome, thanks." }
34
+ end
35
+ end
36
+
37
+ SimpleResponder.new("1").handle_response!
38
+ # => "I'm glad you feel that way."
39
+
40
+ SimpleResponder.new("Yes!").handle_response!
41
+ # => "Awesome, thanks."
42
+ ```
43
+
44
+ This example is super trivial, but don't worry, it gets better.
45
+
46
+ ## Development
47
+
48
+ After checking out the repo, run `bin/setup` to install dependencies. You can
49
+ also run `bin/console` for an interactive prompt that will allow you to
50
+ experiment.
51
+
52
+ To install this gem onto your local machine, run `bundle exec rake install`. To
53
+ release a new version, update the version number in `version.rb`, and then run
54
+ `bundle exec rake release`, which will create a git tag for the version, push
55
+ git commits and tags, and push the `.gem` file
56
+ to [rubygems.org](https://rubygems.org).
57
+
58
+ ## Contributing
59
+
60
+ Bug reports and pull requests are welcome on GitHub at
61
+ https://github.com/shreve/responder_bot.
62
+
63
+ I am particularly looking for feedback on
64
+
65
+ 1. Default matchers (`ResponderBot.default_matchers`)
66
+ 2. New types of matchers
67
+
68
+ ## License
69
+
70
+ The gem is available as open source under the terms of
71
+ the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,2 @@
1
+ require 'bundler/gem_tasks'
2
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "responder_bot"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,122 @@
1
+ require 'responder_bot'
2
+
3
+ #
4
+ # This top-level controller handles all incoming responses
5
+ #
6
+ class ResponseController < ResponderBot::Handler
7
+ matchers help: Regexp.new('^he?l?p?', 'i'),
8
+ balance: Regexp.new('^bala?n?c?e?', 'i')
9
+
10
+ # Any missing methods here are delegated to `respondable`, which is the only
11
+ # parameter used to instantiate this class.
12
+ handle_response do |reply|
13
+ # Prefer to interact with your respondable before anything else
14
+ if response_handler.understands_reply?
15
+ reply.any { response_handler.handle_response! }
16
+ end
17
+
18
+ if user.nil?
19
+ reply.any do
20
+ "We don't know who you are. You'll need to create an account before " \
21
+ 'you can use this SMS system.'
22
+ end
23
+ end
24
+
25
+ reply.help do
26
+ 'This is the SMS help text. Reply BAL to see your balance.'
27
+ end
28
+
29
+ reply.balance do
30
+ "You have #{user.points} points to spend."
31
+ end
32
+
33
+ # If you want help and balance to always be preferred over your respondable,
34
+ # just move this matcher to the bottom.
35
+ if response_handler.understands_reply?
36
+ reply.any { response_handler.handle_response! }
37
+ end
38
+
39
+ reply.any do
40
+ "We didn't understand what you said. Reply HELP for more info."
41
+ end
42
+ end
43
+ end
44
+
45
+ #
46
+ # This is your application object you want users to interact with, like a
47
+ # question, support ticket, or order.
48
+ #
49
+ class Respondable
50
+ belongs_to :user
51
+
52
+ def body
53
+ 'SMS Response'
54
+ end
55
+
56
+ def response_handler
57
+ RespondableResponseHandler.new(self)
58
+ end
59
+
60
+ def status
61
+ :provided_address
62
+ end
63
+
64
+ def stop_order!
65
+ end
66
+
67
+ def delivery_method=(val)
68
+ super(val)
69
+ end
70
+ end
71
+
72
+ #
73
+ # This is your object-specific handler. It can use the data available in
74
+ # respondable to power it. For example, Respondable can be used as a state
75
+ # machine to power a multi-step wizard.
76
+ # The design inspiration for this gem was an SMS-only reward checkout wizard.
77
+ #
78
+ class RespondableResponseHandler < RespondableBot::Handler
79
+ matcher exit: Regexp.new('^ex?i?t?', 'i'),
80
+ x_it: /^[Xx]/
81
+ handle_response do |reply|
82
+ reply.any_of(:quit, :exit, :x_it) do
83
+ stop_order!
84
+ 'Your order has been stopped. You can try again later.'
85
+ end
86
+
87
+ case status
88
+ when :asked_for_delivery_method
89
+ read_delivery_method_prompt
90
+ end
91
+ end
92
+
93
+ # You can use methods to defer large chunks of behavior you don't want inline
94
+ # inside handle_response
95
+ def read_delivery_method_prompt(reply)
96
+ reply
97
+ .is('A') { respondable.delivery_method = :SMS }
98
+ .is('B') { respondable.delivery_method = :email }
99
+ .is('C') { respondable.delivery_method = :mail }
100
+ .is('D') { respondable.delivery_method = :local_pickup }
101
+ end
102
+ end
103
+
104
+ #
105
+ # Now that the classes are defined, let's look at how to use them.
106
+ #
107
+
108
+ # 1. Find an object to respond to. This usually works well by searching using
109
+ # the phone number of the incoming SMS
110
+ respondable = Respondable.find_by(phone_number: params[:phone_number])
111
+
112
+ # 2. Pass the respondable to the main controller
113
+ controller = ResponseController.new(respondable)
114
+
115
+ # 3. Use the controller to do the cool things your app does and generate a reply
116
+ reply = controller.handle_response!
117
+
118
+ # 4. Send the reply back to your user, perhaps using TwiML if you use Twilio
119
+ # (Shout out to Twilio. I love you. Please send me Twilio swag and credits)
120
+ render xml: Twilio::TwiML::Response.new do |t|
121
+ t.Sms reply
122
+ end.text
@@ -0,0 +1,22 @@
1
+ require 'responder_bot/version'
2
+
3
+ module ResponderBot
4
+ def self.default_matchers
5
+ {
6
+ quit: Regexp.new('^qu?i?t?', 'i'),
7
+ exit: Regexp.new('^ex?i?t?', 'i'),
8
+ yes: Regexp.new('^y', 'i'),
9
+ no: Regexp.new('^n', 'i'),
10
+ number: /^[0-9]+/,
11
+
12
+ # The likert scale is a psychometric scale often used in surveys /
13
+ # questionnaires and has a lot of research backing it's effectiveness.
14
+ # https://en.wikipedia.org/wiki/Likert_scale
15
+ likert: ->(text) { (1..5).cover?(text.strip[0].to_i) }
16
+ }
17
+ end
18
+ end
19
+
20
+ require 'responder_bot/errors'
21
+ require 'responder_bot/matcher_list'
22
+ require 'responder_bot/handler'
@@ -0,0 +1,4 @@
1
+ module ResponderBot
2
+ class UnmatchedResponse < StandardError
3
+ end
4
+ end
@@ -0,0 +1,65 @@
1
+ module ResponderBot
2
+ class Handler
3
+ def self.inherited(child)
4
+ child.class_eval do
5
+ class << self
6
+ attr_accessor :matcher_list, :response_handler
7
+ end
8
+ self.matcher_list = MatcherList.new
9
+ end
10
+ end
11
+
12
+ attr_accessor :respondable, :body
13
+
14
+ def initialize(respondable)
15
+ self.respondable = respondable
16
+ self.body = if respondable.is_a?(String)
17
+ respondable
18
+ else
19
+ respondable.body
20
+ end
21
+ instance_eval(&self.class.response_handler)
22
+ end
23
+
24
+ def understands_reply?
25
+ matcher_list.matches?(body)
26
+ end
27
+
28
+ def handle_response!
29
+ if understands_reply?
30
+ matcher_list.action_for(body).call
31
+ else
32
+ raise ResponderBot::UnmatchedResponse,
33
+ "No matcher caught input '#{body}'."
34
+ end
35
+ end
36
+
37
+ def matcher_list
38
+ self.class.matcher_list
39
+ end
40
+
41
+ def self.handle_response(&block)
42
+ self.response_handler = block
43
+ end
44
+
45
+ def self.matcher(new_matchers)
46
+ new_matchers.each_pair do |name, match|
47
+ matcher_list.define_matcher(name, match)
48
+ end
49
+ end
50
+
51
+ def method_missing(method_name, *args, &block)
52
+ if self.class.matcher_list.respond_to?(method_name)
53
+ self.class.matcher_list.send(method_name, *args, &block)
54
+ elsif respondable.respond_to?(method_name)
55
+ respondable.send(method_name, *args, &block)
56
+ else
57
+ super
58
+ end
59
+ end
60
+
61
+ def respond_to_missing?(method_name)
62
+ self.class.matcher_list.respond_to?(method_name)
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,97 @@
1
+ module ResponderBot
2
+ #
3
+ # Matcher List
4
+ #
5
+ # Contains the list of available named matchers for a given handler as well
6
+ # as the map from matchers to actions.
7
+ #
8
+ # Note: Not all matchers need a name. match, is, exactly, and any all create
9
+ # anonymous matchers.
10
+ #
11
+ class MatcherList
12
+ attr_accessor :actions, :matchers
13
+
14
+ def initialize(use_defaults = true)
15
+ self.actions = {}
16
+ self.matchers = if use_defaults
17
+ ResponderBot.default_matchers
18
+ else
19
+ false
20
+ end
21
+ end
22
+
23
+ def matches?(text)
24
+ !action_for(text).nil?
25
+ end
26
+
27
+ # Use any of the named matchers to call this action
28
+ def any_of(*matcher_names, &action)
29
+ matcher_names.each do |name|
30
+ append_action(matchers[name], action)
31
+ end
32
+ self
33
+ end
34
+
35
+ # Provide a regex to use one time
36
+ def match(regex, &action)
37
+ append_action(regex, action)
38
+ self
39
+ end
40
+
41
+ # Case-insensitive comparison
42
+ def is(string, &action)
43
+ append_action(Regexp.new(string, 'i'), action)
44
+ self
45
+ end
46
+
47
+ # Case-sensitive comparison
48
+ def exactly(string, &action)
49
+ append_action(string, action)
50
+ self
51
+ end
52
+
53
+ # Always matches
54
+ def any(&action)
55
+ append_action(true, action)
56
+ end
57
+
58
+ def define_matcher(matcher_name, matcher)
59
+ matchers[matcher_name] = matcher
60
+ end
61
+
62
+ def method_missing(method_name, *args, &block)
63
+ if matchers.key?(method_name)
64
+ append_action(matchers[method_name], block)
65
+ else
66
+ super
67
+ end
68
+ end
69
+
70
+ def respond_to_missing?(method_name, *)
71
+ matchers.key?(method_name)
72
+ end
73
+
74
+ private
75
+
76
+ def append_action(matcher, action)
77
+ actions[matcher] = action
78
+ end
79
+
80
+ def action_for(text)
81
+ actions.each_pair do |matcher, action|
82
+ return action if matcher_matches?(matcher, text)
83
+ end
84
+ nil
85
+ end
86
+
87
+ def matcher_matches?(matcher, text)
88
+ case matcher
89
+ when TrueClass then true
90
+ when Regexp then matcher.match?(text)
91
+ when Proc
92
+ matcher.arity.zero? ? matcher.call : matcher.call(text)
93
+ when String then matcher == text
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,3 @@
1
+ module ResponderBot
2
+ VERSION = '0.0.1'.freeze
3
+ end
@@ -0,0 +1,27 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'responder_bot/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'responder_bot'
7
+ spec.version = ResponderBot::VERSION
8
+ spec.authors = ['Jacob Evan Shreve']
9
+ spec.email = ['github@shreve.io']
10
+
11
+ spec.summary = 'A simple framework for defining a text-based interface'
12
+ spec.description = 'Use matchers to define a series of commands available' \
13
+ ' to an SMS or other chat interface so your users can ' \
14
+ 'interact with just text.'
15
+ spec.homepage = 'https://github.com/shreve/responder_bot'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.16'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: responder_bot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jacob Evan Shreve
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-04-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Use matchers to define a series of commands available to an SMS or other
42
+ chat interface so your users can interact with just text.
43
+ email:
44
+ - github@shreve.io
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - Gemfile
51
+ - Gemfile.lock
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - bin/console
56
+ - bin/setup
57
+ - examples/routed_system.rb
58
+ - lib/responder_bot.rb
59
+ - lib/responder_bot/errors.rb
60
+ - lib/responder_bot/handler.rb
61
+ - lib/responder_bot/matcher_list.rb
62
+ - lib/responder_bot/version.rb
63
+ - responder_bot.gemspec
64
+ homepage: https://github.com/shreve/responder_bot
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.7.6
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: A simple framework for defining a text-based interface
88
+ test_files: []