@@ -13,6 +13,9 @@ class CapiUnhandledMessageWarning < MessageDeliveryError; end
1313 SUPPORTED_INTEGRATION_NAMES = { 'whatsapp' => 'WhatsApp' , 'messenger' => 'Facebook Messenger' , 'twitter' => 'Twitter' , 'telegram' => 'Telegram' , 'viber' => 'Viber' , 'line' => 'LINE' , 'instagram' => 'Instagram' }
1414 SUPPORTED_INTEGRATIONS = SUPPORTED_INTEGRATION_NAMES . keys
1515 SUPPORTED_TRIGGER_MAPPING = { 'message:appUser' => :incoming , 'message:delivery:channel' => :outgoing }
16+ TIPLINE_CUSTOMIZABLE_MESSAGES = [ 'smooch_message_smooch_bot_greetings' , 'submission_prompt' , 'add_more_details_state' , 'ask_if_ready_state' ,
17+ 'search_state' , 'search_no_results' , 'search_result_state' , 'search_result_is_relevant' , 'search_submit' ,
18+ 'newsletter_optin_optout' , 'option_not_available' , 'timeout' , 'smooch_message_smooch_bot_disabled' ]
1619
1720 check_settings
1821
@@ -960,31 +963,33 @@ def self.send_correction_to_user(data, pm, tipline_request, last_published_at, a
960963 self . get_platform_from_message ( data )
961964 uid = data [ 'authorId' ]
962965 lang = data [ 'language' ]
963- field_name = ''
964966 # User received a report before
965967 if subscribed_at . to_i < last_published_at . to_i && published_count > 0
966968 if [ 'publish' , 'republish_and_resend' ] . include? ( action )
967- field_name = 'smooch_report_correction_sent_at'
968- self . send_report_to_user ( uid , data , pm , lang , 'fact_check_report_updated' , self . get_string ( :report_updated , lang ) )
969+ self . send_report_to_user ( uid , data , pm , lang , 'fact_check_report_updated' , self . get_string ( :report_updated , lang ) , tipline_request )
969970 end
970971 # First report
971972 else
972- field_name = 'smooch_report_sent_at'
973- self . send_report_to_user ( uid , data , pm , lang , 'fact_check_report' )
974- end
975- unless field_name . blank?
976- tipline_request . skip_check_ability = true
977- tipline_request . send ( "#{ field_name } =" , Time . now . to_i )
978- tipline_request . save!
973+ self . send_report_to_user ( uid , data , pm , lang , 'fact_check_report' , nil , tipline_request )
979974 end
980975 end
981976
982- def self . send_report_to_user ( uid , data , pm , lang = 'en' , fallback_template = nil , pre_message = nil )
977+ def self . send_report_to_user ( uid , data , pm , lang = 'en' , fallback_template = nil , pre_message = nil , tipline_request = nil )
983978 parent = Relationship . confirmed_parent ( pm )
984979 return if parent . nil?
985980 report = parent . get_dynamic_annotation ( 'report_design' )
986- Rails . logger . info "[Smooch Bot] Sending report to user #{ uid } for item with ID #{ pm . id } ..."
987- if report &.get_field_value ( 'state' ) == 'published' && [ CheckArchivedFlags ::FlagCodes ::NONE , CheckArchivedFlags ::FlagCodes ::UNCONFIRMED ] . include? ( parent . archived ) && report . should_send_report_in_this_language? ( lang )
981+ Rails . logger . info "[Smooch Bot] Trying to send report to user #{ uid } for item with ID #{ pm . id } ..."
982+
983+ # Only send a report if these conditions are met
984+ should_send_report_in_language = !!report &.should_send_report_in_this_language? ( lang )
985+ if report &.get_field_value ( 'state' ) == 'published' && [ CheckArchivedFlags ::FlagCodes ::NONE , CheckArchivedFlags ::FlagCodes ::UNCONFIRMED ] . include? ( parent . archived ) && should_send_report_in_language
986+
987+ # Map the template name to a ticker field
988+ ticker_field_name = {
989+ fact_check_report_updated : 'smooch_report_correction_sent_at' ,
990+ fact_check_report : 'smooch_report_sent_at'
991+ } [ fallback_template . to_s . to_sym ]
992+
988993 unless pre_message . blank?
989994 self . send_message_to_user ( uid , pre_message )
990995 sleep 1
@@ -1005,6 +1010,12 @@ def self.send_report_to_user(uid, data, pm, lang = 'en', fallback_template = nil
10051010 Rails . logger . info "[Smooch Bot] Sent report visual card to user #{ uid } for item with ID #{ pm . id } , response was: #{ last_smooch_response &.body } "
10061011 end
10071012 self . save_smooch_response ( last_smooch_response , parent , data [ 'received' ] , fallback_template , lang )
1013+
1014+ if tipline_request . present? && ticker_field_name . present? && last_smooch_response . present?
1015+ tipline_request . update_column ( ticker_field_name , Time . now . to_i )
1016+ end
1017+ else
1018+ Rails . logger . info "[Smooch Bot] Not sending report to user #{ uid } for item with ID #{ pm . id } . Report state: #{ report &.get_field_value ( 'state' ) } | Item archived flag: #{ parent . archived } | Should send report in #{ lang } ? #{ should_send_report_in_language } "
10081019 end
10091020 end
10101021
@@ -1035,7 +1046,7 @@ def self.send_report_from_parent_to_child(parent_id, target_id)
10351046 data = tr . smooch_data
10361047 self . get_platform_from_message ( data )
10371048 self . get_installation ( self . installation_setting_id_keys , data [ 'app_id' ] ) if self . config . blank?
1038- self . send_report_to_user ( tr . tipline_user_uid , data , parent , tr . language , 'fact_check_report' )
1049+ self . send_report_to_user ( tr . tipline_user_uid , data , parent , tr . language , 'fact_check_report' , nil , tr )
10391050 end
10401051 end
10411052
@@ -1108,4 +1119,28 @@ def self.sanitize_installation(team_bot_installation, blast_secret_settings = fa
11081119 end
11091120 team_bot_installation
11101121 end
1122+
1123+ def self . default_settings
1124+ settings = [
1125+ {
1126+ 'smooch_workflow_language' => 'en' ,
1127+ 'smooch_state_main' => {
1128+ 'smooch_menu_message' => '' ,
1129+ 'smooch_menu_options' => [ ]
1130+ } ,
1131+ 'smooch_state_secondary' => {
1132+ 'smooch_menu_message' => '' ,
1133+ 'smooch_menu_options' => [ ]
1134+ } ,
1135+ 'smooch_state_query' => {
1136+ 'smooch_menu_message' => '' ,
1137+ 'smooch_menu_options' => [ ]
1138+ } ,
1139+ }
1140+ ]
1141+ ::Bot ::Smooch ::TIPLINE_CUSTOMIZABLE_MESSAGES . each do |key |
1142+ settings [ 0 ] [ key ] = ''
1143+ end
1144+ settings
1145+ end
11111146end
0 commit comments