/ SMS / Send SMS with Twilio

Just copy/paste & use this function wherever you want in your Rails app:

def send_sms(message, recipient)
    email_setting = EmailSetting.first
    if email_setting.status == true
    twilio_sid = "YOUR TWILIO ID"
    twilio_token = "YOUR TWILIO TOKEN"
    twilio_phone_number = "7481338505"
    twilio_sender_id = "Rentlord"

    client = Twilio::REST::Client.new(twilio_sid, twilio_token)
          
    begin
      client.messages.create(
        from: twilio_sender_id,
        to:   recipient,
        body: message
      )
    rescue Twilio::REST::RequestError => error
      if error.code == 21612
        client.messages.create(
          from: "+44#{twilio_phone_number}",
          to:   recipient,
          body: message
        )
      else
        # handle this some other way
        raise error
      end
    end		
    end
end

 

It’s that simple 🙂

No video for this post.

No issues for this post.

by ryzalyusoff