I have an app that is supposed to send an SMS to users every 5 minutes (value that will change later).
At the moment, my app is working, it sends SMS every 5 minutes, but to only one user. I don't know why the other users are ignored.
I'm using Twilio.
Here is my task :
task :sms_sender_daily => :environment do |_, args|
User.find_each do |user|
UserTexter.send_daily_sms(user).deliver_now if user.daily_sms == true && user.phone.present? (I tried with deliver_later too, same result)
end
end
Here is my UserTexter :
class UserTexter < Textris::Base
def send_daily_sms(user)
@user = user
# put your own credentials here
account_sid = 'AC25xxx'
auth_token = '059xxx'
# set up a client to talk to the Twilio REST API
@client = Twilio::REST::Client.new account_sid, auth_token
@client.account.messages.create({
:from => '+14234350632',
:to => '+' + @user.phone,
:body => 'SMS journalier',
})
end
end
I only have 3 users with 3 different number, but it only sends to one.
EDIT : When I put the send_daily_sms method in the user.rb model, and hardcode the "To" number, it sends the SMS 3 times to this number, since 3 users have daily_sms as true. Weird.
Twilio developer evangelist here.
I believe, from our discussion in the comments, that you are still using a Twilio trial account. When sending SMS messages from a trial account you can only send to numbers that you have verified. This includes your number, which you verify when you sign up. However it will probably not include the other numbers.
To resolve this, you can either verify the other numbers you are trying to test this with by going to this page in your account: https://www.twilio.com/user/account/phone-numbers/verified
Otherwise you will want to upgrade your account, then you will be able to send messages to anyone.
You can check if Twilio was throwing errors when you were trying to send messages to unverified numbers by taking a look at the alerts section in the Monitor within your account. You can access that here: https://www.twilio.com/user/account/monitor/alerts
Hope this helps!
It's a little strange that you're interacting w/ the Twilio client directly here but also using Textris. Here is how I would do it (based on the textris readme).
# app/models/user.rb
class User < ActiveRecord::Base
scope :wanting_daily_sms,
-> { with_phone.where(daily_sms: true) }
scope :with_phone,
-> { where(arel_table[:phone].not_eq(nil)) }
end
# task
task :sms_sender_daily => :environment do |_, args|
User.wanting_daily_sms.each { |u| UserTexter.send_daily_sms(u).deliver_now }
end
# config/initializers/twilio.rb
Twilio.configure do |config|
config.account_sid = 'AC25xxx'
config.auth_token = '059xxx'
end
# Gemfile
gem 'twilio-ruby'
gem 'textris'
# UserTexter
class UserTexter < Textris::Base
default from: "Our Team <+14234350632>"
def send_daily_sms(user)
@user = user
text to: @user.phone
end
end
# app/views/user_texter/send_daily_sms.text.erb
SMS journalier
After adding the initializer above you'll need to be sure to restart your server.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.