Todos Script

The todos script (scripts/todos.js) contains a three handlers for responding to the following phrases:

hubot show my todo list

hubot add <item> to my todo list

hubot <itemID> is done

The script stores todo items by saving them to files in the todos directory.

Adding Todo Items
When a user adds a todo item, a new file is created in the todos directory. The file name is a randomly generated UUID (uuid version 4), and it contains the the todo item. No special encoding is used. The file just contains the todo item. That’s it.
Removing Todo Items
When a user wants to remove an item, they must specify the item’s UUID. Assuming that an item with the given UUID exists, the item’s corresponding file is deleted from the todos directory.
Listing Todo Items
When a user wants to list the todo items, each item in the todos directory is opened. The Hubot simply replies with each item’s UUID (from the file name) and its corresponding todo item (the file contents).

How it Works

Each phrase has its own independent callback.

Listing Todo Items

Your callback for show my todo list should do the following:

  1. Use fs.readdir to list the names of the files in the DATA_DIR directory.
  • If the fs.readdir callback receives an error (if the error has value), then the Hubot will reply with the following:

    Oh no... I couldn't look for todos...

    and return immediately.

  • If the list of todo files is empty, the Hubot will reply with:

    The list is empty!

    and return immediately.

  • Otherwise, we found some files and proceed.

  1. Loop over received file names, and use fs.readFile to read the contents of each file. Whenever a file is read, its corresponding callback will be executed. Each fs.readFile callback takes two parameters: error and data.

    • If error has value, then the Hubot will reply with:

      Uh oh! Had trouble opening a todo...
    • Otherwise, the Hubot will reply with the ID of the todo item (its file name) and the content of the todo file:

      <itemID>: <item>

In summary, for each file in the DATA_DIR directory, we call fs.readFile and register a callback, so that when the content of a todo file has been read, we can reply to the user with its contents.

We don’t necessarily know the order of the files in the directory, nor do we know how long it will take to read each file, so the Hubot may reply with todo items in a totally arbitrary order.

Adding Todo Items

Your callback for add (.+) to my todo list should do the following:

  1. Generate a new file name using uuid.v4()
  2. Use fs.writeFile to save the todo item (with ASCII encoding) to a file using the generated filename.
  • All todo items must be stored in DATA_DIR.
  1. When Node is done writing the todo item to the file, it calls our callback.
  • If there is an error, the hubot replies with:

    Oh no... I couldn't write the todo file...
  • Otherwise, the hubot replies with:

    OK! I added <todo> to the todo list

Note that file names have no extensions. They are just UUIDs.

Removing Todo Items

  1. Uses fs.unlink to delete the todo file from DATA_DIR with name id.
  • If there is an error deleting the file, the hubot replies with:

    Couldn't find <id>
  • Otherwise, it replies with:

    OK! Removed <id>