Skip to content

Instantly share code, notes, and snippets.

@leggetter
Created July 25, 2012 09:05
Show Gist options
  • Save leggetter/3175204 to your computer and use it in GitHub Desktop.
Save leggetter/3175204 to your computer and use it in GitHub Desktop.
;( function( $ ) {
var pusher;
function init() {
pusher = new Pusher(PUSHER_CONFIG.APP_KEY);
}
$(init);
} )( jQuery );
require 'rubygems'
require 'sinatra/base'
require 'yaml'
require 'pusher'
class App < Sinatra::Base
set :public_folder, Proc.new { File.join(root, "public") }
config = YAML.load_file('./config.yml')
get '/' do
@app_key = config['pusher']['app_key']
erb :index
end
run! if app_file == $0
end
var pusher,
smsChannel,
activeCallChannel;
function init() {
pusher = new Pusher(PUSHER_CONFIG.APP_KEY);
smsChannel = pusher.subscribe('sms');
smsChannel.bind('sms_received', function( data ) {
var li = $('<li>').text( 'From: ' + data.from_number +
' Text: ' + data.text +
' Time: ' + data.timestamp );
$('#sms_messages').prepend( li );
} );
activeCallChannel = pusher.subscribe('calls');
activeCallChannel.bind('call_incoming', function( data ) {
var li = $('<li>').text( 'From: ' + data.from_number +
' Time: ' + data.timestamp );
$('#incoming_calls').prepend( li );
} );
}
post '/call' do
if( params['AccountSid'] != config['twilio']['account_sid'] )
status 401
else
# allow
end
end
post '/sms' do
if( params['AccountSid'] != config['twilio']['account_sid'] )
status 401
else
# allow
end
end
Pusher.app_id = config['pusher']['app_id']
Pusher.key = config['pusher']['app_key']
Pusher.secret = config['pusher']['app_secret']
post '/call' do
if( params['AccountSid'] != config['twilio']['account_sid'] )
status 401
else
Pusher['calls'].trigger('call_incoming', {
:from_number => '...' + params['From'][-4, 4],
:timestamp => Time.now.strftime("%Y-%m-%dT%H:%M:%S")
})
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
pusher:
app_id: <YOUR_APP_ID>
app_key: <YOUR_APP_KEY>
app_secret: <YOUR_APP_SECRET>
twilio:
account_sid: <YOUR_TWILIO_ACCOUNT_SID>
source 'http://rubygems.org'
gem 'sinatra'
gem 'sinatra-contrib'
gem 'pusher'
<!DOCTYPE html>
<html lang="en">
<head>
<title>Pusher &amp; Twilio Example</title>
</head>
<body>
Hello, Pusher &amp; Twilio
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script src="http://js.pusher.com/1.12/pusher.min.js"></script>
<script>
var PUSHER_CONFIG = {
APP_KEY: '<%= @app_key %>'
};
</script>
<script src="js/app.js"></script>
</body>
</html>
<div>
<h3>SMS</h3>
<ul id="sms_messages"></ul>
</div>
<div>
<h3>Incoming Calls</h3>
<ul id="incoming_calls"></ul>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment