Skip to content

Commit

Permalink
moving receive to a webhook worker, cleaning up directory structure
Browse files Browse the repository at this point in the history
  • Loading branch information
carimura committed Jul 13, 2012
1 parent 111d8d9 commit 7a099a4
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 49 deletions.
1 change: 0 additions & 1 deletion Procfile

This file was deleted.

21 changes: 15 additions & 6 deletions 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}
File renamed without changes.
File renamed without changes.
File renamed without changes.
42 changes: 1 addition & 41 deletions web.rb
Expand Up @@ -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...."
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion workers/send_insanity.worker
Expand Up @@ -5,4 +5,4 @@ name "SendInsanity"
gem "twilio-ruby"
gem "iron_cache"

file "config.yml"
file "../config/config.yml"
60 changes: 60 additions & 0 deletions 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"


9 changes: 9 additions & 0 deletions 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"

0 comments on commit 7a099a4

Please sign in to comment.