stealth 1.1.6 → 2.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (103) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +15 -54
  3. data/CHANGELOG.md +72 -0
  4. data/Gemfile +1 -1
  5. data/Gemfile.lock +49 -44
  6. data/LICENSE +4 -17
  7. data/README.md +9 -17
  8. data/VERSION +1 -1
  9. data/lib/stealth/base.rb +62 -15
  10. data/lib/stealth/cli.rb +1 -2
  11. data/lib/stealth/commands/console.rb +1 -1
  12. data/lib/stealth/configuration.rb +0 -3
  13. data/lib/stealth/controller/callbacks.rb +1 -1
  14. data/lib/stealth/controller/catch_all.rb +27 -4
  15. data/lib/stealth/controller/controller.rb +168 -49
  16. data/lib/stealth/controller/dev_jumps.rb +41 -0
  17. data/lib/stealth/controller/dynamic_delay.rb +4 -6
  18. data/lib/stealth/controller/interrupt_detect.rb +100 -0
  19. data/lib/stealth/controller/messages.rb +283 -0
  20. data/lib/stealth/controller/nlp.rb +50 -0
  21. data/lib/stealth/controller/replies.rb +178 -40
  22. data/lib/stealth/controller/unrecognized_message.rb +62 -0
  23. data/lib/stealth/core_ext.rb +5 -0
  24. data/lib/stealth/{flow/core_ext.rb → core_ext/numeric.rb} +0 -1
  25. data/lib/stealth/core_ext/string.rb +18 -0
  26. data/lib/stealth/dispatcher.rb +21 -0
  27. data/lib/stealth/errors.rb +12 -0
  28. data/lib/stealth/flow/base.rb +1 -2
  29. data/lib/stealth/flow/specification.rb +3 -2
  30. data/lib/stealth/flow/state.rb +3 -3
  31. data/lib/stealth/generators/builder/Gemfile +4 -3
  32. data/lib/stealth/generators/builder/bot/controllers/bot_controller.rb +42 -0
  33. data/lib/stealth/generators/builder/bot/controllers/catch_alls_controller.rb +2 -0
  34. data/lib/stealth/generators/builder/bot/controllers/goodbyes_controller.rb +2 -0
  35. data/lib/stealth/generators/builder/bot/controllers/hellos_controller.rb +2 -0
  36. data/lib/stealth/generators/builder/bot/controllers/interrupts_controller.rb +9 -0
  37. data/lib/stealth/generators/builder/bot/controllers/unrecognized_messages_controller.rb +9 -0
  38. data/lib/stealth/generators/builder/config/flow_map.rb +8 -0
  39. data/lib/stealth/generators/builder/config/initializers/autoload.rb +8 -0
  40. data/lib/stealth/generators/builder/config/initializers/inflections.rb +16 -0
  41. data/lib/stealth/generators/builder/config/puma.rb +15 -0
  42. data/lib/stealth/helpers/redis.rb +40 -0
  43. data/lib/stealth/lock.rb +83 -0
  44. data/lib/stealth/logger.rb +27 -18
  45. data/lib/stealth/nlp/client.rb +22 -0
  46. data/lib/stealth/nlp/result.rb +57 -0
  47. data/lib/stealth/reloader.rb +90 -0
  48. data/lib/stealth/reply.rb +17 -0
  49. data/lib/stealth/scheduled_reply.rb +3 -3
  50. data/lib/stealth/server.rb +3 -3
  51. data/lib/stealth/service_message.rb +3 -2
  52. data/lib/stealth/service_reply.rb +5 -1
  53. data/lib/stealth/services/base_reply_handler.rb +2 -2
  54. data/lib/stealth/session.rb +106 -53
  55. data/spec/configuration_spec.rb +9 -2
  56. data/spec/controller/callbacks_spec.rb +23 -28
  57. data/spec/controller/catch_all_spec.rb +81 -29
  58. data/spec/controller/controller_spec.rb +444 -43
  59. data/spec/controller/dynamic_delay_spec.rb +16 -18
  60. data/spec/controller/helpers_spec.rb +1 -2
  61. data/spec/controller/interrupt_detect_spec.rb +171 -0
  62. data/spec/controller/messages_spec.rb +744 -0
  63. data/spec/controller/nlp_spec.rb +93 -0
  64. data/spec/controller/replies_spec.rb +446 -11
  65. data/spec/controller/unrecognized_message_spec.rb +168 -0
  66. data/spec/dispatcher_spec.rb +79 -0
  67. data/spec/flow/flow_spec.rb +1 -2
  68. data/spec/flow/state_spec.rb +14 -3
  69. data/spec/helpers/redis_spec.rb +77 -0
  70. data/spec/lock_spec.rb +100 -0
  71. data/spec/nlp/client_spec.rb +23 -0
  72. data/spec/nlp/result_spec.rb +57 -0
  73. data/spec/replies/messages/say_msgs_without_breaks.yml +4 -0
  74. data/spec/replies/messages/say_randomize_speech.yml +10 -0
  75. data/spec/replies/messages/say_randomize_text.yml +10 -0
  76. data/spec/replies/messages/sub1/sub2/say_nested.yml +10 -0
  77. data/spec/reply_spec.rb +61 -0
  78. data/spec/scheduled_reply_spec.rb +23 -0
  79. data/spec/service_reply_spec.rb +1 -2
  80. data/spec/session_spec.rb +251 -12
  81. data/spec/spec_helper.rb +21 -0
  82. data/spec/support/controllers/vaders_controller.rb +24 -0
  83. data/spec/support/nlp_clients/dialogflow.rb +9 -0
  84. data/spec/support/nlp_clients/luis.rb +9 -0
  85. data/spec/support/nlp_results/luis_result.rb +163 -0
  86. data/spec/version_spec.rb +1 -2
  87. data/stealth.gemspec +6 -6
  88. metadata +83 -38
  89. data/docs/00-introduction.md +0 -37
  90. data/docs/01-getting-started.md +0 -21
  91. data/docs/02-local-development.md +0 -40
  92. data/docs/03-basics.md +0 -171
  93. data/docs/04-sessions.md +0 -29
  94. data/docs/05-controllers.md +0 -179
  95. data/docs/06-models.md +0 -39
  96. data/docs/07-replies.md +0 -114
  97. data/docs/08-catchalls.md +0 -49
  98. data/docs/09-messaging-integrations.md +0 -80
  99. data/docs/10-nlp-integrations.md +0 -13
  100. data/docs/11-analytics.md +0 -13
  101. data/docs/12-commands.md +0 -62
  102. data/docs/13-deployment.md +0 -50
  103. data/lib/stealth/generators/builder/config/initializers/.keep +0 -0
