mirror of
https://github.com/edcommonwealth/sqm-dashboards.git
synced 2026-03-07 21:48:16 -08:00
working on twilio integration
This commit is contained in:
parent
05092fbce9
commit
c8946a02f4
6 changed files with 20 additions and 6 deletions
|
|
@ -3,7 +3,12 @@ class AttemptsController < ApplicationController
|
|||
protect_from_forgery :except => [:twilio]
|
||||
|
||||
def twilio
|
||||
render plain: params.inspect
|
||||
attempt = Attempt.where(twilio_sid: params[:MessageSid]).first
|
||||
attempt.update_attributes(
|
||||
answer_index: params[:Body].to_i,
|
||||
twilio_details: params.to_h.to_yaml
|
||||
)
|
||||
render plain: attempt.inspect
|
||||
end
|
||||
|
||||
# GET /attempts/1/edit
|
||||
|
|
|
|||
|
|
@ -14,13 +14,13 @@ class Attempt < ApplicationRecord
|
|||
message = client.messages.create(
|
||||
from: twilio_number,
|
||||
to: recipient.phone,
|
||||
body: question.text
|
||||
body: "#{question.text}%0a%0a#{question.option1}: Reply 1%0a#{question.option2}: Reply 2%0a#{question.option3}: Reply 3%0a#{question.option4}: Reply 4%0a#{question.option5}: Reply 5"
|
||||
)
|
||||
|
||||
puts message.inspect
|
||||
puts message.try(:Sid)
|
||||
puts message.try(:path)
|
||||
|
||||
update_attributes(sent_at: Time.new)
|
||||
update_attributes(sent_at: Time.new, twilio_sid: message.path.split('/').last)
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
6
db/migrate/20170310191800_add_twilio_sid_to_attempt.rb
Normal file
6
db/migrate/20170310191800_add_twilio_sid_to_attempt.rb
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
class AddTwilioSidToAttempt < ActiveRecord::Migration[5.0]
|
||||
def change
|
||||
add_column :attempts, :twilio_sid, :string
|
||||
add_index :attempts, :twilio_sid
|
||||
end
|
||||
end
|
||||
|
|
@ -10,7 +10,7 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 20170309191211) do
|
||||
ActiveRecord::Schema.define(version: 20170310191800) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
|
|
@ -28,6 +28,8 @@ ActiveRecord::Schema.define(version: 20170309191211) do
|
|||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.text "twilio_details"
|
||||
t.string "twilio_sid"
|
||||
t.index ["twilio_sid"], name: "index_attempts_on_twilio_sid", using: :btree
|
||||
end
|
||||
|
||||
create_table "categories", force: :cascade do |t|
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ RSpec.describe Attempt, type: :model do
|
|||
|
||||
it 'should contact the Twilio API' do
|
||||
expect(FakeSMS.messages.length).to eq(1)
|
||||
expect(FakeSMS.messages.first.body).to eq(question.text)
|
||||
expect(FakeSMS.messages.first.body).to eq('Question 0:1%0a%0aOption 0:1 A: Reply 1%0aOption 0:1 B: Reply 2%0aOption 0:1 C: Reply 3%0aOption 0:1 D: Reply 4%0aOption 0:1 E: Reply 5')
|
||||
expect(FakeSMS.messages.first.to).to eq(recipient.phone)
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -119,6 +119,7 @@ class FakeSMS
|
|||
|
||||
def create(from:, to:, body:)
|
||||
self.class.messages << Message.new(from, to, body)
|
||||
return Struct.new(:path).new('/path')
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue