-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
36 lines (30 loc) · 890 Bytes
/
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
require 'open-uri'
require 'mime-types'
require 'sinatra'
require 'twilio-ruby'
post '/sms' do
num_media = params['NumMedia'].to_i
if num_media > 0
for i in 0..(num_media - 1) do
# Prepare the file information
media_url = params["MediaUrl#{i}"]
content_type = params["MediaContentType#{i}"]
file_name = media_url.split('/').last
file_extension = MIME::Types[content_type].first.extensions.first
file = "#{file_name}.#{file_extension}"
# Dowload the files
open(media_url) do |url|
File.open(file, 'wb') do |f|
f.write(url.read)
end
end
end
end
# Reply message
twiml = Twilio::TwiML::MessagingResponse.new do |resp|
body = num_media > 0 ? "Thanks for sending us #{num_media} file(s)!" : 'Send us an image!'
resp.message body: body
end
content_type 'text/xml'
twiml.to_s
end