Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add banned phones functionality #126

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 = {
Expand Down Expand Up @@ -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 = []
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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...")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,10 @@
<p class="form-hint"><%== t(".option_record_emails_list_hint_html") %></p>
</div>

<div class="form-group group" data-controller="tagify">
<%= form.label :option_banned_phones_list %>
<%= form.text_field :option_banned_phones_list, class: "form-control", data: { tagify_target: "input" } %>
<p class="form-hint"><%== t(".option_banned_phones_list_hint_html") %></p>
</div>

</div>
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,19 @@
</dd>
</div>

<div class="sm:col-span-2">
<dt class="text-sm font-medium text-gray-500">
<%= t("activerecord.attributes.pager_tree/integrations/live_call_routing/twilio/v3.option_banned_phones_list") %>
</dt>
<dd class="mt-1 text-sm text-gray-900">
<div class="flex items-center gap-2">
<p class="flex text-sm truncate">
<%= integration.option_banned_phones.length %>
</p>
</div>
</dd>
</div>

<div class="sm:col-span-1">
<dt class="text-sm font-medium text-gray-500">
<%= t("activerecord.attributes.pager_tree/integrations/live_call_routing/twilio/v3.option_force_input") %>
Expand Down
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down Expand Up @@ -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"
Expand Down