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,9 +105,10 @@ RSpec.describe AttemptsController, type: :controller do
end end
end end
context 'with stop params' do ['stOp', 'cANcel', 'QuIt', 'no'].each do |command|
context "with #{command} command" do
let(:twilio_attributes) { let(:twilio_attributes) {
{'MessageSid' => 'ewuefhwieuhfweiuhfewiuhf','AccountSid' => 'wefiuwhefuwehfuwefinwefw','MessagingServiceSid' => 'efwneufhwuefhweiufhiuewhf','From' => '+0000000000','To' => '2223334444','Body' => 'sToP','NumMedia' => '0'} {'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
@ -123,10 +124,38 @@ RSpec.describe AttemptsController, type: :controller do
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
['staRt', 'reSUme', 'rEstaRt', 'Yes', 'go'].each do |command|
context "with #{command} command" do
before :each do
attempt.recipient.update(opted_out: true)
end
let(:twilio_attributes) {
{'MessageSid' => 'ewuefhwieuhfweiuhfewiuhf','AccountSid' => 'wefiuwhefuwehfuwefinwefw','MessagingServiceSid' => 'efwneufhwuefhweiufhiuewhf','From' => '+0000000000','To' => '2223334444','Body' => command,'NumMedia' => '0'}
}
context 'with skip params' do 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
['skip', 'i dont know', "i don't know", 'next'].each do |command|
context "with #{command} command" do
let(:twilio_skip_attributes) { let(:twilio_skip_attributes) {
{'MessageSid' => 'ewuefhwieuhfweiuhfewiuhf','AccountSid' => 'wefiuwhefuwehfuwefinwefw','MessagingServiceSid' => 'efwneufhwuefhweiufhiuewhf','From' => '+0000000000','To' => '2223334444','Body' => 'SkIP','NumMedia' => '0'} {'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
@ -149,6 +178,7 @@ RSpec.describe AttemptsController, type: :controller do
end end
end end
end end
end
describe "POST #twilio with response to repeated question" do describe "POST #twilio with response to repeated question" do
context "with valid params" do context "with valid params" do

Loading…
Cancel
Save