Scheduling tweets at specific times for twitter_ebooks bots

Published: June 2, 2017

Tags:

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

Max Chadwick Hi, I'm Max!

I'm a software developer who mainly works in PHP, but loves dabbling in other languages like Go and Ruby. Technical topics that interest me are monitoring, security and performance. I'm also a stickler for good documentation and clear technical writing.

During the day I lead a team of developers and solve challenging technical problems at Something Digital where I mainly work with the Magento platform. I've also spoken at a number of events.

In my spare time I blog about tech, work on open source and participate in bug bounty programs.

If you'd like to get in contact, you can find me on Twitter and LinkedIn.