forked from pusher/pusher-twilio-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
78 lines (65 loc) · 1.9 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
require 'rubygems'
require 'sinatra/base'
require 'yaml'
require 'pusher'
require 'twilio-ruby'
class App < Sinatra::Base
set :public_folder, Proc.new { File.join(root, "public") }
config = YAML.load_file('./config.yml')
Pusher.app_id = config['pusher']['app_id']
Pusher.key = config['pusher']['app_key']
Pusher.secret = config['pusher']['app_secret']
before do
@twilio = Twilio::REST::Client.new config['twilio']['account_sid'], config['twilio']['auth_token']
end
def active_calls
conference = @twilio.account.conferences.list(status:"in-progress",friendly_name:"moscow").first
if conference.nil?
return []
end
conference.participants.list.map do |p|
c = @twilio.account.calls.get(p.call_sid)
{from: c.from, start_time:c.start_time}
end
end
get '/' do
@app_key = config['pusher']['app_key']
erb :index
end
post '/call' do
if( params['AccountSid'] != config['twilio']['account_sid'] )
status 401
else
c = active_calls
c << {from: params['From'], start_time:Time.now}
Pusher['calls'].trigger('call_incoming', c)
response = Twilio::TwiML::Response.new do |r|
r.Dial do |d|
d.Conference 'moscow', muted:true, startConferenceOnEnter:false
end
end
content_type 'text/xml'
response.to_xml
end
end
post '/callstatus' do
if( params['AccountSid'] != config['twilio']['account_sid'] )
status 401
else
c = active_calls
Pusher['calls'].trigger('call_incoming', c)
end
end
post '/sms' do
if( params['AccountSid'] != config['twilio']['account_sid'] )
status 401
else
Pusher['sms'].trigger('sms_received', {
:from_number => '...' + params['From'][-4, 4],
:timestamp => Time.now.strftime("%Y-%m-%dT%H:%M:%S"),
:text => params['Body']
})
end
end
run! if app_file == $0
end