Building Rake Task for Sending SMS Messages to Employees
​This guide walks through how to build a custom rake task for sending timed SMS messages.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

In this video, we are going to build the rake task for our SMS functionality.

One way to implement is to define a scope in user.rb file, like this:

scope :employees, -> {where (type: nil) }

This would technically work fine. But, I don't like this implementation because I think this is too fragile. We are checking for something negative, and this doesn't make me too happy.

A better and cleaner way is to create an Employee model. So, go to app/models/ and create a file called employee.rb. In this file, create a class called Employee that inherits from the User.

# app/models/employee.rb

class Employee < User
end

In seeds.rb, let's take advantage of Single Table Inheritance, and change @user to @employee and have it point to Employee.create.

Find every @user word in the file and replace it with @employee, except user_id. The seeds.rb file should now look like this:

# db/seeds.rb

@employee = Employee.create(email: "test@test.com",
                    password: "asdfasdf",
                    password_confirmation: "asdfasdf",
                    first_name: "Jon",
                    last_name: "Snow",
                    phone: "4322386131")

puts "1 employee created"

AdminUser.create(email: "admin@test.com",
                  password: "asdfasdf",
                  password_confirmation: "asdfasdf",
                  first_name: "Admin",
                  last_name: "Name",
                  phone: "4322386131")

puts "1 Admin user created"

AuditLog.create!(user_id: @employee.id, status: 0, start_date: (Date.today - 6.days))
AuditLog.create!(user_id: @employee.id, status: 0, start_date: (Date.today - 13.days))
AuditLog.create!(user_id: @employee.id, status: 0, start_date: (Date.today - 20.days))

puts "3 audit logs have been created"

100.times do |post|
  Post.create!(date: Date.today, rationale: "#{post} rationale content Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", user_id: @employee.id, overtime_request: 2.5)
end

puts "100 Posts have been created"

Now, run bundle exec rake db:setup, and this should work.

Let's go back to our notification.rake and start on our SMS functionality. Since our new model file is in place, we can simply store all employees in an array, iterate over that array and send SMS to every employee.

# lib/tasks/notification.rake

task sms: :environment do
  if Time.now.sunday?
    employees = Employee.all

    employees.each do |employee|
      SmsTool.send_sms()
    end
  end
end

Now, let's quickly take a look at our send_sms method to figure out what information should be passed to it.

# lib/sms_tool.rb

module SmsTool
  account_sid = ENV['TWILIO_ACCOUNT_SID']
  auth_token = ENV['TWILIO_AUTH_TOKEN']

  @client = Twilio::REST::Client.new account_sid, auth_token

  def self.send_sms(number:, message:)
    @client.messages.create(
      from: ENV['TWILIO_PHONE_NUMBER'],
      to: "+1#{number}",
      body: "#{message}"
    )
  end
end

If you see, it needs a number and a message. The number is pretty easy as it's just going to be employee.phone. But, for the message, we're going to store in a variable because it's likely to be long. Finally, we'll pass both the values to send_sms method. The final code should look like this:

# lib/tasks/notification.rake

task sms: :environment do
  if Time.now.sunday?
    employees = Employee.all
    notification_message = "Please log into the overtime management dashboard to request overtime or confirm your hours for last week: https://wlp-overtime.herokuapp.com"

    employees.each do |employee|
      SmsTool.send_sms(number: employee.phone, message: notification_message)
    end
  end
end

If we run the task now, it's working great. I got the SMS message on my phone.

large

Resources