Scheduling tweets at specific times for twitter_ebooks bots
Published: June 2, 2017
In the documentation for twitter_ebooks you’ll see the following code.
def on_startup
scheduler.every '24h' do
# Tweet something every 24 hours
# See https://github.com/jmettraux/rufus-scheduler
# tweet("hi")
# pictweet("hi", "cuteselfie.jpg")
end
end
This will cause your ebooks_bot to tweet every 24 hours. However, what if you want your bot to Tweet at a specific schedule every day? We’ll take a look at how to set that up here…
Cron Expressions in Rufus Scheduler
In the code above, scheduler
refers to an a Rufus::Scheduler
instance (as the comments suggest)
Rufus Scheduler supports cron expressions. For example, if you wanted your bot to tweet at 06:00 and 18:00 every day you’d just update as follows…
def on_startup
scheduler.cron '6,18 0 * * *' do
tweet("hi")
end
end
A Note On Timezones
Rufus Scheduler also allows you to specify a timezone, which will override the server timezone. In my case, I want to tweet at New York time. Specifying a timezone is very simple…
def on_startup
scheduler.cron '6,18 0 * * * America/New_York' do
tweet("hi")
end
end