简体   繁体   中英

How to send sequential SMS using Twilio on Rails?

I am using React as front end, and Rails back end. I have an array of phone numbers that I want to send text messages to.

On the front end, I have this method:

function postMessage(message, newWorkers){
  let phoneArray = newWorkers.map((worker) => {return PhoneHelper.condensePhone(worker.phone)}) //condensePhone converts input to +1PHONENUMBER
  for (var i = 0; i < phoneArray.length; i++){

    return fetch(`text_it`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        message: message,
        phone: phoneArray[i]
      })
    })
  }
};

phoneArray will look something like ["1234567890", "1112223333", "2223334444"] . I will be sending the same text message to all those numbers.

On the back-end, I have:

//routes 
post '/text_it' => 'schedules#text_dat_message'

//schedules_controller
def text_dat_message
  TwilioSender.new.send_it(params[:message], params[:phone])
end

I tested it by sending it to my phone numbers twice, however I only received one text. Then I read on this SO post that Twilio free tier (I am on free trial right now) does 1 message/ second. I installed sidekiq and added a worker to send it in seconds,

  def text_dat_message
    HardWorker.perform_in(10.seconds, params[:message], params[:phone]) #HardWorker does TwilioSender.new.send_it(args[0], args[1])
  end

It is still only sending me a single text when I tried sending 2-3 texts to my number.

How can I change my code so it sends text message to a designated number, sequentially, with say 5 second breaks inbetween?

Edit:

It is working. Michael's answer gave me a huge insight. The problem, it seems, that sidekiq's perform_in 10 seconds that I have kicks it off 10 seconds almost from the same point, and with Twilio's 1 SMS/s limitation, it won't work.

I edited my React side to send a phone array instead of iterating through it:

  ...
  body: JSON.stringify({
    message: message,
    phones: phoneArray
  })

and on the Rails side, I know I will be receiving an array of phones (and one message), so I edited my controller to follow wesley's answer. In addition, I needed to have an array of phones, so I did:

phones = params[:phones]
phones.each_with_index ...

You need to send messages seperately, each with different delay:

phones.each_with_index do |number, index|
    SomeWorker.perform_in(index*3, message, number)
end

Looks to me like the HardWorker delay needs to vary between the messages being sent. All that the Hardworker code is doing is sending all the messages 10 seconds after you receive them (all at the same time still) something like:

def text_dat_message
  HardWorker.perform_in(rand(1..30).seconds, params[:message], params[:phone]) #HardWorker does TwilioSender.new.send_it(args[0], args[1])
end

would randomize the delay, which should allow you to send multiple tests.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM