-
Notifications
You must be signed in to change notification settings - Fork 13
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
[FYST-1089] refactor twilio service #4944
Draft
jnf
wants to merge
22
commits into
main
Choose a base branch
from
jnf/FYST-1089/refactor-twilio-service
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 5 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
7ebf9aa
change the structure of the credentials file (just dev for now) to ha…
jnf a914b96
an idea of how to evolve the twilio service, presented as a v2 for ea…
jnf 505cdfc
tweak how the multi tenant service fetches twilio creds
jnf 623d5dd
bring the v2 version of TwilioClient into the original object so i ca…
jnf ae26344
updates TwilioService specs to conform to new API
jnf c12385b
don't need to clear a class variable in the mock twilio object anymore
jnf 530dcf4
instantiate TwilioService as a gyr messenger unless caller says other…
jnf eacfb9d
updates callsite and specs for twilio webhooks controller; this is th…
jnf ceee3f7
updates callsites and specs for sms sending in the bulk signup
jnf 077654d
updates the other #get_metadata callsite and associated specs
jnf 64d8be2
update the twilio status backfiller to the new TwilioService api. the…
jnf 2a1a4cd
updates verification code job to use new TwilioService api. also upda…
jnf ee2f22b
updated TwilioService callsites in the CtcSignupMessage, but it doesn…
jnf bb05cb2
updates Signup to use updated TwilioService api. updates specs too. i…
jnf 40e4dbd
updates TwilioService callsite in the send_outgoing_text_message_job …
jnf db5747d
updates the gyr incoming text message job/handling/specs to use the u…
jnf 6812f1e
update code in and specs for TextMessageVerificationCodeService to us…
jnf 4bd6e7b
updates the client login spec to work with the updated TwilioService
jnf 288ac36
updates the login spec to work with the new TwilioService api
jnf 64c28b9
removes the 'v2' twilio service as the changes proposed have been ref…
jnf 4f92fea
updates the hub outbound call from to use TwilioService rather than r…
jnf fa01ca8
updates TwilioService, MultiTenantService, dev credentials, and assoc…
jnf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
class TwilioServiceV2 | ||
FAILED_STATUSES = %w(undelivered failed delivery_unknown twilio_error) | ||
SUCCESSFUL_STATUSES = %w(sent delivered) | ||
IN_PROGRESS_STATUSES = %w(accepted queued sending) << nil | ||
ALL_KNOWN_STATUSES = FAILED_STATUSES + SUCCESSFUL_STATUSES + IN_PROGRESS_STATUSES | ||
ORDERED_STATUSES = %w( | ||
twilio_error | ||
queued | ||
accepted | ||
sending | ||
sent | ||
delivery_unknown | ||
delivered | ||
undelivered | ||
failed | ||
).unshift(nil) # why do we need nil in this list, and why must it be first? | ||
|
||
attr_reader :client, :messaging_service_sid | ||
|
||
def initialize(service_type) | ||
mts = MultiTenantService.new(service_type) | ||
@messaging_service_sid = mts.twilio_creds[:messaging_service_id] | ||
@client = Twilio::REST::Client.new(mts.twilio_creds[:account_sid], mts.twilio_creds[:auth_token]) | ||
end | ||
|
||
def send_text_message(to:, body:, status_callback: nil, outgoing_text_message: nil) | ||
arguments = { | ||
messaging_service_sid: ENV['MESSAGING_SERVICE_SID'] || messaging_service_sid, # why do we check the environment for this?? | ||
to: to, | ||
body: body | ||
} | ||
arguments[:status_callback] = status_callback if status_callback.present? | ||
|
||
DatadogApi.increment("twilio.outgoing_text_messages.sent") | ||
|
||
client.messages.create(**arguments) | ||
rescue Twilio::REST::RestError => e | ||
status_key = | ||
if outgoing_text_message.is_a?(OutgoingMessageStatus) | ||
:delivery_status | ||
else | ||
:twilio_status | ||
end | ||
outgoing_text_message&.update(status_key => "twilio_error") | ||
|
||
unless e.code == 21211 # Invalid 'To' Phone Number https://www.twilio.com/docs/api/errors/21211 | ||
raise # should we include the original exception here (e)?? | ||
end | ||
|
||
nil | ||
end | ||
|
||
def get_metadata(phone_number:) | ||
client.lookups.v2.phone_numbers(phone_number).fetch(fields: 'line_type_intelligence').line_type_intelligence | ||
rescue Twilio::REST::RestError | ||
{} | ||
end | ||
|
||
def valid_request?(request) | ||
validator = Twilio::Security::RequestValidator.new(EnvironmentCredentials.dig(:twilio, :auth_token)) | ||
validator.validate( | ||
request.url, | ||
request.POST, | ||
request.headers["X-Twilio-Signature"], | ||
) | ||
end | ||
|
||
def fetch_attachment(url) | ||
response = Net::HTTP.get_response(URI(url)) # first we get a redirect from Twilio to S3 | ||
response = Net::HTTP.get_response(URI(response['location'])) # then we get a redirect from S3 to S3 | ||
response = Net::HTTP.get_response(URI(response['location'])) # finally we should get a 200 OK with the file | ||
filename_from_s3 = response['content-disposition'].split('"').last # S3 gives us the original filename | ||
{ | ||
filename: filename_from_s3, | ||
body: response.body, | ||
} | ||
rescue ArgumentError => e | ||
Rails.logger.error("Error getting attachment from Twilio: #{url}: #{response&.code}: #{response&.to_hash}") | ||
{ | ||
filename: "unknown-file", | ||
body: nil | ||
} | ||
end | ||
|
||
def parse_attachments(params) | ||
num_media = params["NumMedia"].to_i | ||
|
||
(0...num_media).map do |i| | ||
content_type = params["MediaContentType#{i}"] | ||
attachment = fetch_attachment(params["MediaUrl#{i}"]) | ||
|
||
if FileTypeAllowedValidator.mime_types(Document).include?(content_type) && !attachment[:body].empty? | ||
{ | ||
content_type: params["MediaContentType#{i}"], | ||
filename: attachment[:filename], | ||
body: attachment[:body] | ||
} | ||
else | ||
{ | ||
content_type: "text/plain;charset=UTF-8", | ||
filename: "invalid-#{attachment[:filename]}.txt", | ||
body: <<~TEXT | ||
Unusable file with unknown or unsupported file type. | ||
File name: #{attachment[:filename]} | ||
File type: #{content_type} | ||
File size: #{attachment[:body].size} bytes | ||
TEXT | ||
} | ||
end | ||
end | ||
end | ||
end |
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[DUST] i assume you're planning to remove this comment before merging? would prefer not to leave stuff like this around the codebase
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes! for sure. i literally don't know the answer but would like to. 😅