more natural language commands

pull/1/head
Jared Cosulich 9 years ago
parent ed4883c9fb
commit 628891f33a

@ -6,19 +6,27 @@ class AttemptsController < ApplicationController
recipient = Recipient.where(phone: twilio_params['From']).first recipient = Recipient.where(phone: twilio_params['From']).first
attempt = recipient.attempts.last_sent.first attempt = recipient.attempts.last_sent.first
if (twilio_params[:Body].downcase == 'stop') all_twilio_details = (attempt.twilio_details || '').split('~!~')
attempt.recipient.update_attributes(opted_out: true) all_twilio_details << twilio_params.to_h.to_yaml
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.save_response( attempt.save_response(
answer_index: twilio_params[:Body].to_i > 0 ? twilio_params[:Body].to_i : nil, answer_index: twilio_params[:Body].to_i > 0 ? twilio_params[:Body].to_i : nil,
twilio_details: twilio_params.to_h.to_yaml twilio_details: all_twilio_details.join('~!~')
) )
if (twilio_params[:Body].downcase == 'skip') unless (['start', 'resume', 'restart', 'yes', 'go'].index(twilio_params[:Body].downcase).nil?)
recipient.update_attributes(opted_out: false)
render plain: 'Thank you, you will now begin receiving messages again.'
return
end
unless (['stop', 'cancel', 'quit', 'no'].index(twilio_params[:Body].downcase).nil?)
recipient.update_attributes(opted_out: true)
render plain: 'Thank you, you have been opted out of these messages and will no longer receive them.'
return
end
unless (['skip', 'i dont know', "i don't know", 'next'].index(twilio_params[:Body].downcase).nil?)
render plain: 'Thank you, this question has been skipped.' render plain: 'Thank you, this question has been skipped.'
return return
end end

@ -105,47 +105,77 @@ RSpec.describe AttemptsController, type: :controller do
end end
end end
context 'with stop params' do ['stOp', 'cANcel', 'QuIt', 'no'].each do |command|
let(:twilio_attributes) { context "with #{command} command" do
{'MessageSid' => 'ewuefhwieuhfweiuhfewiuhf','AccountSid' => 'wefiuwhefuwehfuwefinwefw','MessagingServiceSid' => 'efwneufhwuefhweiufhiuewhf','From' => '+0000000000','To' => '2223334444','Body' => 'sToP','NumMedia' => '0'} let(:twilio_attributes) {
} {'MessageSid' => 'ewuefhwieuhfweiuhfewiuhf','AccountSid' => 'wefiuwhefuwehfuwefinwefw','MessagingServiceSid' => 'efwneufhwuefhweiufhiuewhf','From' => '+0000000000','To' => '2223334444','Body' => command,'NumMedia' => '0'}
}
it "updates the last attempt by recipient phone number" do it "updates the last attempt by recipient phone number" do
post :twilio, params: twilio_attributes post :twilio, params: twilio_attributes
attempt.reload attempt.reload
expect(attempt.answer_index).to be_nil expect(attempt.answer_index).to be_nil
expect(attempt.twilio_details).to eq(twilio_attributes.with_indifferent_access.to_yaml) expect(attempt.twilio_details).to eq(twilio_attributes.with_indifferent_access.to_yaml)
expect(attempt.recipient).to be_opted_out expect(attempt.recipient).to be_opted_out
end end
it "sends back a message" do it "sends back a message" do
post :twilio, params: twilio_attributes 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.') 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
context 'with skip params' do ['staRt', 'reSUme', 'rEstaRt', 'Yes', 'go'].each do |command|
let(:twilio_skip_attributes) { context "with #{command} command" do
{'MessageSid' => 'ewuefhwieuhfweiuhfewiuhf','AccountSid' => 'wefiuwhefuwehfuwefinwefw','MessagingServiceSid' => 'efwneufhwuefhweiufhiuewhf','From' => '+0000000000','To' => '2223334444','Body' => 'SkIP','NumMedia' => '0'} before :each do
} attempt.recipient.update(opted_out: true)
end
it "updates the last attempt by recipient phone number" do let(:twilio_attributes) {
post :twilio, params: twilio_skip_attributes {'MessageSid' => 'ewuefhwieuhfweiuhfewiuhf','AccountSid' => 'wefiuwhefuwehfuwefinwefw','MessagingServiceSid' => 'efwneufhwuefhweiufhiuewhf','From' => '+0000000000','To' => '2223334444','Body' => command,'NumMedia' => '0'}
attempt.reload }
expect(attempt.answer_index).to be_nil
expect(attempt.responded_at).to be_present
expect(attempt.twilio_details).to eq(twilio_skip_attributes.with_indifferent_access.to_yaml)
expect(attempt.recipient).to_not be_opted_out
school_attempts = attempt.question.attempts.for_school(school) it "updates the last attempt by recipient phone number" do
expect(school_attempts.with_answer.count).to eq(0) expect(attempt.recipient).to be_opted_out
expect(school_attempts.with_no_answer.count).to eq(3) post :twilio, params: twilio_attributes
expect(school_attempts.not_yet_responded.count).to eq(2) 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_not be_opted_out
end
it "sends back a message" do
post :twilio, params: twilio_attributes
expect(response.body).to eq('Thank you, you will now begin receiving messages again.')
end
end end
end
it "sends back a message" do ['skip', 'i dont know', "i don't know", 'next'].each do |command|
post :twilio, params: twilio_skip_attributes context "with #{command} command" do
expect(response.body).to eq('Thank you, this question has been skipped.') let(:twilio_skip_attributes) {
{'MessageSid' => 'ewuefhwieuhfweiuhfewiuhf','AccountSid' => 'wefiuwhefuwehfuwefinwefw','MessagingServiceSid' => 'efwneufhwuefhweiufhiuewhf','From' => '+0000000000','To' => '2223334444','Body' => command,'NumMedia' => '0'}
}
it "updates the last attempt by recipient phone number" do
post :twilio, params: twilio_skip_attributes
attempt.reload
expect(attempt.answer_index).to be_nil
expect(attempt.responded_at).to be_present
expect(attempt.twilio_details).to eq(twilio_skip_attributes.with_indifferent_access.to_yaml)
expect(attempt.recipient).to_not be_opted_out
school_attempts = attempt.question.attempts.for_school(school)
expect(school_attempts.with_answer.count).to eq(0)
expect(school_attempts.with_no_answer.count).to eq(3)
expect(school_attempts.not_yet_responded.count).to eq(2)
end
it "sends back a message" do
post :twilio, params: twilio_skip_attributes
expect(response.body).to eq('Thank you, this question has been skipped.')
end
end end
end end
end end

Loading…
Cancel
Save