Skip to content

Commit

Permalink
Remove code related to tipline bot v1
Browse files Browse the repository at this point in the history
Reference: CV2-2550
  • Loading branch information
danielevalverde committed Aug 26, 2024
1 parent f1f3b6c commit 7085b81
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 56 deletions.
21 changes: 7 additions & 14 deletions app/models/bot/smooch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -358,14 +358,8 @@ def self.start_flow(workflow, language, uid)

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?
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
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)
end

def self.parse_message_based_on_state(message, app_id)
Expand Down Expand Up @@ -429,12 +423,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
Expand All @@ -445,7 +439,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
Expand Down Expand Up @@ -492,11 +486,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)
Expand Down Expand Up @@ -542,7 +535,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') && (!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))
(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))
end

def self.process_menu_option(message, state, app_id)
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/smooch_language.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
14 changes: 4 additions & 10 deletions app/models/concerns/smooch_menus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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
42 changes: 15 additions & 27 deletions app/models/concerns/smooch_messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions test/models/bot/smooch_6_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 7085b81

Please sign in to comment.