Sutto / whitepicketfence

micro sms gateway for the web built with rubygsm, sinatra and a whole bucket of awesomeness.

This URL has Read+Write access

whitepicketfence / incoming.rb
100644 40 lines (32 sloc) 0.844 kb
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
require File.join(File.dirname(__FILE__), "server_tools")
gem 'taf2-curb'
require 'curl'
 
$timeout = (ENV["LOOP_DELAY"] || 5).to_i
 
# loop do
# puts "Sending pending messages..."
# SMS.send_pending($modem)
# sleep $timeout
# end
 
class WPFReceiver
  def initialize(modem)
    modem.receive(method(:incoming))
    @modem = modem
  end
 
  def incoming(from, datetime, message)
    sms = SMS.receive(:contents => message, :number => from)
    dispatch_json({
      "number" => from,
      "contents" => message,
      "id" => sms.id,
      "path" => "/messages/#{sms.id}"
    }.to_json)
  end
  
  protected
  
  def dispatch_json(payload)
    data = Curl::PostField.content('payload', payload)
    settings['hook_urls'].each do |url|
      Curl::Easy.http_post(url, data) rescue nil
    end
  end
  
end
 
WPFReceiver.new($modem)