Class: WhatsAppNotifier::MessagesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/whatsapp_notifier/messages_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST /send — single-recipient send for the current user



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/whatsapp_notifier/messages_controller.rb', line 9

def create
  to        = params[:to] || params[:phone] or
              raise ActionController::ParameterMissing, "to (or phone)"
  message   = params.require(:message)
  media_url = params[:media_url]

  result = WhatsAppNotifier.deliver(
    to:       to,
    body:     message,
    metadata: { user_id: whatsapp_notifier_user_id, media_url: media_url }
  )

  if result.success?
    render json: { success: true, message_id: result.message_id }
  else
    render json: { success: false, error: result.error_message },
           status: :unprocessable_entity
  end
rescue ActionController::ParameterMissing => e
  render json: { success: false, error: e.message }, status: :bad_request
rescue StandardError => e
  render json: { success: false, error: e.message },
         status: :internal_server_error
end