2017-09-12 132 views
0

我試圖從twilio API接收短信。我生成了一個單獨的回覆控制器,它不處理我的路由或資源中的任何其他內容。它使用post方法與twilio進行通信。即時得到錯誤:Rails - ArgumentError(錯誤的參數數量(給出1,預期0)):

"ArgumentError (wrong number of arguments (given 1, expected 0)):" 

replycontroller.rb

class ReplyController < ApplicationController 

    require 'twilio-ruby' 

    skip_before_action :verify_authenticity_token 

    def hart1 

    twiml = Twilio::TwiML::Response.new do |r| 
     r.Message 'The Robots are coming! Head for the hills!' 
    end 

    content_type 'text/xml' 
    twiml.text 
    end 

end 

這裏是我的路線

Rails.application.routes.draw do 
    resources :posts 
    resources :phones 
    resources :users 
    root 'home#index' 

    post "/reply/hart1" => "reply#hart1" 

end 

我的感覺,我不當這個路由下。 Heroku控制檯也給了我一個500錯誤,所以我知道這是我可以修復的東西。

+0

我不是很確定,但根據[THIS](https://www.twilio.com/docs/quickstart/ruby/sms/hello-monkey)文檔,您可以嘗試'r.message(body :「機器人來了!去山頭!」)' – Abhi

+0

另外,我覺得'r.Message'應該是'r.message'(小寫)[REF](https://www.twilio.com/ docs/guides/how-to-receive-and-reply-in-ruby) – Abhi

+0

您使用的是哪個版本的Ruby庫? – philnash

回答

1

要發送消息「twillio-紅寶石」你的代碼看起來應該是這樣

# put your own credentials here 
account_sid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' 
auth_token = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy' 

# set up a client to talk to the Twilio REST API 
@client = Twilio::REST::Client.new account_sid, auth_token 

# Send SMS message 
@client.api.account.messages.create(
    from: '+FROMNUMBER', 
    to: '+TONUMBER', 
    body: 'The Robots are coming! Head for the hills!' 
) 

你控制器它看起來像

require 'twilio-ruby' 
class ReplyController < ApplicationController 
    skip_before_action :verify_authenticity_token 

    def hart1 
    send_text_message 
    content_type 'text/xml' 
    end 

    private 

    def send_text_message 
    # put your own credentials here 
    account_sid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' 
    auth_token = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy' 

    # set up a client to talk to the Twilio REST API 
    @client = Twilio::REST::Client.new account_sid, auth_token 

    # Send SMS message 
    @client.api.account.messages.create(
    from: '+FROMNUMBER', 
    to: '+TONUMBER', 
    body: 'The Robots are coming! Head for the hills!' 
    ) 
    end 
end 

的文檔「twillo-紅寶石」是這裏:https://github.com/twilio/twilio-ruby

+0

他試圖[回覆傳入的消息,你可以用TwiML來做](https://www.twilio.com/文檔/ API/twiml/SMS/twilio_request)。 – philnash

0

我結束了路線nzajt提到你發送一個正常的文本,而不是Twilio提到的答覆。路由很好。所有東西都在我的服務器上從twilio打過來,我能夠從其有效載荷中獲得所需的所有參數供使用。多謝你們。

相關問題