Skip to content
This repository has been archived by the owner on Jun 8, 2023. It is now read-only.

Commit

Permalink
Added script for adding tasks (TODOs) to Hubot
Browse files Browse the repository at this point in the history
hubot add task Write a blog post
=> Task added: #1 - Write a blog post

hubot list tasks
=> #1 - Write a blog post

hubot delete task 1
=> Task deleted: #1 - Write a blog post
  • Loading branch information
crofty committed Nov 30, 2011
1 parent edd93c0 commit cdbd850
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/scripts/tasks.coffee
@@ -0,0 +1,45 @@
# Allows tasks (TODOs) to be added to Hubot
#
# task add <task> - Add a task
# task list tasks - List the tasks
# task delete <task number> - Delete a task
#

class Tasks
constructor: (@robot) ->
@robot.brain.on 'loaded', =>
@robot.brain.data.tasks ?= []
nextTaskNum: ->
tasks = @robot.brain.data.tasks
maxTaskNum = if tasks.length then Math.max.apply(Math,tasks.map (n) -> n.num) else 0
maxTaskNum++
maxTaskNum
add: (taskString) ->
task = {num: @nextTaskNum(), task: taskString}
@robot.brain.data.tasks.push task
task
all: -> @robot.brain.data.tasks
deleteByNumber: (num) ->
index = @robot.brain.data.tasks.map((n) -> n.num).indexOf(parseInt(num))
@robot.brain.data.tasks.splice(index, 1)[0]

module.exports = (robot) ->
tasks = new Tasks robot

robot.respond /(task add|add task) (.+?)$/i, (msg) ->
task = tasks.add msg.match[2]
msg.send "task added: ##{task.num} - #{task.task}"

robot.respond /(task list|list tasks)/i, (msg) ->
if tasks.all().length > 0
response = ""
for task, num in tasks.all()
response += "##{task.num} - #{task.task}\n"
msg.send response
else
msg.send "There are no tasks"

robot.respond /(task delete|delete task) #?(\d+)/i, (msg) ->
taskNum = msg.match[2]
task = tasks.deleteByNumber taskNum
msg.send "Task deleted: ##{task.num} - #{task.task}"

0 comments on commit cdbd850

Please sign in to comment.