I have been using the twilio api for 3 years to send messages and it worked perfectly. After upgrading from ruby 2.6 to ruby 3.1.3, Twilio is no longer working. We are using twilio-ruby 5.74.2. We are getting the error ArgumentError (wrong number of arguments (given 1, expected 0)):
. I have the following code:
def send_pin_through_twilio
client = Twilio::REST::Client.new
client.messages.create({
from: ENV["twilio_phone_number"],
to: self.phone_number,
body: "Hello from ...! Your one-time pin is #{self.one_time_pin}."
})
end
We get the following error:
2023-02-03T17:11:25.312087 00:00 app[web.2]: F, [2023-02-03T17:11:25.311995 #4] FATAL -- : [0d0b1732-1dbc-4170-a3d3-ba99bb7a676e]
2023-02-03T17:11:25.312089 00:00 app[web.2]: [0d0b1732-1dbc-4170-a3d3-ba99bb7a676e] ArgumentError (wrong number of arguments (given 1, expected 0)):
2023-02-03T17:11:25.312089 00:00 app[web.2]: [0d0b1732-1dbc-4170-a3d3-ba99bb7a676e]
2023-02-03T17:11:25.312090 00:00 app[web.2]: [0d0b1732-1dbc-4170-a3d3-ba99bb7a676e] app/models/user.rb:157:in `send_pin_through_twilio'
2023-02-03T17:11:25.312090 00:00 app[web.2]: [0d0b1732-1dbc-4170-a3d3-ba99bb7a676e] app/controllers/api/v3/users_controller.rb:127:in `login_next'
2023-02-03T17:11:25.320370 00:00 heroku[router]: at=info method=PUT path="/api/v3/users/login_next" host=app.hiwave.co request_id=0d0b1732-1dbc-4170-a3d3-ba99bb7a676e fwd="73.179.143.247,108.162.212.48" dyno=web.2 connect=0ms service=2790ms status=500 bytes=307 protocol=https
Here is our initializer for reference:
Twilio.configure do |config|
config.account_sid = ENV["accountsid"]
config.auth_token = ENV["authtoken"]
end
How do I fix this?
CodePudding user response:
Change this:
client.messages.create({
from: ENV["twilio_phone_number"],
to: self.phone_number,
body: "Hello from ...! Your one-time pin is #{self.one_time_pin}."
})
To this:
client.messages.create(
from: ENV["twilio_phone_number"],
to: self.phone_number,
body: "Hello from ...! Your one-time pin is #{self.one_time_pin}."
)
Note that the second example is not a hash, it is using keyword arguments.
This is caused by the separation of positional and keyword arguments in Ruby 3.
If you had updated to Ruby 2.7 then you would have received a warning about this rather than an error. By skipping from 2.6 to 3 you missed the warning and went straight to an error.
CodePudding user response:
I'd say that's because you're using a hash as parameter while the method is now waiting for keyword arguments, try to replace the method call with this line:
client.messages.create(from: ENV["twilio_phone_number"], to: self.phone_number, body: "Hello from ...! Your one-time pin is #{self.one_time_pin}.")