From b7e9c568897cf4c68d3796637220353f3207f32b Mon Sep 17 00:00:00 2001 From: Austin Miller Date: Tue, 19 Nov 2024 12:36:38 -0700 Subject: [PATCH] Add banned phones functionality --- .../live_call_routing/twilio/v3.rb | 48 ++++++++++++++++++- .../twilio/v3/_form_options.html.erb | 6 +++ .../twilio/v3/_show_options.html.erb | 13 +++++ config/locales/en.yml | 2 + 4 files changed, 68 insertions(+), 1 deletion(-) diff --git a/app/models/pager_tree/integrations/live_call_routing/twilio/v3.rb b/app/models/pager_tree/integrations/live_call_routing/twilio/v3.rb index 138bb1b..39b3f7a 100644 --- a/app/models/pager_tree/integrations/live_call_routing/twilio/v3.rb +++ b/app/models/pager_tree/integrations/live_call_routing/twilio/v3.rb @@ -7,6 +7,7 @@ class LiveCallRouting::Twilio::V3 < Integration {key: :force_input, type: :boolean, default: false}, {key: :record, type: :boolean, default: false}, {key: :record_email, type: :string, default: ""}, + {key: :banned_phone, type: :string, default: ""}, {key: :dial_pause, type: :integer} ] store_accessor :options, *OPTIONS.map { |x| x[:key] }.map(&:to_s), prefix: "option" @@ -32,6 +33,7 @@ class LiveCallRouting::Twilio::V3 < Integration self.option_force_input ||= false self.option_record ||= false self.option_record_email ||= "" + self.option_banned_phone ||= "" end SPEAK_OPTIONS = { @@ -67,6 +69,14 @@ def option_record_emails self.option_record_email.split(",") end + def option_banned_phones=(x) + self.option_banned_phone = Array(x).join(",") + end + + def option_banned_phones + self.option_banned_phone.split(",") + end + def option_record_emails_list=(x) # what comes in as json, via tagify uniq_array = [] @@ -83,6 +93,22 @@ def option_record_emails_list option_record_emails end + def option_banned_phones_list=(x) + # what comes in as json, via tagify + uniq_array = [] + begin + uniq_array = JSON.parse(x).map { |y| y["value"] }.uniq + rescue JSON::ParserError => exception + Rails.logger.debug(exception) + end + + self.option_banned_phones = uniq_array + end + + def option_banned_phones_list + option_banned_phones + end + def validate_record_emails errors.add(:record_emails, "must be a valid email") if option_record_emails.any? { |x| !(x.match(URI::MailTo::EMAIL_REGEXP) || ["team", "team-admin", "on-call"].include?(x)) } end @@ -111,8 +137,20 @@ def adapter_will_route_alert? true end + def is_banned? + from_number = adapter_incoming_request_params.dig("From") + return false unless from_number.present? + option_banned_phones.any? { |x| from_number.include?(x) } + rescue + false + end + def adapter_action - :create + if is_banned? + :other + else + :create + end end def adapter_thirdparty_id @@ -131,6 +169,14 @@ def adapter_process_create end def adapter_response_incoming + if is_banned? + _twiml.reject + + adapter_source_log&.sublog("Caller #{adapter_incoming_request_params.dig("From")} on blocked list. Rejected call.") + adapter_source_log&.save + + return adapter_controller&.render(xml: _twiml.to_xml) + end # if this was attached to a router if !adapter_alert.meta["live_call_router_team_prefix_ids"].present? && routers.size > 0 && account.subscription_feature_routers? adapter_alert.logs.create!(message: "Routed to router. Attempting to get a list of teams...") diff --git a/app/views/pager_tree/integrations/live_call_routing/twilio/v3/_form_options.html.erb b/app/views/pager_tree/integrations/live_call_routing/twilio/v3/_form_options.html.erb index caf435d..09371d1 100644 --- a/app/views/pager_tree/integrations/live_call_routing/twilio/v3/_form_options.html.erb +++ b/app/views/pager_tree/integrations/live_call_routing/twilio/v3/_form_options.html.erb @@ -77,4 +77,10 @@

<%== t(".option_record_emails_list_hint_html") %>

+
+ <%= form.label :option_banned_phones_list %> + <%= form.text_field :option_banned_phones_list, class: "form-control", data: { tagify_target: "input" } %> +

<%== t(".option_banned_phones_list_hint_html") %>

+
+ \ No newline at end of file diff --git a/app/views/pager_tree/integrations/live_call_routing/twilio/v3/_show_options.html.erb b/app/views/pager_tree/integrations/live_call_routing/twilio/v3/_show_options.html.erb index c1558ea..dfa7625 100644 --- a/app/views/pager_tree/integrations/live_call_routing/twilio/v3/_show_options.html.erb +++ b/app/views/pager_tree/integrations/live_call_routing/twilio/v3/_show_options.html.erb @@ -170,6 +170,19 @@ +
+
+ <%= t("activerecord.attributes.pager_tree/integrations/live_call_routing/twilio/v3.option_banned_phones_list") %> +
+
+
+

+ <%= integration.option_banned_phones.length %> +

+
+
+
+
<%= t("activerecord.attributes.pager_tree/integrations/live_call_routing/twilio/v3.option_force_input") %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 757f098..c3af7d0 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -83,6 +83,7 @@ en: option_force_input_hint_html: "Force the caller to select a team (even if the integration only has one team)" option_record_hint_html: "Record a voicemail when no one acknowledges the call" option_record_emails_list_hint_html: "List of email addresses to notify when a voicemail has been recorded" + option_banned_phones_list_hint_html: "List of phone numbers that are banned from calling the integration" v3_mailer: call_recording: subject: "🎧 Alert #%{tiny_id} - New voicemail from %{from}" @@ -211,6 +212,7 @@ en: option_force_input: "Force Caller Input" option_record: "Voicemail" option_record_emails_list: "Voicemail Emails" + option_banned_phones_list: "Banned Phones" "pager_tree/integrations/logic_monitor/v3": option_access_id: "Logic Monitor Access ID" option_access_key: "Logic Monitor Access Key"