Archive for July 2012

Mixing the Command-Line and IRC bots


Making cookies with IRC and the command-line

My IRC bot checks a directory every minute for files and reads the contents into a channel. This simple feature is also one of my favorites.

The loop:

  1. Checks for the presence of files under /say.
  2. Reads the contents of the file into a string.
  3. Prints the contents into a channel (pulled from filename).
  4. Removes the file.

Mix crontab into this and you can use any of your favorite commandline tools. Here’s a few examples from my own usage:

Finance tip at 8AM every day:

00 08 * * * curl -sLk -o ~/say/#finance@tip

This just hits a PHP script that returns a random money tip. Note: The @ sign is being used to separate the name of the channel and a unique identifier for the file. Multiple files can land in the directory without one overwriting the other.

Top HackerNews link at 1PM:

00 13 * * * ~/cronjobs/ > ~/say/#dcs@hn

Note: The original job was a one liner using Mojolicious:

perl -Mojo -E 'say g("")->dom->at("tr > td.title > a")->tree->[2]->{href}'

It’s been expanded since to include the title and keep the crontab clean.

One-off website update checker:

* * * * * curl -s | diff file1 -
|| echo 'The page changed!' > ~/say/msg@stan_theman@update

This is a quick cheap way of being automatically alerted to changes on a website.

Note: You could easily toss this into a shell script and do some more work to update the file being diffed. This would let you know about continued changes. The current script will ping you until you remove the cronjob in this state. You also need to curl the page into file1 before installing the script — I said it was cheap!

You can use this for any short piece of information (RSS feed updates, system mail, CPU/disk usage). I’ll continue with my other favorite use for this in the next blog post.