Rails scheduled SMS sender

27 views
Skip to first unread message

Marco Dias

unread,
Jul 23, 2015, 6:35:47 PM7/23/15
to Ruby on Rails: Talk
I want send an SMS each 5 minutes to my users. At the moment, my application sends an SMS during the creation of an account.
 
 
   # users_controller.rb
   
def create
         
@user = User.new(user_params)
         
if @user.save
           
@user.send_activation_email
           
@user.send_daily_sms
            flash
[:info] = "Veuillez contrôler votre boîte mail pour activer votre compte."
            redirect_to root_url
         
else
            render
'new'
         
end
       
end


   
# user.rb
   
def send_daily_sms
   
       
# put your own credentials here
        account_sid
= '**********************'
        auth_token
= '**********************'
     
       
# 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 => '**********',
       
:to => '***********',
       
:body => 'Salut',  
       
})
     
end


I already have scheduled mails working in my project by doing this :

   
 # schedule.rb
    every
:day, :at => '12pm' do    
      rake
"email_sender_daily"
   
end
   
# My task
    task
:email_sender_daily => :environment do |_, args|
     
User.find_each do |user|
       
UserMailer.daily_mail(user).deliver_now if user.daily == true
     
end
   
end
   
# My UserMailer
   
def daily_mail(user)
   
@user = user
    mail to
: user.email, subject: "Mail journalier"
   
end


I'm showing you this because, with the UserMailer, I know how to access it from an other file. Here, I'd like to do the exactly the same for SMS, but how can I access the method that is in my Model ? If not, where can I put this method to be able to access it from my rake task ? 

thiagocifani

unread,
Jul 23, 2015, 7:13:58 PM7/23/15
to rubyonra...@googlegroups.com
Hello Marco, as you can see, you can send email or sms in the same way. The only thing you can do to avoid repetition, you should move the sms logic to a service class and instantiate it in the controller and in a rake task to send it from time to time.

I hope I helped you somehow.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/6977da81-1fd7-47d6-a16d-36d63afedea7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Marco Dias

unread,
Jul 23, 2015, 7:23:58 PM7/23/15
to Ruby on Rails: Talk, cifani...@gmail.com
Thanks for your answer,

The thing is, the mails being sent with the scheduled jobs are not in a Controller, they are in the UserMailer and they're easy to access via the 
UserMailer.daily_mail(user).deliver_now

What I'd like to do, is having a similar thing with SMS. Maybe create a SMSController. The thing is I don't know how to access a method of a Controller from outside. 

Marco Dias

unread,
Jul 24, 2015, 5:53:20 PM7/24/15
to Ruby on Rails: Talk, dias...@gmail.com
Here is the update of my post :

EDIT: 

I'm able to send SMS now, but weird thing, it only sends to one user, even if I have 3 users.

My method in User.rb

    def send_daily_sms(user)
        
        @user = user
    
        # put your own credentials here 
        account_sid = 'AC2556499574e7111844811bff28e73061' 
        auth_token = '0593dd8cc063cfbe704bc73a8f83fbba' 
     
        # 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

My task : 

task :email_sender_monthly => :environment do |_, args|
 
User.find_each do |user|
   
user.send_daily_sms if user.daily_sms == true && user.phone.present?
 
end
end


I tried to use the gem [Textris](https://github.com/visualitypl/textris) to see if using this would work (they have Twilio API built-in function). 

My UserTexter :

    class UserTexter < Textris::Base
      default from: "Our Team <+14234350632>"
    
      def send_daily_sms(user)
        @user = user
        phone = "+" + @user.phone
        puts phone
        text :to => phone
      end
    end

The thing is, I have this in the log :

    rake aborted!
    Twilio::REST::RequestError: The 'To' number 41798352547 is not a valid phone number.

But as you can see, the number printed in the log is right, and I was doing quite the same when I was using the method in User.rb (it was sending SMS then, but only to one user).

Don't know what to do.
Reply all
Reply to author
Forward
0 new messages