-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
144 lines (121 loc) · 3.38 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# app.rb
require 'sinatra'
require 'twilio-ruby'
require_relative 'init'
class App < Sinatra::Base
helpers HotlineConfig
configure :production, :development do
enable :logging
end
get '/' do
return 200
end
post '/call?' do
client = get_twilio_client
caller = params[:From]
active_conferences = get_active_conferences(client)
text_main_members(caller, client) unless get_main_members.include?(caller)
if active_conferences.size == 0
call_main_members(client, caller)
sleep(10)
get_start_conference_xml
else
if get_main_members.include?(caller)
get_start_conference_xml
else
conference_sid = active_conferences.first.sid
num_outside_participants = get_number_outside_participants(conference_sid, client)
if num_outside_participants == 0
get_start_conference_xml
else
text_missed_call_main_members(caller, client)
get_try_again_xml
end
end
end
end
post '/sms?' do
sender = params[:From]
message = params[:Body]
client = get_twilio_client
if get_main_members.include?(sender)
client.messages.create(
from: get_main_number,
to: sender,
body: "You can't reply to a message from this number."
)
else
get_main_members.each do |phone_number|
client.messages.create(
from: get_main_number,
to: phone_number,
body: 'from: ' + sender + "\n\n" + message
)
end
end
end
post '/start_conference?' do
get_start_conference_xml
end
### internals
def get_active_conferences(client)
return client.account.conferences.list({
:status => "in-progress",
:friendly_name => get_conference_id})
end
def get_number_outside_participants(conference_sid, client)
num_outside_participants = 0
conference = client.account.conferences.get(conference_sid)
participants = conference.participants
participants.list.each do |participant|
participant_number = client.account.calls.get(participant.call_sid).from
if !get_main_members.include?(participant_number)
num_outside_participants = num_outside_participants + 1
end
end
return num_outside_participants
end
def text_main_members(caller, client)
get_main_members.each do |phone_number|
client.messages.create(
from: get_main_number,
to: phone_number,
body: 'Incoming call from: ' + caller
)
end
end
def text_missed_call_main_members(caller, client)
get_main_members.each do |phone_number|
client.messages.create(
from: get_main_number,
to: phone_number,
body: 'Missed call from: ' + caller
)
end
end
def call_main_members(client, caller)
get_main_members.each do |phone_number|
unless phone_number == caller
client.calls.create(
from: get_main_number,
to: phone_number,
timeout: 5,
url: get_host + '/start_conference'
)
end
end
end
def get_start_conference_xml
Twilio::TwiML::Response.new do |r|
r.Dial do |d|
r.Conference get_conference_id
end
r.Say 'Goodbye'
end.text
end
def get_try_again_xml
Twilio::TwiML::Response.new do |r|
r.Say "Sorry, we are currently unavailable. We'll call you right back."
end.text
end
end