Created
August 1, 2012 02:47
-
-
Save oogali/3223136 to your computer and use it in GitHub Desktop.
Lightweight incoming fax handler via Phaxio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'sinatra' | |
require 'json' | |
require 'active_support/core_ext/object' | |
require 'pony' | |
FROM_ADDRESS = '[email protected]' | |
ROUTING = { | |
'2035551212' => '[email protected]', | |
:default => '[email protected]' | |
} | |
set :server, :thin | |
set :port, 1534 | |
post '/incoming' do | |
unless not params[:direction].blank? and not params[:fax].blank? | |
halt 403, 'Missing required object information' | |
end | |
incoming = params[:direction] == 'received' | |
fax = JSON.parse params[:fax] | |
metadata = params[:metadata].blank? ? nil : JSON.parse(params[:metadata]) | |
fax['completed_at'] = Time.at(fax['completed_at']) if fax['status'] == 'success' | |
Pony.mail( | |
:via => :sendmail, | |
:from => FROM_ADDRESS, | |
:to => (ROUTING[fax['to_number']] || ROUTING[:default]), | |
:subject => "Incoming Fax from #{fax['from_number']}, #{fax['num_pages']} page(s)", | |
:body => "Don't you love archaic technologies?", | |
:attachments => { params['filename'][:filename] => params['filename'][:tempfile].read } | |
) | |
"OK\n" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment