Schedule Jobs in Ruby on Rails with Whenever gem | by Florence Njeri | Mar, 2022

Get hourly reminders to drink water on slack.

‘Image credit: Author’
Picture credit score: writer

Getting began

Create a Slack App
Create an app from scratch
Add app identify, decide workspace and click on create an app
Copy the Webhook URL
bundle gem slackbot
WEBHOOK_URL = "your_webhook_url"
gem 'dotenv-rails', teams: [:development, :test]
bundle set up
Step 1 and Step 2 above

What’s a cron job?

gem 'each time', require: false
bundle set up
Step 1 and Step 2 above
bundle exec wheneverize .
Step 3
rails g activity slack run_notifications
require 'httparty'require 'dotenv/duties'namespace :slack dodesc "Ship hourly remonders to drink water in Slack"activity :run_notifications => :atmosphere doHTTParty.publish(ENV["WEBHOOK_URL"], :physique => :channel  => '#basic',:username => 'Slack Bot',:textual content     => 'Time to take a glass of water."'.to_json)finish
gem 'httparty'bundle set up
set :output, "log/cron.log"
each 1.hour dorake "slack:run_notifications"finish
each time — update-crontab — set atmosphere=’improvement’
rake "slack:run_notifications"

More Posts