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?
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.
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}.")
Related
I am working on deploying a rails 5 api. After much trail error its almost functioning. The problem I am having is that I am running into a 500 server error. I sshed into the instance to check the logs and I keep getting this name error. The log output:
[2017-11-14T15:41:47.289647 #10484] INFO -- : [6d21f1c2-6770-42a2-93be-cd68ee0441fc] Completed 500 Internal Server Error in 244ms (Views: 0.2ms | ActiveRecord: 3.6ms)
F, [2017-11-14T15:41:47.290161 #10484] FATAL -- : [6d21f1c2-6770-42a2-93be-cd68ee0441fc]
F, [2017-11-14T15:41:47.290225 #10484] FATAL -- : [6d21f1c2-6770-42a2-93be-cd68ee0441fc] NameError (uninitialized constant Net::HTTP):
F, [2017-11-14T15:41:47.290245 #10484] FATAL -- : [6d21f1c2-6770-42a2-93be-cd68ee0441fc]
F, [2017-11-14T15:41:47.290263 #10484] FATAL -- : [6d21f1c2-6770-42a2-93be-cd68ee0441fc] app/models/user.rb:41:in `authorization'
[6d21f1c2-6770-42a2-93be-cd68ee0441fc] app/models/user.rb:17:in `create_new_auth_token'
I thought I forgot to require net/http, but when I check the require statement was already there. I'm not understanding this at all. Further, I checked the console and its showing that the package has been included. The code:
require "uri"
require "net/http"
def authorization(client_id, token)
encrypted_token = Digest::SHA256.hexdigest(token)
params = { "token": encrypted_token, "client": client_id }
Net::HTTP.post_form(URI.parse("path to api"), params)
end
Does anyone have any insight as to what is going on?
I am trying to send data from an external website to a Rails app but the post request is not routed through correctly.
The Rails app has the route
post '/submit_text/' => 'activities#submitText', as: :submitText
The code of the corresponding controller action is
before_filter :submitText
protect_from_forgery :except => :submitText
def submitText
puts "in submitText"
puts params[:text]
puts "end of submitText "
end
I am trying to send data from an external website to the Rails app like this
<form action="https://todo-list-study.herokuapp.com/submit_text/" method= "post">
<textarea name="response[text]" rows="25" cols="75" id="answer_text">
</textarea>
<input type="submit" name="Submit" value="test" onclick="recordAnswer()">
</form>
When I submit data from the external website, the Rails app receives it but it does not pass it on to the specified controller action. Here is what happens according to the Heroku log file:
heroku[router]: at=info method=POST path="/submit_text/" host=todo-list-study.herokuapp.com request_id=fee72511-ad1e-41c3-ae4c-d4b3395e6afd fwd="24.7.71.184" dyno=web.1 connect=1ms service=19ms status=500 bytes=1754 protocol=https
Unfortunately, that is all that happens. Given that the put statement at the beginning of the method is not executed I conclude that it is not being called. This happens even though both the Rails app and the external website run on the HTTPS protocol.
What can I do to fix this?
The router is finding an action for that call, otherwise it would have responded with a 404 instead of a 500, which indicates that there's an internal error.
Assuming that you're not trying to send back a response with content back to the client, try adding a call to render so that your controller sends back a response instead of trying to find a view template to render, which is where I'm guessing the issue is.
For example, try updating your code to:
def submitText
puts "in submitText"
puts params[:text]
puts "end of submitText "
render nothing: true
end
I am testing out the service-worker rails gem and am using VAPID private and public keys to send push notifications. I've succesfully been able to create push notifications locally, but when I deploy to heroku, the app raises the error stated above when trying to go to the push notification page. Here is the error:
75] Started GET "/push-simple/" for 69.253.120.203 at 2017-02-12 06:37:35 +0000
2017-02-12T06:37:35.339318+00:00 app[web.1]: [3d626b62-4c1b-41ca-aad3-fc653dc96b75] ActionView::Template::Error (undefined method `tr' for nil:NilClass
2017-02-12T06:37:35.333379+00:00 app[web.1]: [3d626b62-4c1b-41ca-aad3-fc653dc96b75] Rendering pages/push-simple.html.erb within layouts/application
2017-02-12T06:37:35.332337+00:00 app[web.1]: [3d626b62-4c1b-41ca-aad3-fc653dc96b75] Processing by PagesController#show as HTML
2017-02-12T06:37:35.336377+00:00 app[web.1]: [3d626b62-4c1b-41ca-aad3-fc653dc96b75] Rendered pages/push-simple.html.erb within layouts/application (2.8ms)
2017-02-12T06:37:35.336998+00:00 app[web.1]: [3d626b62-4c1b-41ca-aad3-fc653dc96b75] Completed 500 Internal Server Error in 4ms
2017-02-12T06:37:35.339281+00:00 app[web.1]: [3d626b62-4c1b-41ca-aad3-fc653dc96b75]
2017-02-12T06:37:35.339490+00:00 app[web.1]: [3d626b62-4c1b-41ca-aad3-fc653dc96b75] 13: <script type="text/javascript">
2017-02-12T06:37:35.339320+00:00 app[web.1]: Did you mean? try):
2017-02-12T06:37:35.339489+00:00 app[web.1]: [3d626b62-4c1b-41ca-aad3-fc653dc96b75] 11: </div>
2017-02-12T06:37:35.339489+00:00 app[web.1]: [3d626b62-4c1b-41ca-aad3-fc653dc96b75] 12:
2017-02-12T06:37:35.339561+00:00 app[web.1]: [3d626b62-4c1b-41ca-aad3-fc653dc96b75] app/views/pages/push-simple.html.erb:14:in `_app_views_pages_push_simple_html_erb__2838769268823495646_39526220'
2017-02-12T06:37:35.339491+00:00 app[web.1]: [3d626b62-4c1b-41ca-aad3-fc653dc96b75] 15: </script>
2017-02-12T06:37:35.339491+00:00 app[web.1]: [3d626b62-4c1b-41ca-aad3-fc653dc96b75] 14: var publicKey = new Uint8Array(<%= WebpushClient.public_key_bytes %>);
2017-02-12T06:37:35.339492+00:00 app[web.1]: [3d626b62-4c1b-41ca-aad3-fc653dc96b75] 16: <%= javascript_include_tag 'push-simple' %>
2017-02-12T06:37:35.339520+00:00 app[web.1]: [3d626b62-4c1b-41ca-aad3-fc653dc96b75]
2017-02-12T06:37:35.339560+00:00 app[web.1]: [3d626b62-4c1b-41ca-aad3-fc653dc96b75] app/clients/webpush_client.rb:7:in `public_key_bytes'
And here is my webpushclient where the VAPID keys are defined:
class WebpushClient
def self.public_key
ENV['VAPID_PUBLIC_KEY']
end
def self.public_key_bytes
Base64.urlsafe_decode64(public_key).bytes
end
def self.private_key
ENV['VAPID_PRIVATE_KEY']
end
# Send webpush message using subscription parameters
#
# #param message [String] text to encrypt
# #param subscription_params [Hash<Symbol, String>]
# #option subscription_params [String] :endpoint url to send encrypted message
# #option subscription_params [Hash<Symbol, String>] :keys auth keys to send with message for decryption
# #return true/false
def send_notification(message, endpoint: "", p256dh: "", auth: "")
raise ArgumentError, ":endpoint param is required" if endpoint.blank?
raise ArgumentError, "subscription :keys are missing" if p256dh.blank? || auth.blank?
Rails.logger.info("Sending WebPush notification...............")
Rails.logger.info("message: #{message}")
Rails.logger.info("endpoint: #{endpoint}")
Rails.logger.info("p256dh: #{p256dh}")
Rails.logger.info("auth: #{auth}")
Webpush.payload_send \
message: message,
endpoint: endpoint,
p256dh: p256dh,
auth: auth,
vapid: {
subject: "jon.corrin#gmail.com",
public_key: public_key,
private_key: private_key
}
end
def public_key
self.class.public_key
end
def private_key
self.class.private_key
end
end
and here is the script im using to call the public key:
<script type="text/javascript">
var publicKey = new Uint8Array(<%= WebpushClient.public_key_bytes %>);
</script>
<%= javascript_include_tag 'push-react' %>
Like I said before, it works locally. I think I may have forgotten to define a variable, I've looked at similar posts and have been debugging for quite some time, but have had no luck. Any suggestions? I also want to add, I'm quite new, so if it's an obvious error, I'll completely understand.
Your error is coming from this method:
def self.public_key_bytes
Base64.urlsafe_decode64(public_key).bytes
end
Looking up the source code for Base64#urlsafe_decode64, this is defined by:
def urlsafe_decode64(str)
strict_decode64(str.tr("-_", "+/"))
end
This is where your undefined method 'tr' for nil:NilClass must be coming from: the public_key. Which you have defined by:
def self.public_key
ENV['VAPID_PUBLIC_KEY']
end
...In other words, you have not set the environmental variable on your production server. That's why it works locally, but not on production.
I'm using devise_token_auth in my app. When user registers, it automatically sends confirmation email.
from https://github.com/lynndylanhurley/devise_token_auth/blob/master/app/controllers/devise_token_auth/registrations_controller.rb
# ..code
unless #resource.confirmed?
# user will require email authentication
#resource.send_confirmation_instructions({
client_config: params[:config_name],
redirect_url: #redirect_url
})
# ..code
And I get 500 error.
I, [2016-09-01T12:32:06.142856 #15153] INFO -- : [07ca58ab-1797-4edb-99bb-c7d408bfd2c9] Completed 500 Internal Server Error in 31331ms (ActiveRecord: 8.4ms)
F, [2016-09-01T12:32:06.145874 #15153] FATAL -- : [07ca58ab-1797-4edb-99bb-c7d408bfd2c9]
F, [2016-09-01T12:32:06.146192 #15153] FATAL -- : [07ca58ab-1797-4edb-99bb-c7d408bfd2c9] Net::OpenTimeout (execution expired):
F, [2016-09-01T12:32:06.146249 #15153] FATAL -- : [07ca58ab-1797-4edb-99bb-c7d408bfd2c9]
F, [2016-09-01T12:32:06.146292 #15153] FATAL -- : [07ca58ab-1797-4edb-99bb-c7d408bfd2c9] /home/deploy/.rbenv/versions/2.3.1/lib/ruby/2.3.0/net/smtp.rb:542:in `initialize'
...
As you can see from the log it times out Net::OpenTimeout (execution expired):
But when I run User.last.send_confirmation_instructions from console it sends the email how it is suppose to be.
What am I suppose to do?
Thanks!
The problem was because of ipv6.
To fix it I had to edit /etc/gai.conf by uncommenting #precedence ::ffff:0:0/96 100 line
I've got some very strange issue which I cannot debug.
This is my code:
def find_order_and_payment
#payment = Spree::Payment.find_by_identifier(params['session_id'])
Rails.logger.info "payment_id #{#payment.id}"
#order = #payment.order
end
And this is the output:
I, [2014-01-17T16:39:43.084827 #12342] INFO -- : payment_id 187
I, [2014-01-17T16:39:43.090718 #12332] INFO -- : Completed 500 Internal Server Error in 448ms
NoMethodError (undefined method `id' for nil:NilClass):
app/controllers/payu_status_controller.rb:36:in `find_order_and_payment'
Line 36 is the line with Rails.logger. I don't understand, why I get correct id, but same line returns undefined method id? If I'll call above code from console everything works as expected.