-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
75 lines (62 loc) · 1.64 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
require 'rubygems'
require 'bundler'
Bundler.require
disable :protection
set :root, File.dirname(__FILE__)
configure :development do
Mail.defaults do
delivery_method :smtp, address: 'localhost', port: 1025
end
end
configure :production do
Mail.defaults do
delivery_method :smtp,
address: ENV['SMTP_SERVER'],
port: 587,
user_name: ENV['SMTP_USERNAME'],
password: ENV['SMTP_PASSWORD'],
authentication: 'plain',
enable_starttls_auto: true
end
end
get '/' do
# [200, {}, 'OK']
[200, { 'SERVER' => 'HOOKSY & CAFFEINE' }, 'OK']
end
post '/twilio/mailer' do
sender = params['From']
receipient = params['To']
message = params['Body']
subject = "SMS received from #{sender} to #{receipient}"
if ENV['DEBUG']
lines = []
request.params.keys.each do |k|
lines << "\n#{k} = #{request.params[k]}"
end
lines << "\n"
lines << JSON.pretty_generate(request.env)
message += "\n\n" + lines.join("\n")
end
channels = ENV.fetch('CHANNELS', '').split(',')
if channels.include?('EMAIL')
Mail.deliver do
from ENV['EMAIL_FROM']
to ENV['EMAIL_TO']
subject subject
body message
end
end
if channels.include?('SLACK')
Slack.configure { |config| config.token = ENV['SLACK_API_TOKEN'] }
client = Slack::Web::Client.new
client.chat_postMessage(channel: ENV['SLACK_CHANNEL'], text: "#{subject}: \n#{message}", as_user: true)
end
[
200,
{
'SERVER' => 'HOOKSY & CAFFEINE',
'Content-type' => 'text/xml'
},
'<Response></Response>'
]
end