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
attempt = recipient.attempts.last_sent.first
if (twilio_params[:Body].downcase == 'stop')
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
all_twilio_details = (attempt.twilio_details || '').split('~!~')
all_twilio_details << twilio_params.to_h.to_yaml
attempt.save_response(
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.'
return
end

@ -105,47 +105,77 @@ RSpec.describe AttemptsController, type: :controller do
end
end
context 'with stop params' do
let(:twilio_attributes) {
{'MessageSid' => 'ewuefhwieuhfweiuhfewiuhf','AccountSid' => 'wefiuwhefuwehfuwefinwefw','MessagingServiceSid' => 'efwneufhwuefhweiufhiuewhf','From' => '+0000000000','To' => '2223334444','Body' => 'sToP','NumMedia' => '0'}
}
['stOp', 'cANcel', 'QuIt', 'no'].each do |command|
context "with #{command} command" do
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
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 "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.')
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
context 'with skip params' do
let(:twilio_skip_attributes) {
{'MessageSid' => 'ewuefhwieuhfweiuhfewiuhf','AccountSid' => 'wefiuwhefuwehfuwefinwefw','MessagingServiceSid' => 'efwneufhwuefhweiufhiuewhf','From' => '+0000000000','To' => '2223334444','Body' => 'SkIP','NumMedia' => '0'}
}
['staRt', 'reSUme', 'rEstaRt', 'Yes', 'go'].each do |command|
context "with #{command} command" do
before :each do
attempt.recipient.update(opted_out: true)
end
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
let(:twilio_attributes) {
{'MessageSid' => 'ewuefhwieuhfweiuhfewiuhf','AccountSid' => 'wefiuwhefuwehfuwefinwefw','MessagingServiceSid' => 'efwneufhwuefhweiufhiuewhf','From' => '+0000000000','To' => '2223334444','Body' => command,'NumMedia' => '0'}
}
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)
it "updates the last attempt by recipient phone number" do
expect(attempt.recipient).to be_opted_out
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_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
it "sends back a message" do
post :twilio, params: twilio_skip_attributes
expect(response.body).to eq('Thank you, this question has been skipped.')
['skip', 'i dont know', "i don't know", 'next'].each do |command|
context "with #{command} command" do
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

Loading…
Cancel
Save