-
Notifications
You must be signed in to change notification settings - Fork 3
/
outbound_sms_controller.rb
55 lines (47 loc) · 1.21 KB
/
outbound_sms_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
class OutboundSmsController < ApplicationController
# Shows the UI for sending an SMS
def index
@sms = Sms.new
end
# Sends an SMS
def create
# Create a SMS record to be stored in the database
@sms = Sms.new(safe_params)
if @sms.save
deliver @sms
redirect_to :outbound_sms, notice: 'SMS Sent'
else
flash[:alert] = 'Something went wrong'
render :index
end
end
private
# Initializes the Nexmo API client
def nexmo
# We do not pass in any API key or secret as
# we're using environment variables `NEXMO_API_KEY`
# and `NEXMO_API_SECRET`
client = Nexmo::Client.new
end
# Determines the params that can be
# stored in the database safely
def safe_params
params.require(:sms).permit(:to, :from, :text)
end
# Uses the Nexmo API to send the stored
# SMS message
def deliver sms
response = nexmo.send_message(
from: sms.from,
to: sms.to,
text: sms.text
)
# If sending the SMS was a success then store
# the message ID on the SMS record
if response['messages'].first['status'] == '0'
sms.update_attributes(
message_id: response['messages'].first['message-id']
)
end
end
end