diff --git a/app/models/bot/smooch.rb b/app/models/bot/smooch.rb index 765a8363c..ac9849734 100644 --- a/app/models/bot/smooch.rb +++ b/app/models/bot/smooch.rb @@ -360,11 +360,9 @@ def self.parse_query_message(message, app_id, uid, workflow, language) sm = CheckStateMachine.new(uid) if self.process_menu_option(message, sm.state.value, app_id) # Do nothing else - the action will be executed by "process_menu_option" method - elsif self.is_v2? + else sm.go_to_ask_if_ready unless sm.state.value == 'ask_if_ready' self.ask_if_ready_to_submit(uid, workflow, 'ask_if_ready', language) - else - self.delay_for(self.time_to_send_request, { queue: 'smooch', retry: false }).bundle_messages(message['authorId'], message['_id'], app_id) end end @@ -429,12 +427,12 @@ def self.get_menu_options(state, workflow, uid) { 'smooch_menu_option_keyword' => '1', 'smooch_menu_option_value' => 'search_result_is_relevant' }, { 'smooch_menu_option_keyword' => '2', 'smooch_menu_option_value' => 'search_result_is_not_relevant' } ] - elsif state == 'subscription' && self.is_v2? + elsif state == 'subscription' [ { 'smooch_menu_option_keyword' => '1', 'smooch_menu_option_value' => 'subscription_confirmation' }, { 'smooch_menu_option_keyword' => '2', 'smooch_menu_option_value' => 'main_state' } ] - elsif ['query', 'add_more_details'].include?(state) && self.is_v2? + elsif ['query', 'add_more_details'].include?(state) destination = { 'query' => 'main_state', 'add_more_details' => 'ask_if_ready_state' }[state] [{ 'smooch_menu_option_keyword' => '1', 'smooch_menu_option_value' => destination }] # Custom menus @@ -445,7 +443,7 @@ def self.get_menu_options(state, workflow, uid) def self.get_custom_menu_options(state, workflow, uid) options = workflow.dig("smooch_state_#{state}", 'smooch_menu_options').to_a.clone - if ['main', 'waiting_for_message'].include?(state) && self.is_v2? + if ['main', 'waiting_for_message'].include?(state) if self.should_ask_for_language_confirmation?(uid) options = [] i = 0 @@ -492,11 +490,10 @@ def self.process_menu_option_value_for_state(value, message, language, workflow, sm = CheckStateMachine.new(uid) self.bundle_message(message) new_state = value.gsub(/_state$/, '') - self.delay_for(self.time_to_send_request, { queue: 'smooch', retry: false }).bundle_messages(uid, message['_id'], app_id) if new_state == 'query' && !self.is_v2? sm.send("go_to_#{new_state}") self.delay_for(1.seconds, { queue: 'smooch_priority', retry: false }).search(app_id, uid, language, message, self.config['team_id'].to_i, workflow, RequestStore.store[:smooch_bot_provider]) if new_state == 'search' self.clear_user_bundled_messages(uid) if new_state == 'main' - new_state == 'main' && self.is_v2? ? self.send_message_to_user_with_main_menu_appended(uid, self.get_string('cancelled', language), workflow, language) : self.send_message_for_state(uid, workflow, new_state, language) + new_state == 'main' ? self.send_message_to_user_with_main_menu_appended(uid, self.get_string('cancelled', language), workflow, language) : self.send_message_for_state(uid, workflow, new_state, language) end def self.process_menu_option_value(value, option, message, language, workflow, app_id) @@ -542,7 +539,7 @@ def self.process_menu_option_value(value, option, message, language, workflow, a end def self.is_a_shortcut_for_submission?(state, message) - self.is_v2? && (state == 'main' || state == 'waiting_for_message') && ( + (state == 'main' || state == 'waiting_for_message') && ( !message['mediaUrl'].blank? || ::Bot::Alegre.get_number_of_words(message['text'].to_s) > CheckConfig.get('min_number_of_words_for_tipline_submit_shortcut', 10, :integer) || !Twitter::TwitterText::Extractor.extract_urls(message['text'].to_s).blank? # URL in message? diff --git a/app/models/concerns/smooch_language.rb b/app/models/concerns/smooch_language.rb index 56bb3b3ce..19d9f1e52 100644 --- a/app/models/concerns/smooch_language.rb +++ b/app/models/concerns/smooch_language.rb @@ -44,7 +44,7 @@ def get_supported_languages end def should_ask_for_language_confirmation?(uid) - self.is_v2? && self.get_supported_languages.size > 1 && !self.user_language_confirmed?(uid) + self.get_supported_languages.size > 1 && !self.user_language_confirmed?(uid) end def get_language(message, fallback_language = 'en') diff --git a/app/models/concerns/smooch_menus.rb b/app/models/concerns/smooch_menus.rb index bd2df20bb..6bd5051ce 100644 --- a/app/models/concerns/smooch_menus.rb +++ b/app/models/concerns/smooch_menus.rb @@ -4,10 +4,6 @@ module SmoochMenus extend ActiveSupport::Concern module ClassMethods - def is_v2? - self.config['smooch_version'] == 'v2' - end - def send_message_to_user_with_main_menu_appended(uid, text, workflow, language, tbi_id = nil, event = nil) self.get_installation('team_bot_installation_id', tbi_id) { |i| i.id == tbi_id } if self.config.blank? && !tbi_id.nil? main = [] @@ -304,12 +300,10 @@ def ask_for_language_confirmation(_workflow, language, uid, with_text = true) end def send_greeting(uid, workflow) - if self.is_v2? - text = self.get_custom_string('smooch_message_smooch_bot_greetings', workflow['smooch_workflow_language']) - image = workflow['smooch_greeting_image'] if workflow['smooch_greeting_image'] =~ /^https?:\/\// - image.blank? || image == 'none' ? self.send_message_to_user(uid, text) : self.send_message_to_user(uid, text, { 'type' => 'image', 'mediaUrl' => image }) - sleep 3 # Give it some time, so the main menu message is sent after the greetings - end + text = self.get_custom_string('smooch_message_smooch_bot_greetings', workflow['smooch_workflow_language']) + image = workflow['smooch_greeting_image'] if workflow['smooch_greeting_image'] =~ /^https?:\/\// + image.blank? || image == 'none' ? self.send_message_to_user(uid, text) : self.send_message_to_user(uid, text, { 'type' => 'image', 'mediaUrl' => image }) + sleep 3 # Give it some time, so the main menu message is sent after the greetings end end end diff --git a/app/models/concerns/smooch_messages.rb b/app/models/concerns/smooch_messages.rb index 2e8a2d590..7eebbb01c 100644 --- a/app/models/concerns/smooch_messages.rb +++ b/app/models/concerns/smooch_messages.rb @@ -78,25 +78,19 @@ def bundle_messages(uid, id, app_id, type = 'default_requests', annotated = nil, end def send_final_message_to_user(uid, text, workflow, language, event = nil) - if self.is_v2? - CheckStateMachine.new(uid).go_to_main - self.send_message_to_user_with_main_menu_appended(uid, text, workflow, language, nil, event) - else - self.send_message_to_user(uid, text) - end + CheckStateMachine.new(uid).go_to_main + self.send_message_to_user_with_main_menu_appended(uid, text, workflow, language, nil, event) end def send_final_messages_to_user(uid, text, workflow, language, interval = 1, preview_url = true, event = nil) response = self.send_message_to_user(uid, text, {}, false, preview_url, event) - if self.is_v2? - label = self.get_string('navigation_button', language) - CheckStateMachine.new(uid).go_to_main - if interval > 1 - self.delay_for(interval.seconds, { queue: 'smooch' }).send_message_to_user_with_main_menu_appended(uid, label, nil, language, self.config['installation_id']) - else - sleep(interval) - response = self.send_message_to_user_with_main_menu_appended(uid, label, workflow, language) - end + label = self.get_string('navigation_button', language) + CheckStateMachine.new(uid).go_to_main + if interval > 1 + self.delay_for(interval.seconds, { queue: 'smooch' }).send_message_to_user_with_main_menu_appended(uid, label, nil, language, self.config['installation_id']) + else + sleep(interval) + response = self.send_message_to_user_with_main_menu_appended(uid, label, workflow, language) end response end @@ -106,15 +100,11 @@ def send_message_for_state(uid, workflow, state, language, pretext = '', event = message = self.get_message_for_state(workflow, state, language, uid).to_s message = UrlRewriter.shorten_and_utmize_urls(message, team.get_outgoing_urls_utm_code) if team.get_shorten_outgoing_urls text = [pretext, message].reject{ |part| part.blank? }.join("\n\n") - if self.is_v2? - if self.should_ask_for_language_confirmation?(uid) - self.ask_for_language_confirmation(workflow, language, uid) - else - # On v2, when we go to the "main" state, we need to show the main menu - state == 'main' ? self.send_message_to_user_with_main_menu_appended(uid, text, workflow, language, nil, event) : self.send_message_for_state_with_buttons(uid, text, workflow, state, language) - end + if self.should_ask_for_language_confirmation?(uid) + self.ask_for_language_confirmation(workflow, language, uid) else - self.send_message_to_user(uid, text) + # On v2, when we go to the "main" state, we need to show the main menu + state == 'main' ? self.send_message_to_user_with_main_menu_appended(uid, text, workflow, language, nil, event) : self.send_message_for_state_with_buttons(uid, text, workflow, state, language) end end @@ -144,13 +134,11 @@ def send_message_for_state_with_buttons(uid, text, workflow, state, language) def get_message_for_state(workflow, state, language, uid = nil) message = [] - is_v2 = (self.config['smooch_version'] == 'v2') if state.to_s == 'main' - message << (is_v2 ? self.get_string('navigation_button', language) : workflow['smooch_message_smooch_bot_greetings']) - message << self.tos_message(workflow, language) unless is_v2 + message << self.get_string('navigation_button', language) end message << self.subscription_message(uid, language) if state.to_s == 'subscription' - message << self.get_custom_string(["smooch_state_#{state}", 'smooch_menu_message'], language) if state != 'main' || !is_v2 + message << self.get_custom_string(["smooch_state_#{state}", 'smooch_menu_message'], language) if state != 'main' message << self.get_custom_string("#{state}_state", language) if ['search', 'search_result', 'add_more_details', 'ask_if_ready'].include?(state.to_s) message.reject{ |m| m.blank? }.join("\n\n") end diff --git a/test/models/bot/smooch_6_test.rb b/test/models/bot/smooch_6_test.rb index de5b67ff2..e8c5c7f2e 100644 --- a/test/models/bot/smooch_6_test.rb +++ b/test/models/bot/smooch_6_test.rb @@ -121,10 +121,6 @@ def send_message_outside_24_hours_window(template, pm = nil) assert_not_nil Rails.cache.read("smooch:original:#{@msgid}") end - test "should use v2" do - assert Bot::Smooch.is_v2? - end - test "should start on tipline bot v2" do assert_state 'waiting_for_message' send_message 'hello'