|
require 'sinatra' |
|
require 'data_mapper' |
|
require 'dm-migrations' |
|
require 'twilio-ruby' |
|
require 'json' |
|
|
|
# Get these credentials from http://twilio.com/user/account |
|
TWILIO_ACCOUNT_SID = 'my-account-sid' |
|
TWILIO_AUTH_TOKEN = 'my-auth-token' |
|
TWILIO_PHONE_NUMBER = 'my-phone-number' |
|
# Your URL should be accessible through the web. |
|
# If you are testing locally, you can use 'ngrok' to expose it. |
|
# Remember to append the /twiml suffix. |
|
TWIML_URL = 'https://www.example.org/twiml' |
|
|
|
DataMapper.setup(:default, 'sqlite::memory:') |
|
|
|
class User |
|
include DataMapper::Resource |
|
|
|
property :id, Serial |
|
property :phone_number, String, required: true |
|
property :verification_code, String |
|
property :verified, Boolean, default: false |
|
end |
|
|
|
DataMapper.finalize |
|
DataMapper.auto_migrate! |
|
|
|
get '/' do |
|
send_file 'index.html' |
|
end |
|
|
|
post '/call' do |
|
verification_code = rand(100000...999999) |
|
phone_number = params[:phone_number] |
|
|
|
User.all.destroy |
|
User.create( |
|
phone_number: phone_number, |
|
verification_code: verification_code, |
|
verified: false |
|
) |
|
|
|
client = Twilio::REST::Client.new TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN |
|
client.account.calls.create( |
|
from: TWILIO_PHONE_NUMBER, |
|
to: phone_number, |
|
url: TWIML_URL |
|
) |
|
|
|
{ verification_code: verification_code}.to_json |
|
end |
|
|
|
post '/twiml' do |
|
code = params[:Digits] |
|
if code.nil? || code.empty? |
|
Twilio::TwiML::Response.new do |r| |
|
r.Gather numDigits: '6' do |g| |
|
g.Say 'Please enter your verification code.' |
|
end |
|
end |
|
else |
|
phone_number = params[:Called] |
|
user = User.first(phone_number: phone_number, verification_code: code) |
|
if user.nil? |
|
Twilio::TwiML::Response.new do |r| |
|
r.Gather numDigits: '6' do |g| |
|
g.Say 'Verification code incorrect, please try again.' |
|
end |
|
end |
|
else |
|
user.verified = true |
|
user.save() |
|
Twilio::TwiML::Response.new do |r| |
|
r.Say 'Thank you! Your phone number has been verified.' |
|
end |
|
end |
|
end.text |
|
end |
|
|
|
post '/status' do |
|
phone_number = params[:phone_number] |
|
user = User.first(phone_number: phone_number, verified: true) |
|
{status: user.nil? ? 'unverified': 'verified'}.to_json |
|
end |
@mosampaio, would you mind removing the trailing slashes from the route's definitions please?
e.g. https://gist.github.com/mosampaio/0771e1322b81600b09f25737c303f4fb/#file-app-rb-L55