2017-05-17 5 views
0

Ich versuche ein Beispiel Twilio Rails Projekt einzurichten, das eine Person anruft. Ich befolge das mit this repo verbundene Tutorial und habe grundsätzlich eine Durchschrift der Codebasis. Ich bekomme einen Fehler, der meiner Meinung nach von dieser Zeile stammt @validator = Twilio::Util::RequestValidator.new(@@twilio_token).NoMethodError: undefined Methode `sort` aus Twilio Sprachbeispiel

Hier ist mein twilio_controller.rb

class TwilioController < ApplicationController 
    # Before we allow the incoming request to connect, verify 
    # that it is a Twilio request 
    skip_before_action :verify_authenticity_token 
    before_action :authenticate_twilio_request, :only => [ 
    :connect 
    ] 

    @@twilio_sid = ENV['TWILIO_ACCOUNT_SID'] 
    @@twilio_token = ENV['TWILIO_AUTH_TOKEN'] 
    @@twilio_number = ENV['TWILIO_NUMBER'] 
    @@api_host = ENV['TWILIO_HOST'] 

    # Render home page 
    def index 
    render 'index' 
    end 

    def voice 
    response = Twilio::TwiML::Response.new do |r| 
     r.Say "Yay! You're on Rails!", voice: "alice" 
     r.Sms "Well done building your first Twilio on Rails 5 app!" 
    end 
    render :xml => response.to_xml 
    end 

    # Handle a POST from our web form and connect a call via REST API 
    def call 
    contact = Contact.new 
    contact.user_phone = params[:userPhone] 
    contact.sales_phone = params[:salesPhone] 

    # Validate contact 
    if contact.valid? 

     @client = Twilio::REST::Client.new @@twilio_sid, @@twilio_token 
     # Connect an outbound call to the number submitted 
     @call = @client.calls.create(
     :from => @@twilio_number, 
     :to => contact.user_phone, 
     :url => "#{@@api_host}/connect/#{contact.encoded_sales_phone}" # Fetch instructions from this URL when the call connects 
    ) 

     # Let's respond to the ajax call with some positive reinforcement 
     @msg = { :message => 'Phone call incoming!', :status => 'ok' } 

    else 

     # Oops there was an error, lets return the validation errors 
     @msg = { :message => contact.errors.full_messages, :status => 'ok' } 
    end 
    respond_to do |format| 
     format.json { render :json => @msg } 
    end 
    end 

    # This URL contains instructions for the call that is connected with a lead 
    # that is using the web form. 
    def connect 
    # Our response to this request will be an XML document in the "TwiML" 
    # format. Our Ruby library provides a helper for generating one 
    # of these documents 
    response = Twilio::TwiML::Response.new do |r| 
     r.Say 'FUCK.', :voice => 'alice' 
     # r.Dial params[:sales_number] 
    end 
    render text: response.text 
    end 


    # Authenticate that all requests to our public-facing TwiML pages are 
    # coming from Twilio. Adapted from the example at 
    # http://twilio-ruby.readthedocs.org/en/latest/usage/validation.html 
    # Read more on Twilio Security at https://www.twilio.com/docs/security 
    private 
    def authenticate_twilio_request 
    twilio_signature = request.headers['HTTP_X_TWILIO_SIGNATURE'] 
    # Helper from twilio-ruby to validate requests. 
    @validator = Twilio::Util::RequestValidator.new(@@twilio_token) 

    # the POST variables attached to the request (eg "From", "To") 
    # Twilio requests only accept lowercase letters. So scrub here: 
    post_vars = params.reject {|k, v| k.downcase == k} 

    is_twilio_req = @validator.validate(request.url, post_vars, twilio_signature) 

    unless is_twilio_req 
     render :xml => (Twilio::TwiML::Response.new {|r| r.Hangup}).text, :status => :unauthorized 
     false 
    end 
    end 

end 

Fehlerbild:

enter image description here

I ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin15] und Rails 5.1.0 verwenden.

+0

Er scheiterte an der Linie 85 nach innen 'authenticate_twilio_request', für nicht definierte Methode' sort'. Wenn Sie Ihren Code betrachten, haben Sie 'sort' nicht in' authenticate_twilio_request' verwendet, haben Sie vielleicht Ihren Code oben bearbeitet? –

+0

Scheint so, als ob 'ActionController :: Parameters # sort' in Rails 5.1 bereits entfernt wurde, weil "ActionController :: Parameters' nicht mehr vom Hash-Code erbt", als ich in Rails 5.0.3' sortieren 'wollte und bekam Verwarnungswarnung. –

+0

Bei weiterer Überprüfung, weil ich keine Spur von "Sortieren" auf Ihrem Code oben sehen konnte, fand ich die Zeile, die den Fehler verursacht von https://github.com/twilio/twilio-ruby/blob/ac2680cde6913af94e24fb24ee27bb28ffa2602b/lib /twilio-ruby/util/request_validator.rb:'data = url + params.sort.join', das schließlich von Ihrem 'is_twilio_req = @ validator.validate (request.url, post_vars, twillio_signature) aufgerufen wird, siehe meine Antwort unten für eine Reparatur. –

Antwort

3

Ihr Code wird höchstwahrscheinlich bei is_twilio_req = @validator.validate(request.url, post_vars, twilio_signature) scheitern, weil bei einer Überprüfung der gem's code, wird bei sort andernfalls unter

data = url + params.sort.join

Dies liegt daran, in Rails 5.1, ActionController::Parameters nicht mehr von Hash erbt, so Hash Methoden wie sort (see Hash docs) wird nicht mehr funktionieren.

Sie müssen params in Hash konvertieren explizit:

def authenticate_twilio_request 
    twilio_signature = request.headers['HTTP_X_TWILIO_SIGNATURE'] 
    @validator = Twilio::Util::RequestValidator.new(@@twilio_token) 

    # convert `params` which is an `ActionController::Parameters` object into `Hash` 
    # you will need `permit!` to strong-params-permit EVERYTHING so that they will be included in the converted `Hash` (you don't need to specifically whitelist specific parameters for now as the params are used by the Twilio gem) 
    params_hash = params.permit!.to_hash 

    post_vars = params_hash.reject {|k, v| k.downcase == k} 

    is_twilio_req = @validator.validate(request.url, post_vars, twilio_signature) 

    unless is_twilio_req 
    render :xml => (Twilio::TwiML::Response.new {|r| r.Hangup}).text, :status => :unauthorized 
    false 
    end 
end 
+0

danke! Ich werde es versuchen, wenn ich von der Arbeit nach Hause komme – Sticky

+1

funktioniert, danke! : D – Sticky

+0

@Sticky Kein Problem! :) –

Verwandte Themen