wit_bot 0.5.0 → 0.5.1

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: 6665e746f0e43cac8afda18e6597652e9938c3c8
4
- data.tar.gz: 61201b2b210381924ebbafebf873117d32f7fbec
3
+ metadata.gz: d6f8586c33704055bce42268f28aa62d9342368a
4
+ data.tar.gz: c64ca87eb3a43d348f737f63336f76fe4eb95a2b
5
5
  SHA512:
6
- metadata.gz: b6b754b9c04dafd6a263b400f0d6423b0ccb553a0f809aa905c56145c2f463e3529d304949275e94c8058b1793d543b568db1ac6b888e05ee6291d80ec034565
7
- data.tar.gz: 984e8ba0c67d29c9365bb9c794a6085f2cd142058ce1190bfd619b9544475f5a42899bf10b11d155e2f3b871df5ceaedbe261290e7301cbe9c85a0f638c84c0f
6
+ metadata.gz: 742baac6a12728fd5bbcd6968889602f2d8e805e652c1e72afd1edb02074dc796c27839c8f86ad0e51cdb52d2cd4ae0988bd8cda68f4bbfb4f5a70beef4f4a67
7
+ data.tar.gz: 94c26dca334458d1350e8e20ddc35952f7f0b79c4be87fe92fb8e129b2927eb0bcfb5f691208bbf47b855d8cde74b10765a182cd8eccad6998d69d24429b3dad
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Sets up an initializer for wit_bot.
3
+
4
+ Example:
5
+ rails generate wit_bot:setup
6
+
7
+ This will create:
8
+ config/initializers/wit_bot.rb
@@ -0,0 +1,35 @@
1
+ module WitBot
2
+ module Generators
3
+ class MessageBusGenerator < Rails::Generators::Base
4
+ source_root File.expand_path('../templates', __FILE__)
5
+ argument :bot_name, type: :string, required: true
6
+ argument :user_model, type: :string, default: 'User'
7
+ argument :bot_channel, type: :string, default: '/witbot'
8
+
9
+ def install_dependencies
10
+ gem 'redis', '~> 3'
11
+ gem 'message_bus', '~> 2.0.0.beta.5'
12
+ Bundler.with_clean_env do
13
+ run 'bundle install'
14
+ end
15
+ end
16
+
17
+ def create_message_bus_listener
18
+ template 'message_bus_listener.rb.erb', 'app/wit_bot/message_bus_listener.rb'
19
+ end
20
+
21
+ def create_message_bus_controller
22
+ template 'message_bus_controller.rb.erb', 'app/controllers/message_bus_controller.rb'
23
+ end
24
+
25
+ def create_job
26
+ template 'job.rb.erb', 'app/jobs/message_bus_job.rb'
27
+ end
28
+
29
+ private
30
+ def user_lower
31
+ user_model.underscore
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,13 @@
1
+ class MessageBusJob < ActiveJob::Base
2
+ queue_as :high
3
+
4
+ def perform(<%= user_lower %>_id, text)
5
+ # Make sure the user still exists
6
+ if <%= "(#{user_lower} = #{user_model}.find_by_id #{user_lower}_id)" %>
7
+ # Get the listener
8
+ listener = MessageBusListener.from_<%= user_lower %> <%= user_lower %>
9
+ # And send the message
10
+ listener.send_message text
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ class MessageBusController < ApplicationController
2
+ <% if defined? Doorkeeper %>
3
+ before_action(only: [:send_message]){ doorkeeper_authorize! }
4
+ <% else %>
5
+ before_action :authorize!, only: [:send_message] # Replace with your authentication logic
6
+ <% end %>
7
+
8
+ def send_message
9
+ MessageBusJob.perform_later current_<%= user_model.underscore %>.id, params[:text] # Enqueue
10
+ head :accepted # Respond the with the HTTP Accepted status code.
11
+ end
12
+ end
@@ -0,0 +1,45 @@
1
+ class MessageBusListener < WitBot::Bot::Conversation::Participant
2
+ attr_accessor :conversation_model, <%= ":#{user_lower}" %>
3
+
4
+ # Initialize the conversation, set the local <%= user_lower %>, and add the bot to the message
5
+ def initialize(conversation, <%= user_lower %>)
6
+ # Send the conversation object to the parent class
7
+ super conversation.object
8
+
9
+ # Set instance variables (@conversation is the actual conversation object)
10
+ <%= "@#{user_lower} = #{user_lower}" %>
11
+ @conversation_model = conversation
12
+
13
+ # Add the bot to the conversation
14
+ <%= "#{bot_name}." %>new @conversation<%= ", @#{user_lower}" %>
15
+ end
16
+
17
+ # A method that saves the conversation.
18
+ def save
19
+ @conversation_model.object = @conversation
20
+ @conversation_model.save
21
+ end
22
+
23
+ class << self
24
+ # A method to retrieve a listener from a <%= user_lower %>.
25
+ def from_<%= user_lower %>(<%= user_lower %>)
26
+ unless <%= "#{user_lower}." %>conversation # If the <%= user_lower %> doesn't have a conversation...
27
+ # Create one
28
+ <%= "#{user_lower}." %>conversation = Conversation.new
29
+ <%= user_lower %>.save! # Save the <%= user_lower %>
30
+ end
31
+ MessageBusListener.new <%= "#{user_lower}." %>conversation, <%= "#{user_lower}." %>to_s # Create a new listener from the conversation and the <%= user_lower %>.
32
+ end
33
+ end
34
+
35
+ def bot?
36
+ false # Tell wit_bot that this class handles messages for a user.
37
+ end
38
+
39
+ # A method that gets called on output of a message automatically by wit_bot
40
+ def on_output(message)
41
+ save # Once we output a message, save the <%= user_lower %>.
42
+ msg_hash = {text: message.text, bot: message.bot?} # Create a hash to send to the <%= user_lower %>
43
+ MessageBus.publish '<%= bot_channel %>', msg_hash, user_ids: [<%= "@#{user_lower}." %>id] # Send the message to the <%= user_lower %>.
44
+ end
45
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Sets up an initializer for wit_bot.
3
+
4
+ Example:
5
+ rails generate wit_bot:setup
6
+
7
+ This will create:
8
+ config/initializers/wit_bot.rb
@@ -0,0 +1,30 @@
1
+ module WitBot
2
+ module Generators
3
+ class ModelGenerator < Rails::Generators::Base
4
+ source_root File.expand_path('../templates', __FILE__)
5
+ argument :user_model, type: :string, default: 'User'
6
+ hook_for :force, type: :boolean
7
+
8
+ def create_conversations
9
+ user_lower = user_model.underscore
10
+
11
+ migration_options = 'migration', 'CreateConversations', 'object:text', "#{user_lower}:references"
12
+ migration_options << '--force' if options[:force]
13
+
14
+ generate *migration_options
15
+ template 'model.rb.erb', 'app/models/conversation.rb'
16
+
17
+ user_file = "app/models/#{user_lower}.rb"
18
+ inject_into_class user_file, user_model, " has_one :conversation, dependent: :destroy\n"
19
+
20
+ puts <<TEXT
21
+ If you're thinking that storing conversations will take up a lot of room in your database,
22
+ you are correct. You'll want to destroy inactive conversations from the database every few hours.
23
+ Here's the guide on setting up a job to clear old conversations with Clockwork:
24
+
25
+ https://github.com/penne12/wit_bot/wiki/Clear-Old-Conversations-in-ActiveRecord-with-Clockwork-and-ActiveJob
26
+ TEXT
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,16 @@
1
+ class Conversation < ActiveRecord::Base
2
+ <%= "belongs_to :#{user_model.underscore}" %>
3
+ store :object, coder: JSON
4
+
5
+ def object
6
+ h = super # Get the hash from the parent method
7
+ if h && !h.empty? # If the hash exists and is not empty,
8
+ WitBot::Bot::Conversation::Base.from_hash h # Then create a conversation from it.
9
+ else
10
+ WitBot::Bot::Conversation::Base.new # Otherwise, create a new conversation.
11
+ end
12
+ end
13
+ def object=(conversation)
14
+ super conversation.as_json # Turn the conversation back into a hash.
15
+ end
16
+ end
@@ -1,3 +1,3 @@
1
1
  module WitBot
2
- VERSION = '0.5.0'
2
+ VERSION = '0.5.1'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wit_bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben (@penne12_)
@@ -151,6 +151,14 @@ files:
151
151
  - lib/generators/wit_bot/bot/USAGE
152
152
  - lib/generators/wit_bot/bot/bot_generator.rb
153
153
  - lib/generators/wit_bot/bot/templates/bot.rb.erb
154
+ - lib/generators/wit_bot/message_bus/USAGE
155
+ - lib/generators/wit_bot/message_bus/message_bus_generator.rb
156
+ - lib/generators/wit_bot/message_bus/templates/job.rb.erb
157
+ - lib/generators/wit_bot/message_bus/templates/message_bus_controller.rb.erb
158
+ - lib/generators/wit_bot/message_bus/templates/message_bus_listener.rb.erb
159
+ - lib/generators/wit_bot/model/USAGE
160
+ - lib/generators/wit_bot/model/model_generator.rb
161
+ - lib/generators/wit_bot/model/templates/model.rb.erb
154
162
  - lib/generators/wit_bot/setup/USAGE
155
163
  - lib/generators/wit_bot/setup/setup_generator.rb
156
164
  - lib/generators/wit_bot/setup/templates/initializer.rb