-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
82 lines (68 loc) · 1.97 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
75
76
77
78
79
80
81
82
# Require dependencies
require 'sinatra'
require 'sinatra/multi_route'
require 'sinatra/activerecord'
require 'vonage'
require 'dotenv/load'
require 'json'
# Require models
current_dir = Dir.pwd
Dir["#{current_dir}/models/*.rb"].each { |file| require file }
# Set up the database
set :database, { adapter: 'postgresql', encoding: 'unicode', database: 'voice_demo_db', pool: 2 }
# Helper method to parse incoming phone call data into JSON
helpers do
def parsed_body
JSON.parse(request.body.read)
end
end
# Set content type for application to JSON
before do
content_type :json
end
# Create variables to hold Vonage API credentials
VONAGE_API_KEY = ENV['VONAGE_API_KEY']
VONAGE_API_SECRET = ENV['VONAGE_API_SECRET']
VONAGE_APPLICATION_ID = ''
VONAGE_NUMBER = ''
# Instantiate Vonage SDK client
def vonage
@vonage ||= Vonage::Client.new(
api_key: VONAGE_API_KEY,
api_secret: VONAGE_API_SECRET,
application_id: VONAGE_APPLICATION_ID,
private_key: File.read('./private.key')
)
end
# Accept incoming phone calls and create new contestant entries
route :post, '/webhooks/answer' do
from = params['from'] || parsed_body['from']
new_contestant = Contestant.new(phone_number: from)
if new_contestant.save
puts "New entry received!"
message = 'Thanks for entering the raffle!'
else
message = "Thanks for calling, however: #{new_contestant.errors.full_messages}"
end
[{
action: 'talk',
text: message
}].to_json
end
route :get, '/winner' do
puts 'Picking a random contestant...'
winner = Contestant.order('RANDOM()').first
puts 'Calling the winner now...'
response = vonage.voice.create(
to: [{ type: 'phone', number: winner.phone_number }],
from: { type: 'phone', number: VONAGE_NUMBER },
ncco: [{ action: 'talk', text: 'Congratulations! You won! Please find us to claim your prize.' }]
)
puts response.inspect
end
route :get, '/webhooks/event' do
status 200
body ''
end