-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinbound_sms_controller.rb
More file actions
47 lines (41 loc) · 1.13 KB
/
inbound_sms_controller.rb
File metadata and controls
47 lines (41 loc) · 1.13 KB
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
class InboundSmsController < ApplicationController
# We disable CSRF for this webhook call
skip_before_action :verify_authenticity_token
# Stores an inbound SMS when a webhook
# from Nexmo is received
def create
# Creates a new SMS record in
# the database
sms = Sms.create(
to: params[:to],
# The number that send the SMS is called a
# MSISDN in this webhook
from: params[:msisdn],
text: params[:text],
message_id: params[:messageId],
# Seperate inbound SMS from outbound ones
is_inbound: true
)
# Send a reply
reply sms
# Return an empty HTTP 200 status regardless
head :ok
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
# Uses the Nexmo API to send a quick reply to the SMS received
# for simplicity we're not storing this one in the database
def reply sms
nexmo.send_message(
from: sms.to,
to: sms.from,
text: sms.text.reverse
)
end
end