Class: Twilio::Rails::SMS::SendOperation
- Inherits:
-
ApplicationOperation
- Object
- ActiveOperation::Base
- ApplicationOperation
- Twilio::Rails::SMS::SendOperation
- Defined in:
- app/operations/twilio/rails/sms/send_operation.rb
Overview
Public entrypoint used to send an SMS message. This operation will create a new conversation to the phone caller and send a series of messages to them. The interaction will be stored in the database and sent via Twilio’s API. The operation will raise if the #from_number is not a valid phone number.
Note: Operations should be called with ‘call(params)` and not by calling `new(params).execute` directly.
Constant Summary collapse
- TWILIO_UNSUBSCRIBED_ERROR_CODES =
[ 21610 ].freeze
Instance Method Summary collapse
-
#execute ⇒ Twilio::Rails::Models::SMSConversation
number is ‘nil` then it will attempt to extract the phone number from the last phone call.
Instance Method Details
#execute ⇒ Twilio::Rails::Models::SMSConversation
number is ‘nil` then it will attempt to extract the phone number from the last phone call. If that is not found then it will raise Error.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'app/operations/twilio/rails/sms/send_operation.rb', line 30 def execute return nil if .blank? raise Twilio::Rails::SMS::Error, "from_number=#{ from_number } is not a valid phone number" if from_number.present? && !Twilio::Rails::Formatter.coerce_to_valid_phone_number(from_number) conversation = ::Twilio::Rails.config.sms_conversation_class.new( number: calculated_from_number, from_number: calculated_to_number, from_city: phone_call&.from_city, from_province: phone_call&.from_province, from_country: phone_call&.from_country, ) conversation.save! .each do |body| sid = nil begin sid = Twilio::Rails::Client.( message: body, to: calculated_to_number, from: calculated_from_number, ) rescue Twilio::REST::RestError => e if TWILIO_UNSUBSCRIBED_ERROR_CODES.include?(e.code) Twilio::Rails.config.logger.tagged(self.class) { |l| l.warn("tried to send to unsubscribed and got Twilio::REST::RestError code=21610 phone_caller_id=#{ phone_caller.id } phone_number=#{ calculated_to_number } message=#{ body }") } else Twilio::Rails.notify_exception(e, message: "Failed to send Twilio message. Got REST error response.", context: { to: calculated_to_number, from: calculated_from_number, phone_call_id: phone_call&.id, }, exception_binding: binding ) raise end end = conversation..build(body: body, sid: sid, direction: "outbound") .save! end conversation end |