-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.rb
310 lines (265 loc) · 6.45 KB
/
app.rb
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
require 'sinatra'
if Sinatra::Application.environment == :development
require 'dotenv/load'
require 'pry'
end
require "sinatra/activerecord"
require 'yaml'
require 'haml'
require 'open-uri'
require 'twilio-ruby'
require 'mailgun-ruby'
require 'rack-google-analytics'
require 'sinatra/flash'
#Constances for ENV
DB_HOST = ENV['DB_HOST']
DB_PORT = ENV['DB_PORT']
DB_USERNAME = ENV['DB_USERNAME']
DB_PASSWORD = ENV['DB_PASSWORD']
DB_NAME = ENV['DB_NAME']
SESSION_SECRET = ENV["SESSION_SECRET"]
TWILIO_NUMBER = ENV['TWILIO_NUMBER']
MY_NUMBER = ENV['MY_NUMBER']
ACCOUNT_SID = ENV['ACCOUNT_SID']
AUTH_TOKEN = ENV['AUTH_TOKEN']
MY_EMAIL = ENV['MY_EMAIL']
GA_TRACKER = ENV['GA_TRACKER']
MG_KEY = ENV['MG_KEY']
MG_SENDING_DOMAIN = ENV['MG_SENDING_DOMAIN']
MG_FROM_EMAIL = ENV['MG_FROM_EMAIL']
#Confirm ENV
#Enable Sessions
enable :sessions
set :session_secret, SESSION_SECRET
#Setup Google Analytics
use Rack::GoogleAnalytics, :tracker => GA_TRACKER
#Setup DBs
set :database_file, 'config/database.yml'
if Sinatra::Application.environment == :production
ActiveRecord::Base.establish_connection(ENV['DATABASE_URL'])
else
db_option = 'postgres://' + DB_USERNAME + ':' + DB_PASSWORD + '@' + DB_HOST + ":" + DB_PORT + '/' + DB_NAME
ActiveRecord::Base.establish_connection(db_option)
end
#Require Models
current_dir = Dir.pwd
Dir["#{current_dir}/models/*.rb"].each { |file| require file }
#Helpers & AUTH
helpers do
def title
if @title
"#{@title}"
else
"Sinatra Broadcast: Fly Them to the Moon"
end
end
def current_user
User.find(session[:user_id])
end
def logged_in?
!!session[:user_id]
end
def redirect_if_not_logged_in
if !logged_in?
redirect "/login"
end
end
end
# First, instantiate the Twillio Client with your API Creds
client = Twilio::REST::Client.new ACCOUNT_SID, AUTH_TOKEN
# Public Routes
get '/' do
if !logged_in?
@broadcasts = Broadcast.order('updated_at DESC')
@title = "Sinatra Broadcast: Fly Them to the Moon"
haml :index
else
@broadcasts = Broadcast.order('updated_at DESC')
@title = "Sinatra Broadcast: Fly Them to the Moon"
haml :index
end
end
get '/login' do
@title = 'Sinatra Broadcast Login'
haml :login
end
post '/login' do
@user = User.find_by(username:params[:username])
if @user && @user.authenticate(params[:password])
session[:user_id] = @user.id
redirect "/send"
else
flash[:message] = "We can't your login credentials. Please try again"
redirect_if_not_logged_in
end
end
get '/logout' do
if logged_in?
session.destroy
redirect to '/login'
else
redirect to '/'
end
end
get '/broadcasts/:id' do
@broadcast = Broadcast.find(params[:id])
haml :single_broadcast
end
get '/about' do
@title = "About Sinatra Broadcast"
haml :about
end
get '/subscribe' do
@title = "Subscribe to Sinatra Broadcast"
haml :subscribe
end
post '/subscribe' do
name = params[:name]
email = params[:email]
body = params[:body]
phone_number = params[:phone_number]
# First, instantiate the Mailgun Client with your API key
mg_client = Mailgun::Client.new MG_KEY
# Define your message parameters
message_params = {
:from => 'subscribe@mg.example.com',
:to => MY_EMAIL,
:subject => "#{name} Would Like to Receive Broadcasts",
:text => "Please add #{phone_number} to the sinatra broadcast. I would like to be added because #{body}",
}
# Send your message through the client
mg_client.send_message MG_SENDING_DOMAIN, message_params
haml :subscribe
redirect '/thanks'
end
get '/thanks' do
@title = "Thanks for Subscribing!"
haml :thanks
end
# Protected Routes
get '/broadcasts' do
if logged_in?
@broadcasts = Broadcast.order('updated_at DESC')
@title = "Broadcasts We Have Sent Out."
haml :broadcasts
else
redirect_if_not_logged_in
end
end
delete '/broadcasts/:id' do
if logged_in?
@broadcast = Broadcastl.find_by_id(params[:id])
if @broadcast
@broadcast.destroy
else
halt 404, "Broadcast not found"
end
200
redirect '/broadcastss'
else
redirect_if_not_logged_in
end
end
get '/contacts' do
if logged_in?
@contacts = Contact.order(:name)
@title = "Contacts We Send To."
haml :contacts
else
redirect_if_not_logged_in
end
end
post '/contacts' do
if logged_in?
@contacts = Contact.create(name: params[:name], phone_number: params[:phone_number])
redirect '/contacts'
else
redirect_if_not_logged_in
end
end
get '/contacts/:id' do
if logged_in?
@contact = Contact.find(params[:id])
haml :single_contact
else
redirect_if_not_logged_in
end
end
delete '/contacts/:id' do
if logged_in?
@contact = Contact.find_by_id(params[:id])
if @contact
@contact.destroy
else
halt 404, "Contact not found"
end
200
redirect '/contacts'
else
redirect_if_not_logged_in
end
end
get '/send' do
if logged_in?
@title = "Send Your Broadcast."
haml :send
else
redirect_if_not_logged_in
end
end
post '/send' do
if logged_in?
#Save Broadcast to DB
@broadcast = Broadcast.create(from: params[:from],subject: params[:subject], body: params[:body])
#Send Saved Broadcast to MMS Twilio
Contact.all.each do |contact|
client.messages.create(
from: TWILIO_NUMBER,
to: contact.phone_number,
body: @broadcast.body.to_s,
media_url: '' #Need Images to get MMS to Join in Order, Not needed for SMS
)
end
redirect '/'
else
redirect_if_not_logged_in
end
end
get '/message' do
if logged_in?
"Things are Working!"
else
redirect_if_not_logged_in
end
end
# MMS Routing using API REST, Modify for SMS Routing
post '/message' do
if logged_in?
from = params['From']
body = params['Body']
media_url = params['MediaUrl0']
if from == MY_NUMBER
Contact.all.each do |contact|
message_params = {
from: TWILIO_NUMBER,
to: contact.phone_number,
body: body.to_s
}
message_params [:media_url] = media_url if media_url.present?
client.messages.create(message_params)
end
else
contact = Contact.where(phone_number: from).first!
body = "#{contact.name} (#{from}): \n#{body}"
message_params = {
from: TWILIO_NUMBER,
to: MY_NUMBER,
body: body.to_s
}
message_params [:media_url] = media_url if media_url.present?
client.messages.create(message_params)
end
else
redirect_if_not_logged_in
end
end