This repository has been archived by the owner on May 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.rb
74 lines (61 loc) · 1.84 KB
/
app.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# frozen_string_literal: true
require 'twilio-ruby'
require 'sinatra'
require 'sinatra/json'
require 'dotenv'
require 'faker'
# Load environment configuration
Dotenv.load
# Set the environment after dotenv loads
# Default to production
enviroment = (ENV['APP_ENV'] || ENV['RACK_ENV'] || :production).to_sym
set :environment, enviroment
# Render home page
get '/' do
File.read(File.join('public', 'index.html'))
end
# Generate a token for use in our Video application
get '/token' do
# Required for any Twilio Access Token
account_sid = ENV['TWILIO_ACCOUNT_SID']
api_key = ENV['API_KEY']
api_secret = ENV['API_SECRET']
# Required for Voice
outgoing_application_sid = ENV['TWILIO_TWIML_APP_SID']
# Create a random username for the client
identity = Faker::Internet.user_name.gsub(/[^0-9a-z_]/i, '')
# Create Voice grant for our token
grant = Twilio::JWT::AccessToken::VoiceGrant.new
grant.outgoing_application_sid = outgoing_application_sid
# Optional: add to allow incoming calls
grant.incoming_allow = true
# Create an Access Token
token = Twilio::JWT::AccessToken.new(
account_sid,
api_key,
api_secret,
[grant],
identity: identity
)
# Generate the token and send to client
json :identity => identity, :token => token.to_jwt
end
post '/voice' do
twiml = Twilio::TwiML::VoiceResponse.new do |r|
if params['To'] && params['To'] != ''
r.dial(caller_id: ENV['TWILIO_CALLER_ID']) do |d|
# wrap the phone number or client name in the appropriate TwiML verb
# by checking if the number given has only digits and format symbols
if params['To'] =~ /^[\d\+\-\(\) ]+$/
d.number(params['To'])
else
d.client identity: params['To']
end
end
else
r.say(message: "Thanks for calling!")
end
end
content_type 'text/xml'
twiml.to_s
end