pull/1/head
Jared Cosulich 9 years ago
parent d4b3a83681
commit 2a3b4565d9

@ -4,6 +4,14 @@ class AttemptsController < ApplicationController
def twilio def twilio
attempt = Recipient.where(phone: twilio_params['From']).first.attempts.last attempt = Recipient.where(phone: twilio_params['From']).first.attempts.last
if (twilio_params[:Body].downcase == 'cancel')
attempt.recipient.update_attributes(opted_out: true)
attempt.update_attributes(twilio_details: twilio_params.to_h.to_yaml)
render plain: 'Thank you, you have been opted out of these messages and will no longer receive them.'
return
end
attempt.update_attributes( attempt.update_attributes(
answer_index: twilio_params[:Body].to_i, answer_index: twilio_params[:Body].to_i,
twilio_details: twilio_params.to_h.to_yaml twilio_details: twilio_params.to_h.to_yaml

@ -14,18 +14,28 @@ class Attempt < ApplicationRecord
scope :for_school, -> (school) { joins(:recipient).merge(Recipient.for_school(school)) } scope :for_school, -> (school) { joins(:recipient).merge(Recipient.for_school(school)) }
scope :with_response, -> { where('answer_index is not null or open_response_id is not null')} scope :with_response, -> { where('answer_index is not null or open_response_id is not null')}
def messages
[
question.text,
"#{question.option1}: Reply 1\n\r#{question.option2}: Reply 2\n\r#{question.option3}: Reply 3\n\r#{question.option4}: Reply 4\n\r#{question.option5}: Reply 5\n\rReply 'cancel' to stop these messages."
]
end
def send_message def send_message
twilio_number = ENV['TWILIO_NUMBER'] twilio_number = ENV['TWILIO_NUMBER']
client = Twilio::REST::Client.new ENV['TWILIO_ACCOUNT_SID'], ENV['TWILIO_AUTH_TOKEN'] client = Twilio::REST::Client.new ENV['TWILIO_ACCOUNT_SID'], ENV['TWILIO_AUTH_TOKEN']
message = client.messages.create( sids = []
from: twilio_number, messages.each do |message|
to: recipient.phone, sids << client.messages.create(
body: "#{question.text}\n\r#{question.option1}: Reply 1\n\r#{question.option2}: Reply 2\n\r#{question.option3}: Reply 3\n\r#{question.option4}: Reply 4\n\r#{question.option5}: Reply 5" from: twilio_number,
) to: recipient.phone,
body: message
).sid
end
update_attributes(sent_at: Time.new, twilio_sid: message.sid) update_attributes(sent_at: Time.new, twilio_sid: sids.join(','))
recipient.update_attributes(phone: client.messages.get(message.sid).to) recipient.update_attributes(phone: client.messages.get(sids.last).to)
end end
def response def response

@ -48,5 +48,24 @@ RSpec.describe AttemptsController, type: :controller do
expect(response.body).to eq('Thank you!') expect(response.body).to eq('Thank you!')
end end
end end
context 'with cancel params' do
let(:twilio_attributes) {
{'MessageSid' => 'ewuefhwieuhfweiuhfewiuhf','AccountSid' => 'wefiuwhefuwehfuwefinwefw','MessagingServiceSid' => 'efwneufhwuefhweiufhiuewhf','From' => '+11231231234','To' => '2223334444','Body' => 'cAnCel','NumMedia' => '0'}
}
it "updates the last attempt by recipient phone number" do
post :twilio, params: twilio_attributes
attempt.reload
expect(attempt.answer_index).to be_nil
expect(attempt.twilio_details).to eq(twilio_attributes.with_indifferent_access.to_yaml)
expect(attempt.recipient).to be_opted_out
end
it "sends back a message" do
post :twilio, params: twilio_attributes
expect(response.body).to eq('Thank you, you have been opted out of these messages and will no longer receive them.')
end
end
end end
end end

@ -92,8 +92,9 @@ RSpec.describe Attempt, type: :model do
end end
it 'should contact the Twilio API' do it 'should contact the Twilio API' do
expect(FakeSMS.messages.length).to eq(1) expect(FakeSMS.messages.length).to eq(2)
expect(FakeSMS.messages.first.body).to eq("Question 0:1\n\rOption 0:1 A: Reply 1\n\rOption 0:1 B: Reply 2\n\rOption 0:1 C: Reply 3\n\rOption 0:1 D: Reply 4\n\rOption 0:1 E: Reply 5") expect(FakeSMS.messages.first.body).to eq("Question 0:1")
expect(FakeSMS.messages.last.body).to eq("Option 0:1 A: Reply 1\n\rOption 0:1 B: Reply 2\n\rOption 0:1 C: Reply 3\n\rOption 0:1 D: Reply 4\n\rOption 0:1 E: Reply 5\n\rReply 'cancel' to stop these messages.")
expect(FakeSMS.messages.first.to).to eq('1111111111') expect(FakeSMS.messages.first.to).to eq('1111111111')
end end

Loading…
Cancel
Save