diff --git a/Procfile b/Procfile deleted file mode 100644 index 5c636c6..0000000 --- a/Procfile +++ /dev/null @@ -1 +0,0 @@ -web: bundle exec ruby web.rb -p $PORT diff --git a/README.md b/README.md index 2f80dc4..e4c6a43 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,19 @@ -1. fill in values in config file -2. iron_worker upload send_insanity -3. deploy app to heroku +Setup: -To receive Twilio SMS's back: +sudo bundle install (iron_worker_ng has the iron_worker command line interface) +fill in values in config file +fill in values in workers/iron.json +cd workers +iron_worker upload send_insanity +iron_worker upload twilio_webhook -https://devcenter.heroku.com/articles/ruby +To Run: -Deploy the Heroku app to your own account +ruby web.rb +browse to http://localhost:4567/ +enter your number + + + +https://worker-aws-us-east-1.iron.io/2/projects/{PROJECT_ID}/tasks/webhook?code_name=TwilioWebhook&oauth={TOKEN} diff --git a/config.yml b/config/config.yml similarity index 100% rename from config.yml rename to config/config.yml diff --git a/insanity_schedule.txt b/lists/insanity_schedule.txt similarity index 100% rename from insanity_schedule.txt rename to lists/insanity_schedule.txt diff --git a/shaunisms.txt b/lists/shaunisms.txt similarity index 100% rename from shaunisms.txt rename to lists/shaunisms.txt diff --git a/web.rb b/web.rb index fbd7c99..d0b29c9 100644 --- a/web.rb +++ b/web.rb @@ -41,49 +41,9 @@ def puts(msg) end -get '/receive' do - puts "-------------- RECEIVING REQUEST ---------------" - puts "FROM: #{params[:From]}" - puts "BODY: #{params[:Body]}" - puts "------------------------------------------------" - - number = params[:From].reverse.chop.chop.reverse - - move_day_forward(number) if params[:Body].downcase == "done" -end - private -def move_day_forward(number) - config = YAML.load_file("config.yml") - twilio = Twilio::REST::Client.new config['twilio']['account_sid'], config['twilio']['auth_token'] - - cache = get_cache("insanity-#{number}") - cache.increment("day") - - message = "Great job today. As Shaun T would say \"#{shaunism.chomp}\"" - - puts "Sending Shaunism" - twilio.account.sms.messages.create( - :from => config['app']['from'], - :to => number, - :body => message - ) - puts "Congratulations! We've moved your workout forward one day!" -end - - -def shaunism - all = [] - File.open('shaunisms.txt', 'r') do |f| - while line = f.gets - all << line.chomp - end - end - all[rand(all.size)] -end - def get_cache(name) puts "Creating or Getting Cache...." @@ -95,7 +55,7 @@ def get_cache(name) def load_schedule(cache) puts "Loading Cache Up...." i=0 - File.open('insanity_schedule.txt', 'r') do |f| + File.open('lists/insanity_schedule.txt', 'r') do |f| while line = f.gets cache.put(i.to_s, line) i+=1 diff --git a/workers/send_insanity.worker b/workers/send_insanity.worker index 40b9db2..3c0b0e7 100644 --- a/workers/send_insanity.worker +++ b/workers/send_insanity.worker @@ -5,4 +5,4 @@ name "SendInsanity" gem "twilio-ruby" gem "iron_cache" -file "config.yml" \ No newline at end of file +file "../config/config.yml" \ No newline at end of file diff --git a/workers/twilio_webhook.rb b/workers/twilio_webhook.rb new file mode 100644 index 0000000..95e9bae --- /dev/null +++ b/workers/twilio_webhook.rb @@ -0,0 +1,60 @@ +require 'cgi' +require 'yaml' +require 'iron_cache' +require 'twilio-ruby' + + +parsed = CGI::parse(payload) +from = parsed["From"][0] +body = parsed["Body"][0] + + +puts "-------------- RECEIVING REQUEST ---------------" +puts "PAYLOAD: #{payload}" +puts "Parsed Payload: #{parsed}" +puts "FROM: #{from}" +puts "BODY: #{body}" +puts "------------------------------------------------" + +number = from.reverse.chop.chop.reverse + + +def shaunism + all = [] + File.open('shaunisms.txt', 'r') do |f| + while line = f.gets + all << line.chomp + end + end + all[rand(all.size)] +end + + + +def move_day_forward(number) + puts "Moving Day Forward" + + config = YAML.load_file("config.yml") + twilio = Twilio::REST::Client.new config['twilio']['account_sid'], config['twilio']['auth_token'] + + puts "Creating or Getting Cache...." + ironcache = IronCache::Client.new(:project_id => config['iron']['project_id'], :token => config['iron']['token']) + cache = ironcache.cache("insanity-#{number}") + + puts "Incrementing Day Cache" + cache.increment("day") + + puts "Sending Shaunism" + message = "Great job today. As Shaun T would say \"#{shaunism.chomp}\"" + twilio.account.sms.messages.create( + :from => config['app']['from'], + :to => number, + :body => message + ) +end + + + +move_day_forward(number) if body.downcase == "done" + + diff --git a/workers/twilio_webhook.worker b/workers/twilio_webhook.worker new file mode 100644 index 0000000..ee2cab5 --- /dev/null +++ b/workers/twilio_webhook.worker @@ -0,0 +1,9 @@ +runtime "ruby" +exec "twilio_webhook.rb" +name "TwilioWebhook" + +gem "twilio-ruby" +gem "iron_cache" + +file "../config/config.yml" +file "../lists/shaunisms.txt" \ No newline at end of file