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

0 votes
asked Sep 12, 2017 by james-hart

I'm trying to receive an SMS from the twilio API. I generated a separate reply controller that doesn't deal with anything else in my routes or resources. It uses a post method to communicate with twilio. Im getting the error:

"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

here are my routes

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

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

end

I'm under the impression I'm routing this improperly. The Heroku console also gives me a 500 error so I know it's something fixable on my end.

2 Answers

0 votes
answered Sep 12, 2017 by nzajt

To send a message with 'twillio-ruby' your code should look like this

# 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!'
)

in you controller it would look like

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

The documentation for 'twillo-ruby' is here: https://github.com/twilio/twilio-ruby

0 votes
answered Sep 12, 2017 by james-hart

I wound up going the route nzajt mentioned where you send a regular text rather than the reply Twilio mentions. The routing was fine. Everything hit the post route on my server from twilio and I was able to take all the params I wanted from its payload for use. Thanks guys.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...