Arif Usman
Arif Usman

Reputation: 1728

Asynchronous SMS using Twilio in rails application

Is there any way to send the SMS using Twilio asynchronously, just like sending mail using deliver_later. I have gone through the Twilio documentation but didn't found anything regarding this. My main motive is to perform each and every action like update records for all User and then send SMS after that to each and every User. My code look like this:

      message = 'One job is waiting for you'
      number = @get_worker.phone_number
      account_sid = '...'
      auth_token = '...' 
      @client = Twilio::REST::Client.new account_sid, auth_token
      begin
        @message = @client.account.messages.create({:to => "#{number}",:from => "+181xxxxxxxx",:body => "#{message}"})
      rescue Twilio::REST::RequestError => e
        puts e.message
        #redirect_to job_details_path, notice: e.message
      end

Upvotes: 1

Views: 251

Answers (1)

philnash
philnash

Reputation: 73029

Twilio developer evangelist here.

There is nothing built into the Twilio gem itself to integrate with a background queue. However, if you want a more ActionMailer like interface to sending SMS messages including integration with ActiveJob for delaying the API requests, you might want to check out the Textris gem.

With Textris you create Texter classes that can be used in the same way as Mailer classes. Here's an example showing various delay and deliver methods from the README:

UserTexter.welcome(user).deliver_later
UserTexter.welcome(user).deliver_later(:wait => 1.hour)
UserTexter.welcome(user).deliver_later(:wait_until => 1.day.from_now)
UserTexter.welcome(user).deliver_later(:queue => :custom_queue)
UserTexter.welcome(user).deliver_now

Textris integrates with Twilio, so you could drop this in to use in your Rails app and simplify your asynchronous API requests.

Upvotes: 1

Related Questions