سؤال

لا يعمل رمز عينة API Twilio هذا في Rails 3:

#voice_controller.rb

  def reminder
    @postto = BASE_URL + '/directions'

    respond_to do |format|
      format.xml { @postto }
    end
  end

#reminder.xml.builder

xml.instruct!
xml.Response do
xml.Gather(:action => @postto, :numDigits => 1) do
    xml.Say "Hello this is a call from Twilio.  You have an appointment 
        tomorrow at 9 AM."
    xml.Say "Please press 1 to repeat this menu. Press 2 for directions.
        Or press 3 if you are done."
    end
end

أيه أفكار؟

يبدو أن Twilio يقوم بإجراء المكالمة الهاتفية بنجاح (يمكنني رؤية المعلمات برقم هاتفي ، والموقع ، وما إلى ذلك) ولكن بعد ذلك أعد رمز الاستجابة الغامض:

Completed 406 Not Acceptable in 0ms
هل كانت مفيدة؟

المحلول

لا يرسل Twilio رأس HTTP في طلباتها, ، مما يؤدي إلى أن تقرر Rails 3 أنه لا يمكن الاستجابة بنوع محتوى مناسب. أعتقد أن ما يلي سوف يتجول من أجلك رغم ذلك:

# voice_controller.rb

  def reminder
    @postto = BASE_URL + '/directions'

    render :content_type => 'application/xml'
  end

نصائح أخرى

موظف Twilio هنا. كانت هناك مجموعة من التغييرات على القضبان منذ نشر هذا السؤال الأصلي ، وأردت أن أشارك كيف يمكنك معالجة هذه المشكلة باستخدام Rails 4 ، والمخاوف وجوهرة Twilio Ruby.

في عينة الكود أدناه ، أحدد وحدة التحكم في /controllers/voice_controller.rb وتشمل مصدر قلق يسمى webhookable. يتيح لنا الاهتمام القابل للوصول إلى تغليف المنطق المتعلق بـ Twilio Webhooks (تعيين رأس استجابة HTTP على النص/XML ، مما يجعل TWIML ، التحقق من صحة هذه الطلبات تنشأ من Twilio ، إلخ) إلى وحدة نمطية واحدة.

require 'twilio-ruby'

class VoiceController < ApplicationController
  include Webhookable

  after_filter :set_header

  # controller code here

end

القلق نفسه يعيش في /controllers/concerns/webhookable.rb وهو بسيط إلى حد ما. في الوقت الحالي ، يقوم ببساطة بتعيين نوع المحتوى على النص/XML لجميع الإجراءات ويوفر طريقة لتقديم كائن TWIML. لم أقم بتضمين الرمز للتحقق من أن الطلبات نشأ من Twilio ، ولكن سيكون من السهل إضافة:

module Webhookable
    extend ActiveSupport::Concern

    def set_header
      response.headers["Content-Type"] = "text/xml"
    end

    def render_twiml(response)
      render text: response.text
    end

end

أخيرًا ، هذا ما الخاص بك reminder قد يبدو العمل مثل استخدام GEM Twilio لإنشاء TWIML واستخدام القلق لتقديم هذا الكائن كنص:

  def reminder
    response = Twilio::TwiML::Response.new do |r|
      r.Gather :action => BASE_URL + '/directions', :numDigits => 1 do |g|
        g.Say 'Hello this is a call from Twilio.  You have an appointment 
    tomorrow at 9 AM.'
        g.Say 'Please press 1 to repeat this menu. Press 2 for directions.
    Or press 3 if you are done.'
      end
    end

    render_twiml response
  end
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top