Class: Api::V1::Twilio::MessagesController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/api/v1/twilio/messages_controller.rb

Overview

Instance Method Summary collapse

Instance Method Details

#media_downloadObject



45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/api/v1/twilio/messages_controller.rb', line 45

def media_download
  @download = Upload.joins(:download_tokens).
                     where(DownloadToken[:token].eq(params[:token])).
                     where(DownloadToken[:expires_at].gteq(Time.current).or(DownloadToken[:expires_at].eq(nil))).first
  if @download&.attachment
    send_data @download.attachment.data, type: @download.attachment.mime_type
  else
    head :not_found
  end
end

#replyObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/controllers/api/v1/twilio/messages_controller.rb', line 16

def reply
  from = params['From']
  to = params['To']
  extra_data = params.to_h.except(:action, :controller)

  # Store the message — create_or_find_by! makes this idempotent against Twilio webhook retries
  m = SmsMessage.create_or_find_by!(unique_id: params['MessageSid']) do |msg|
    msg.sender = from
    msg.recipient = to
    msg.body = params['Body']
    msg.num_media = params['NumMedia']
    msg.raw_data = extra_data
  end
  m.trigger_received_queue # This saves the message without any processing, if the number is blocked it will be moved to suppressed

  # Receive the message will now be downloaded with attachment if any and moved to received
  m.receive_message

  # Response back to twilio
  twiml = Twilio::TwiML::MessagingResponse.new
  render xml: twiml.to_s
end

#update_statusObject



40
41
42
43
# File 'app/controllers/api/v1/twilio/messages_controller.rb', line 40

def update_status
  SmsMessage.update_status(params.to_h)
  head :no_content
end