Skip to content

Instantly share code, notes, and snippets.

@oogali
Created August 1, 2012 02:47
Show Gist options
  • Save oogali/3223136 to your computer and use it in GitHub Desktop.
Save oogali/3223136 to your computer and use it in GitHub Desktop.
Lightweight incoming fax handler via Phaxio
#!/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