From cafe7253544d230f0b6e5102c73d7d4b1de777d6 Mon Sep 17 00:00:00 2001 From: Bruce Lin Date: Sat, 25 Mar 2017 19:17:21 -0400 Subject: [PATCH 1/8] added twilio messenger module, also tweaked the application config to autoload the module --- config/application.rb | 2 ++ lib/messenger.rb | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 lib/messenger.rb diff --git a/config/application.rb b/config/application.rb index 85cabc84..9eddf709 100644 --- a/config/application.rb +++ b/config/application.rb @@ -24,6 +24,8 @@ class Application < Rails::Application # Do not swallow errors in after_commit/after_rollback callbacks. config.active_record.raise_in_transactional_callbacks = true + config.autoload_paths += %W(#{config.root}/lib) + WillPaginate::ViewHelpers.pagination_options[:inner_window] = 1 WillPaginate::ViewHelpers.pagination_options[:outer_window] = 0 end diff --git a/lib/messenger.rb b/lib/messenger.rb new file mode 100644 index 00000000..7d602b88 --- /dev/null +++ b/lib/messenger.rb @@ -0,0 +1,22 @@ +module Messenger + + def send_sms(number, content) + sid = ENV["TWILIO_ACCT_SID"] + auth = ENV["TWILIO_AUTH"] + + @client = Twilio::REST::Client.new sid, auth + + from = "+14123854063" + + message = @client.account.messages.create( + :from => from, + :to => '+1'+number, + :body => content + ) + end + + def receive_sms + + end + +end \ No newline at end of file From edf81c3d3d1226cf147d961733a8fc1b31dbefe3 Mon Sep 17 00:00:00 2001 From: Bruce Lin Date: Sat, 25 Mar 2017 22:07:56 -0400 Subject: [PATCH 2/8] tried to modified messenger with text processing but failed --- lib/messenger.rb | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/messenger.rb b/lib/messenger.rb index 7d602b88..3f313c0d 100644 --- a/lib/messenger.rb +++ b/lib/messenger.rb @@ -15,8 +15,19 @@ def send_sms(number, content) ) end - def receive_sms + # def receive_sms + # body = params["Body"] + # puts "\n\n\nbody text right here boy: #{body}\n\n\n" + # Twilio::TwiML::Response.new do |r| + # if body == "renew" + # return 1 + # elsif body == "cancel" + # return 0 + # else + # return -1 + # end + # end + # end - end end \ No newline at end of file From 27b44a88692a04e4dcbae3ffd7d91811acb6953a Mon Sep 17 00:00:00 2001 From: Bruce Lin Date: Mon, 27 Mar 2017 17:03:25 -0400 Subject: [PATCH 3/8] added code to send out text alert to the next person on the wait list after a tool is checked back in --- app/models/checkout.rb | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/app/models/checkout.rb b/app/models/checkout.rb index d3593823..c2373fa3 100644 --- a/app/models/checkout.rb +++ b/app/models/checkout.rb @@ -22,6 +22,8 @@ # class Checkout < ActiveRecord::Base + include Messenger + # For lookups def card_number=( card_number ) @card_number = card_number @@ -35,6 +37,7 @@ def card_number validates_associated :tool, :organization, :participant before_save :checked_out_at, :presence => true + after_update :notify belongs_to :participant, :touch => true belongs_to :organization, :touch => true @@ -44,4 +47,20 @@ def card_number scope :old, -> { where('checked_in_at IS NOT NULL') } scope :current, -> { where('checked_in_at IS NULL') } -end + private + + def notify + if (self.checked_in_at != nil) + toolCategory = self.tool.tool_type + waitlist = ToolWaitlist.for_tool_type(toolCategory.id).by_wait_start_time + if (waitlist.count != 0) + nextPerson = waitlist.first.participant + number = nextPerson.phone_number + puts ("\n\n\nThis guy is: #{nextPerson.name}") + puts ("\n\n\nThis guy's number is: #{number}\n\n\n") + content = "#{toolCategory.name} is now available at the trailer. Please come pick it up within 5 minutes!" + send_sms(number, content) + end + end + end +end \ No newline at end of file From 1edbd5a5c748cfd2ec36e33338b50630cdd1081f Mon Sep 17 00:00:00 2001 From: Bruce Lin Date: Sun, 2 Apr 2017 17:19:17 -0400 Subject: [PATCH 4/8] added a safeguard for phone number in case someone has not put their phonenumber in the system yet --- app/models/checkout.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/app/models/checkout.rb b/app/models/checkout.rb index c2373fa3..a2a8b12e 100644 --- a/app/models/checkout.rb +++ b/app/models/checkout.rb @@ -55,11 +55,13 @@ def notify waitlist = ToolWaitlist.for_tool_type(toolCategory.id).by_wait_start_time if (waitlist.count != 0) nextPerson = waitlist.first.participant - number = nextPerson.phone_number - puts ("\n\n\nThis guy is: #{nextPerson.name}") - puts ("\n\n\nThis guy's number is: #{number}\n\n\n") - content = "#{toolCategory.name} is now available at the trailer. Please come pick it up within 5 minutes!" - send_sms(number, content) + unless (nextPerson.phone_number.blank?) + number = nextPerson.phone_number + puts ("\n\n\nThis guy is: #{nextPerson.name}") + puts ("\n\n\nThis guy's number is: #{number}\n\n\n") + content = "#{toolCategory.name} is now available at the trailer. Please come pick it up within 5 minutes!" + send_sms(number, content) + end end end end From 5cd7d5e23af50fba63c712ae971f8a14ec15f0b0 Mon Sep 17 00:00:00 2001 From: Bruce Lin Date: Sun, 2 Apr 2017 20:17:34 -0400 Subject: [PATCH 5/8] updated messenger module with try-rescue block --- lib/messenger.rb | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/lib/messenger.rb b/lib/messenger.rb index 3f313c0d..bf9c9113 100644 --- a/lib/messenger.rb +++ b/lib/messenger.rb @@ -8,26 +8,23 @@ def send_sms(number, content) from = "+14123854063" - message = @client.account.messages.create( - :from => from, - :to => '+1'+number, - :body => content - ) - end + # The following try-rescue block is needed in case user unsubscribe + # if the user ubsubscribe and we attempt to message them + # the api will report an error - # def receive_sms - # body = params["Body"] - # puts "\n\n\nbody text right here boy: #{body}\n\n\n" - # Twilio::TwiML::Response.new do |r| - # if body == "renew" - # return 1 - # elsif body == "cancel" - # return 0 - # else - # return -1 - # end - # end - # end + begin + message = @client.account.messages.create( + :from => from, + :to => '+1'+number, + :body => content + ) + rescue Twilio::REST::RequestError => e + case e.code + when 21610 + puts e.message + end + end + end end \ No newline at end of file From d11daf3b0ab4daba3fd5f097f71edbd80c48d99a Mon Sep 17 00:00:00 2001 From: Bruce Lin Date: Sun, 2 Apr 2017 20:20:19 -0400 Subject: [PATCH 6/8] commented out code for reply forwarding. There is an issue for the forwarding and it seems to be security related. Need to discuss it with people more knowledgable before implemeting this feature --- app/controllers/checkouts_controller.rb | 8 ++++++-- app/models/checkout.rb | 27 +++++++++++++++++++++++-- config/routes.rb | 6 ++++++ 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/app/controllers/checkouts_controller.rb b/app/controllers/checkouts_controller.rb index ae7688e5..7d450ff2 100644 --- a/app/controllers/checkouts_controller.rb +++ b/app/controllers/checkouts_controller.rb @@ -136,8 +136,6 @@ def uncheckin format.html { redirect_to tool_path(checkout.tool), notice: "Error" } end end - - end @@ -181,5 +179,11 @@ def checkin_bak end end end + + # def reply + # message_body = params['Body'] + # @checkout = Checkout.find(params[:id]) + # @checkout.reply_to(message_body) + # end end diff --git a/app/models/checkout.rb b/app/models/checkout.rb index a2a8b12e..da3b3927 100644 --- a/app/models/checkout.rb +++ b/app/models/checkout.rb @@ -33,6 +33,10 @@ def card_number @card_number end + # def reply_to(message_content) + # process_text(message_content) + # end + validates_presence_of :tool, :organization validates_associated :tool, :organization, :participant @@ -57,12 +61,31 @@ def notify nextPerson = waitlist.first.participant unless (nextPerson.phone_number.blank?) number = nextPerson.phone_number - puts ("\n\n\nThis guy is: #{nextPerson.name}") - puts ("\n\n\nThis guy's number is: #{number}\n\n\n") content = "#{toolCategory.name} is now available at the trailer. Please come pick it up within 5 minutes!" send_sms(number, content) end end end end + + # def process_text(message_content) + # mc = message_content.downcase + # if (!self.checked_in_at.blank?) + # if (mc.match("cancel")) + # toolCategory = self.tool.tool_type + # waitlist = ToolWaitlist.for_tool_type(toolCategory.id).by_wait_start_time + # nextPerson = waitlist.first.participant + # first_wait = waitlist.first + # first_wait.destroy + # unless (nextPerson.phone_number.blank?) + # number = nextPerson.phone_number + # content = "You have been removed from the waitlist for #{toolCategory.name}! Happy building!" + # send_sms(number, content) + # puts("I sent out my text boy!!!! \n\n\n") + # end + # else + # puts("Don't send non-keywords back!") + # end + # end + # end end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index aff9b4c1..bb5c62c3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -63,6 +63,12 @@ post 'uncheckin', on: :member end + # resource :checkouts do + # collection do + # post 'reply' + # end + # end + get "store" => "store/items#index" namespace :store do resources 'items' do From 06d5cabee24de36f0188c21dd1292b6cd949b3ba Mon Sep 17 00:00:00 2001 From: Bruce Lin Date: Mon, 3 Apr 2017 21:10:33 -0400 Subject: [PATCH 7/8] removed commented code. Will probably add it to a seperate branch. Just trying to merge this code to goat branch --- app/controllers/checkouts_controller.rb | 5 ----- app/models/checkout.rb | 24 ------------------------ config/routes.rb | 6 ------ lib/messenger.rb | 1 - 4 files changed, 36 deletions(-) diff --git a/app/controllers/checkouts_controller.rb b/app/controllers/checkouts_controller.rb index 7d450ff2..6ee4deb7 100644 --- a/app/controllers/checkouts_controller.rb +++ b/app/controllers/checkouts_controller.rb @@ -180,10 +180,5 @@ def checkin_bak end end - # def reply - # message_body = params['Body'] - # @checkout = Checkout.find(params[:id]) - # @checkout.reply_to(message_body) - # end end diff --git a/app/models/checkout.rb b/app/models/checkout.rb index da3b3927..96c2b6f1 100644 --- a/app/models/checkout.rb +++ b/app/models/checkout.rb @@ -33,10 +33,6 @@ def card_number @card_number end - # def reply_to(message_content) - # process_text(message_content) - # end - validates_presence_of :tool, :organization validates_associated :tool, :organization, :participant @@ -68,24 +64,4 @@ def notify end end - # def process_text(message_content) - # mc = message_content.downcase - # if (!self.checked_in_at.blank?) - # if (mc.match("cancel")) - # toolCategory = self.tool.tool_type - # waitlist = ToolWaitlist.for_tool_type(toolCategory.id).by_wait_start_time - # nextPerson = waitlist.first.participant - # first_wait = waitlist.first - # first_wait.destroy - # unless (nextPerson.phone_number.blank?) - # number = nextPerson.phone_number - # content = "You have been removed from the waitlist for #{toolCategory.name}! Happy building!" - # send_sms(number, content) - # puts("I sent out my text boy!!!! \n\n\n") - # end - # else - # puts("Don't send non-keywords back!") - # end - # end - # end end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index bb5c62c3..aff9b4c1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -63,12 +63,6 @@ post 'uncheckin', on: :member end - # resource :checkouts do - # collection do - # post 'reply' - # end - # end - get "store" => "store/items#index" namespace :store do resources 'items' do diff --git a/lib/messenger.rb b/lib/messenger.rb index bf9c9113..004ec7c1 100644 --- a/lib/messenger.rb +++ b/lib/messenger.rb @@ -24,7 +24,6 @@ def send_sms(number, content) puts e.message end end - end end \ No newline at end of file From f1915633c0d196a111b7e4777fcddcf2e26a68e1 Mon Sep 17 00:00:00 2001 From: Bruce Lin Date: Sun, 9 Apr 2017 18:32:43 -0400 Subject: [PATCH 8/8] modified messenger to keep track of error code --- lib/messenger.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/messenger.rb b/lib/messenger.rb index 004ec7c1..0c293334 100644 --- a/lib/messenger.rb +++ b/lib/messenger.rb @@ -1,3 +1,5 @@ +USER_UNSUBSCRIBED_FROM_TWILIO_ERROR_CODE = 21610 + module Messenger def send_sms(number, content) @@ -20,7 +22,7 @@ def send_sms(number, content) ) rescue Twilio::REST::RequestError => e case e.code - when 21610 + when USER_UNSUBSCRIBED_FROM_TWILIO_ERROR_CODE puts e.message end end