@@ -1,39 +0,0 @@
1
- ---
2
- title: Models
3
- ---
4
-
5
- Models in Stealth are powered by [ActiveRecord](http://guides.rubyonrails.org/active_record_basics.html). Your bot may not need to persist data, but if it does, ActiveRecord comes built in. We've tried to keep things identical to Ruby on Rails.
6
-
7
- ## ActiveRecord Models
8
-
9
- An ActiveRecord model in Stealth inherits all of the functionality from [ActiveRecord](http://guides.rubyonrails.org/active_record_basics.html). An empty model looks like this in Stealth:
10
-
11
- ```ruby
12
- class Quote < BotRecord
13
-
14
- end
15
- ```
16
-
17
- With the exception of inheriting from `BotRecord` instead of `ApplicationRecord`, everything else matches what is in the [ActiveRecord](http://guides.rubyonrails.org/active_record_basics.html) documentation.
18
-
19
- ## Configuration
20
-
21
- Configuring a database is done via `config/database.yml`. A sample `database.yml` file is included when you generate your bot. It is configured to use SQLite3. For more options please refer to the [Ruby on Rails documentation](http://guides.rubyonrails.org/configuring.html#configuring-a-database).
22
-
23
- ## Migrations
24
-
25
- In order to use your models, you'll need to generate migrations to create your database schema:
26
-
27
- ```
28
- stealth g migration create_users
29
- ```
30
-
31
- This will create a migration named `CreateUsers`. To migrate your database:
32
-
33
- ```
34
- stealth db:migrate
35
- ```
36
-
37
- For more information about migrations, seed data, creating databases, or dropping databases please refer to the [Ruby on Rails documentation](http://guides.rubyonrails.org/active_record_migrations.html).
38
-
39
- Just remember to prefix your commands with `stealth` rather than `rails`.
@@ -1,114 +0,0 @@
1
- ---
2
- title: Replies
3
- ---
4
-
5
- Stealth replies can send one or more replies to a user. The supported reply types will depend on the specific messaging service you're using. Each service integration will detail it's supported reply types in it's respective docs.
6
-
7
- However, here is a generic reply using text, delays, and suggestions.
8
-
9
- ```yml
10
- - reply_type: text
11
- text: "Hello. Welcome to our Bot."
12
- - reply_type: delay
13
- duration: 2
14
- - reply_type: text
15
- text: "We're here to help you learn more about something or another."
16
- - reply_type: delay
17
- duration: 2
18
- - reply_type: text
19
- text: 'By using the "Yes" and "No" buttons below, are you interested in do you want to continue?'
20
- suggestions:
21
- - text: "Yes"
22
- - text: "No"
23
- ```
24
-
25
- ## Reply Variants
26
-
27
- By default, Stealth will look for your replies in the folder corresponding to your controller name. So, for example, if you have a `MessagesController`, Stealth will look for replies in `bot/replies/messages`.
28
-
29
- If you have an action named `say_hello`, it will look for a reply file named `bot/replies/messages/say_hello.yml.erb` first, and then if that is not found, it will look for `bot/replies/messages/say_hello.yml`. If neither of these files are found, Stealth will raise a `Stealth::Errors::ReplyNotFound`.
30
-
31
- In addition to these two naming conventions, Stealth 1.1+ supports Reply Variants. By adding the name of the service to your reply filename, Stealth will reply to users from that service using the designated reply file. That's a mouthful. Let's try an example.
32
-
33
- For example, if the bot is replying to a message via an action called `hello`:
34
-
35
- Facebook users would receive the reply in `hello.yml+facebook.erb`.
36
- Twilio SMS users would receive the reply in `hello.yml+twilio.erb`.
37
- Every other service would receive the reply in `hello.yml.erb`.
38
-
39
- This allows you to take advantage of things like Facebook Messenger Cards while still maintaining compatibility for users using SMS.
40
-
41
- ## Format
42
-
43
- Stealth reply templates are written in YAML. Stealth doesn't use advanced YAML features, but we do recommend you familiarize yourself with the syntax. In the above reply example, you should be able to see there are 5 replies included in the reply file.
44
-
45
- **Caveat:** YAML interprets "yes", "no", "true", "false", "y", "n", etc (without quotes) as boolean values. So make sure you wrap them in quotes as we did above.
46
-
47
- ## ERB
48
-
49
- Reply templates currently support ERB:
50
-
51
- ```erb
52
- - reply_type: text
53
- text: "Hello, <%= current_user.first_name %>. Welcome to our Bot."
54
- - reply_type: delay
55
- duration: 2
56
- - reply_type: text
57
- text: "We're here to help you learn more about something or another."
58
- - reply_type: delay
59
- duration: 2
60
- <% if current_user.valid? %>
61
- - reply_type: text
62
- text: 'By using the "Yes" and "No" buttons below, are you interested in do you want to continue?'
63
- suggestions:
64
- - text: "Yes"
65
- - text: "No"
66
- <% end %>
67
- ```
68
-
69
- With ERB in your reply templates, you can access controller instance variables and helper methods in your replies.
70
-
71
- ## Delays
72
-
73
- Delays are a common pattern of chatbot design. After a block of text, it's recommended to pause for a bit to give the user a chance to read the message. The duration of the delay depends on the length of the message sent.
74
-
75
- Stealth will pause for the duration specified. For service integrations that support it (like Facebook), Stealth will send a typing indicator while it is paused.
76
-
77
- ### Dynamic Delays
78
-
79
- Rather than specifying an explicit delay duration, you can optionally choose to specify a dynamic duration:
80
-
81
- ```yaml
82
- - reply_type: delay
83
- duration: dynamic
84
- ```
85
-
86
- The dynamic delay uses a heuristic to dynamically determine the length of the delay. The previous message sent to the user is examined and depending on it's type and text length (in the case of text replies), an optimal duration is computed.
87
-
88
- If you find that the dynamic delays are too fast for your taste, you can slow them down by setting the multiplier value to something between 0 and 1:
89
-
90
- ```ruby
91
- Stealth.config.dynamic_delay_muliplier = 0.5
92
- ```
93
-
94
- If you find them to be too slow, you can speed them up by setting the multipler to a value greater than 1:
95
-
96
- ```ruby
97
- Stealth.config.dynamic_delay_muliplier = 2.5
98
- ```
99
-
100
- You can set this option by setting the above value in an intializer file, i.e., `config/dynamic_delay_config.rb`.
101
-
102
- ## Naming Conventions
103
-
104
- Replies are named after a flow's state (which is also the controller's action). So for a given controller:
105
-
106
- ```ruby
107
- class QuotesController < BotController
108
- def say_price
109
-
110
- end
111
- end
112
- ```
113
-
114
- You would need to place your reply template in `replies/quotes/say_price.yml`. If your template contains ERB, you must add the `.erb` suffix to the template filename: `replies/quotes/say_price.yml.erb`.
@@ -1,49 +0,0 @@
1
- ---
2
- title: CatchAll
3
- ---
4
-
5
- Stealth CatchAlls are designed to handle a very common scenario within chatbots. What happens when the user says something the bot doesn't understand? The majority of bots will simply respond back with a generic "I don't understand" and hope the user to figures out the next step. While this experience might be ok for some bots, we built a more robust way of handling these experiences right into Stealth. The better your CatchAlls, the better your bot.
6
-
7
- ## Triggering
8
-
9
- A CatchAll flow is automatically triggered when a controller action fails to do **at least one** of the following:
10
-
11
- 1. Update a session (via `step_to`, `update_session_to`, or any other of the step methods)
12
- 2. Send a reply via `send_replies`
13
-
14
- In addition to the above two conditions, if your controller action raises an Exception, the CatchAll flow will automatically be triggered.
15
-
16
- ## Multi-Level
17
-
18
- Stealth keeps track of how many times a CatchAll is triggered for a given session. This allows you to build experiences in which the user is provided different responses for subsequent failures. Once a session progresses past a failing state, the CatchAll counter resets.
19
-
20
- ## Retrying
21
-
22
- By default, a Stealth bot comes with the first level CatchAll already defined. Here is the `CatchAllsController` action and associated reply:
23
-
24
- ```ruby
25
- def level1
26
- send_replies
27
-
28
- if previous_session_specifies_fails_to?
29
- step_to flow: previous_session.flow_string, state: previous_state.to_s
30
- else
31
- step_to session: previous_session - 2.states
32
- end
33
- end
34
- ```
35
-
36
- ```yml
37
- - reply_type: text
38
- text: Oops. It looks like something went wrong. Let's try that again
39
- ```
40
-
41
- In the controller action, we check if the `previous_session` (the one that failed) specified a `fails_to` state. If so, we send the user there. Otherwise, we send the user back 2 states.
42
-
43
- Sending a user back two states is a pretty good generic action. Going back 1 state takes us back to the action that failed. Since the actions most likely to fail are `get` actions, or actions that deal with user responses, going back 2 states usually takes us back to the original "question".
44
-
45
- ## Adding More Levels
46
-
47
- If you would like to expand the experience, simply add a `level2` controller action and associated reply (and update the `FlowMap`). You can go as far as you want. CatchAlls have no limit, just make sure you increment using the standardized method names of `level1`, `level2`, `level3`, `level4`, etc.
48
-
49
- If a user has encountered the maximum number of CatchAll levels as you have defined, the user's session will remain at the last CatchAll state.
@@ -1,80 +0,0 @@
1
- ---
2
- title: Messaging Integrations
3
- ---
4
-
5
- Stealth is designed for your bot to support one or more messaging integrations. For example, this could be just SMS or both SMS and Facebook Messenger. Messaging integrations can be attached to your Stealth bot by adding the messaging integration gem to your `Gemfile`.
6
-
7
- ## `Gemfile`
8
-
9
- ```
10
- source 'https://rubygems.org'
11
-
12
- ruby '2.5.1'
13
-
14
- gem 'stealth', '~> 0.10.0'
15
-
16
- # Uncomment to enable the Stealth Facebook Driver
17
- # gem 'stealth-facebook'
18
-
19
- # Uncomment to enable the Stealth Twilio SMS Driver
20
- # gem 'stealth-twilio'
21
- ```
22
-
23
-
24
- ## `services.yml`
25
-
26
- ```yml
27
- default: &default
28
- # ==========================================
29
- # ===== Example Facebook Service Setup =====
30
- # ==========================================
31
- # facebook:
32
- # verify_token: XXXFACEBOOK_VERIFY_TOKENXXX
33
- # page_access_token: XXXFACEBOOK_ACCESS_TOKENXXX
34
- # setup:
35
- # greeting: # Greetings are broken up by locale
36
- # - locale: default
37
- # text: "Welcome to the Stealth bot 🤖"
38
- # persistent_menu:
39
- # - type: payload
40
- # text: Main Menu
41
- # payload: main_menu
42
- # - type: url
43
- # text: Visit our website
44
- # url: https://example.com
45
- # - type: call
46
- # text: Call us
47
- # payload: "+4155330000"
48
- #
49
- # ===========================================
50
- # ======== Example SMS Service Setup ========
51
- # ===========================================
52
- # twilio:
53
- # account_sid: XXXTWILIO_ACCOUNT_SIDXXX
54
- # auth_token: XXXTWILIO_AUTH_TOKENXXX
55
- # from_phone: +14155330000
56
-
57
- production:
58
- <<: *default
59
- development:
60
- <<: *default
61
- test:
62
- <<: *default
63
-
64
- ```
65
-
66
- ## `stealth setup`
67
-
68
- Most messaging integrations require an initial setup. For example, Facebook requires you to send a payload to define the default greeting and persistent menu. You can accomplish this by running the `stealth setup` followed by the integration. For example:
69
-
70
- `stealth setup facebook`
71
-
72
- Make sure to reference the respective messaging integration documentation for more specifics.
73
-
74
- ## Officially Supported
75
-
76
- * [Facebook Messenger](https://github.com/hellostealth/stealth-facebook)
77
- * [SMS (Twillio)](https://github.com/hellostealth/stealth-twilio)
78
- * [Smooch (many platforms, including a web widget)](https://github.com/hellostealth/stealth-smooch)
79
-
80
- While we plan to add more integrations in the future, please feel free to add your own and let us know so we can keep this list updated. 😎
@@ -1,13 +0,0 @@
1
- ---
2
- title: NLP Integrations
3
- ---
4
-
5
- Stealth can be extended with NLP/NLU integrations. While these are not needed for the majority of interactions within chatbot, you may find they are helpful for certain types of interactions.
6
-
7
- ## Officially Supported
8
-
9
- Stealth currently supports:
10
-
11
- * [AWS Comprehend](https://github.com/hellostealth/stealth-aws-comprehend)
12
-
13
- While we plan to add more integrations in the future, please feel free to add your own and let us know so we can keep this list updated. 😎
@@ -1,13 +0,0 @@
1
- ---
2
- title: Analytics Integrations
3
- ---
4
-
5
- Stealth can be extended with metric/analytic integrations.
6
-
7
- ## Officially Supported
8
-
9
- Stealth currently supports:
10
-
11
- * [Mixpanel](https://github.com/hellostealth/stealth-mixpanel)
12
-
13
- While we plan to add more integrations in the future, please feel free to add your own and let us know so we can keep this list updated. 😎
@@ -1,62 +0,0 @@
1
- ---
2
- title: Commands
3
- ---
4
-
5
- Stealth provides the `stealth` command-line program. It is used to generate new bots, generate flows, start a console, run integration setup tasks, run the server, and more.
6
-
7
- To view details for a command at any time use `stealth help`.
8
-
9
- ```
10
- Usage:
11
-
12
- stealth [<flags>] <command> [<args> ...]
13
-
14
- Flags:
15
-
16
- -h, --help Output usage information.
17
- -C, --chdir="." Change working directory.
18
- -v, --verbose Enable verbose log output.
19
- --format="text" Output formatter.
20
- --version Show application version.
21
-
22
- Commands:
23
- stealth console # Starts a stealth console
24
- stealth db:create # Creates the database from DATABASE_URL or config/database.yml for the current STEALTH_ENV
25
- stealth db:create:all # Creates all databases from DATABASE_URL or config/database.yml
26
- stealth db:drop # Drops the database from DATABASE_URL or config/database.yml for the current STEALTH_ENV
27
- stealth db:drop:all # Drops all databases from DATABASE_URL or config/database.yml
28
- stealth db:environment:set # Set the environment value for the database
29
- stealth db:migrate # Migrate the database
30
- stealth db:rollback # Rolls the schema back to the previous version
31
- stealth db:schema:dump # Creates a db/schema.rb file that is portable against any DB supported by Active Record
32
- stealth db:schema:load # Loads a schema.rb file into the database
33
- stealth db:seed # Seeds the database with data from db/seeds.rb
34
- stealth db:setup # Creates the database, loads the schema, and initializes with the seed data (use db:reset to also drop the database first)
35
- stealth db:structure:dump # Dumps the database structure to db/structure.sql. Specify another file with SCHEMA=db/my_structure.sql
36
- stealth db:structure:load # Recreates the databases from the structure.sql file
37
- stealth db:version # Retrieves the current schema version number
38
- stealth generate # Generates scaffold Stealth files
39
- stealth help [COMMAND] # Describe available commands or one specific command
40
- stealth new # Creates a new Stealth bot
41
- stealth server # Starts a stealth server
42
- stealth sessions:clear # Clears all sessions in development
43
- stealth setup # Runs setup tasks for a specified service
44
- stealth version # Prints stealth version
45
-
46
- Examples:
47
-
48
- Start a new Stealth project.
49
- $ stealth new [BOT NAME]
50
-
51
- Generate a new flow inside your Stealth project.
52
- $ stealth generate flow [FLOW NAME]
53
-
54
- Run setup tasks for a specific driver.
55
- $ stealth setup [INTEGRATION NAME]
56
-
57
- Start a Stealth console.
58
- $ stealth c
59
-
60
- Start the Stealth server.
61
- $ stealth s
62
- ```
@@ -1,50 +0,0 @@
1
- ---
2
- title: Deployment
3
- ---
4
-
5
- Stealth is a rack based application. That means it can be hosted on most platforms as well as taking advantage of existing tools such as Docker.
6
-
7
- ## Deploying on Heroku
8
-
9
- Stealth supports [Heroku](http://herokuapp.com) out of the box. In fact, running a `stealth s` command locally boots `foreman` using a `Procfile.dev` file similar to what Heroku does. Here is a quick guide to get you started.
10
-
11
- If you haven't, make sure to track your bot in Git
12
-
13
- ```
14
- $ git init
15
- Initialized empty Git repository in .git/
16
- $ git add .
17
- $ git commit -m "My first commit"
18
- Created initial commit 5df2d09: My first commit
19
- 42 files changed, 470 insertions(+)
20
- create mode 100644 Gemfile
21
- create mode 100644 Gemfile.lock
22
- create mode 100644 Procfile
23
- ...
24
- ```
25
-
26
- After you have your bot tracked with Git, you're ready to deploy to Heroku. Next, we'll add our bot to Heroku using:
27
-
28
- ```
29
- $ heroku apps:create <BOT NAME>
30
- ```
31
-
32
- You will want a production `Procfile` separate from your development `Procfile.dev`. We recommend adding:
33
-
34
- ```
35
- web: bundle exec puma -C config/puma.rb
36
- sidekiq: bundle exec sidekiq -C config/sidekiq.yml -q webhooks -q default -r ./config/boot.rb
37
- release: bundle exec rake db:migrate
38
- ```
39
-
40
- Then deploy your bot to Heroku.
41
-
42
- ```
43
- $ git push heroku master
44
- ```
45
-
46
- Once deployed:
47
-
48
- 1. Make sure to enable both the `Heroku Postgres` (if you use a database) and `Heroku Redis` addons
49
- 2. Make sure the `web` and `sidekiq` dynos are spun up
50
- 3. Make sure you run any `stealth setup` commands to configure your messaging service