-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.rb
More file actions
128 lines (108 loc) · 3.1 KB
/
server.rb
File metadata and controls
128 lines (108 loc) · 3.1 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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/ruby
require 'sinatra'
require 'sinatra/reloader' if development?
require './database'
require './MessageCarrierSecretKeys'
require 'builder'
require 'twiliolib'
require 'yajl'
require 'net/smtp'
require 'twitter'
TWILIO_API_V = '2010-04-01'
$keys = MessageCarrierSecretKeys.new()
Twitter.configure do |config|
config.consumer_key = $keys.TWITTER_CONSUMER_KEY
config.consumer_secret = $keys.TWITTER_CONSUMER_SECRET
config.oauth_token = $keys.TWITTER_OATH_TOKEN
config.oauth_token_secret = $keys.TWITTER_OATH_TOKEN_SECRET
end
get "/" do
erb :index
end
post "/messages" do
content_type :json
statuses = { }
messages = Yajl::Parser.parse(request.body.read)
unless messages.is_a?(Array)
messages = [messages]
end
messages.each do |msg|
begin
if database[:messages].where(:messageid => msg['messageid']).first
statuses[msg['messageid']] = :existing
else
if database[:messages].insert(msg)
if msg['messagetype'].to_i == 0
send_sms(msg)
elsif msg['messagetype'].to_i == 1
send_email(msg)
elsif msg['messagetype'].to_i == 2
send_twitter(msg)
else
# TODO: handle unknown message types
end
statuses[msg['messageid']] = :accepted
else
puts "DB error"
statuses[msg['messageid']] = :error
end
end
rescue Exception => e
puts e.inspect
statuses[msg['messageid']] = :error
end
end
Yajl::Encoder.encode(statuses)
end
helpers do
def format_text(msg)
"Emergency msg: #{msg['messagebody']}"
end
def send_sms(msg)
t = {
'From' => $keys.TWILIO_CALLER_ID,
'To' => msg['destination'],
'Body' => format_text(msg)
}
begin
account = Twilio::RestAccount.new($keys.TWILIO_SID, $keys.TWILIO_TOKEN)
resp =
account.request("/#{TWILIO_API_V}/Accounts/#{$keys.TWILIO_SID}/SMS/Messages",
'POST',
t)
ensure
puts "Twilio Response: " + resp.body
end
resp.error! unless resp.kind_of? Net::HTTPSuccess
puts "code: %s\nbody: %s" % [resp.code, resp.body]
true # TODO: handle errors
end
def send_email(msg)
from = $keys.EMAIL_USER + '@' + $keys.EMAIL_HOSTDOMAIN
to = msg['destination']
subject = "[MessageCarrier] Emergency Message"
time = Time.now
emaildate = time.strftime("%a, %d %b %Y %H:%M:%S GMT")
#Compose the message for the email
emailmsg = <<END_OF_MESSAGE
Date: #{emaildate}
From: #{from}
To: #{to}
Subject: #{subject}
You have received a message from an
area experiencing a communications emergency:
#{msg['messagebody']}
Please don't reply to this email, it will not be delivered.
END_OF_MESSAGE
Net::SMTP.start($keys.EMAIL_HOSTDOMAIN, $keys.EMAIL_PORT,
$keys.EMAIL_HOSTDOMAIN, $keys.EMAIL_USER, $keys.EMAIL_PWD,
:plain) do |smtp|
smtp.send_message emailmsg, from, to
end
end
def send_twitter(msg)
latlong = msg['location'].split(',')
Twitter.update(msg['messagebody'], {"lat" => latlong[0],
"long" => latlong[1], "display_coordinates" => "true"})
end
end