【问题标题】:Rails - ArgumentError (wrong number of arguments (given 1, expected 0)):Rails - ArgumentError(参数数量错误(给定 1,预期为 0)):
【发布时间】:2017-09-12 15:26:50
【问题描述】:

我正在尝试从 twilio API 接收短信。我生成了一个单独的回复控制器,它不处理我的路由或资源中的任何其他内容。它使用 post 方法与 twilio 进行通信。我得到了错误:

"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 错误,所以我知道这对我来说是可以修复的。

【问题讨论】:

  • 我不太确定,但根据THIS 文档,您可以尝试r.message(body: "The Robots are coming! Head for the hills!")
  • 另外,我觉得r.Message应该是r.message(小写)REF
  • 您使用的是哪个版本的 Ruby 库?
  • @philnash ruby​​ 2.3.0p0 & Rails 4.2.4
  • 抱歉,Twilio gem 版本 :)

标签: ruby-on-rails ruby twilio


【解决方案1】:

要使用“twillio-ruby”发送消息,您的代码应如下所示

# 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-ruby”的文档在这里:https://github.com/twilio/twilio-ruby

【解决方案2】:

我最终选择了 nzajt 提到的你发送常规文本而不是 Twilio 提到的回复的路线。路由很好。一切都从 twilio 到达我的服务器上的 post 路由,我能够从其有效负载中获取我想要的所有参数以供使用。多谢你们。

【讨论】:

    猜你喜欢
    • 2023-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    相关资源
    最近更新 更多