Created
November 12, 2012 16:03
-
-
Save lidios/4060176 to your computer and use it in GitHub Desktop.
LunchOrders
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Lunch Orders | |
# | |
# Commands: | |
# which menu? / menu? - answer week's menu. | |
# order lunch / lunch - answer week's menu and the link for orders. | |
# orders sent? - answer if the lunch orders were already sent. | |
# | |
# STARRED | |
mail = require("mailer") | |
sys = require('sys') | |
dateFormat = require('dateformat') | |
link_form = "FORM_LINK" | |
module.exports = (robot) -> | |
name_regex = new RegExp("order lunch(\\?)?$|lunch(\\?)?$", "i") | |
robot.hear name_regex, (msg) -> | |
menu = guess_menu "" | |
msg.reply "This week the menu is "+menu+"\n"+link_form | |
robot.hear new RegExp("menu(\\?)?$|(w|W)hich menu(\\?)?$", "i"), (msg) -> | |
menu = guess_menu "" | |
msg.reply menu | |
robot.hear new RegExp("orders sent(\\?)?$", "i"), (msg) -> | |
msg.reply "If it was already sent, you should have received an email.\n" | |
#Schedule Orders | |
d = new Date() | |
d.setHours(10) | |
d.setMinutes(50) | |
d.setSeconds(0) | |
if d.getTime() < Date.now()-1000*45 | |
d.setDate(d.getDate()+1) | |
setTimeout -> | |
broadcastLunchOrder robot | |
, d.getTime()-Date.now() | |
guess_menu = () -> | |
Date.prototype.getWeek = () -> | |
onejan = new Date(this.getFullYear(),0,1); | |
return Math.ceil((((this - onejan) / 86400000) + onejan.getDay()+1)/7); | |
d = new Date() | |
menu="Cardapio 1" | |
#odd weeks's menu , even week's menu 1 | |
if (d.getWeek() % 2) == 1 | |
menu = "Cardapio 2" | |
return menu; | |
broadcastLunchOrder = (robot) -> | |
#console.log "Sending orders by 10 minutes ..." | |
d = new Date() | |
if d.getDay() != 0 and d.getDay() != 6 | |
robot.broadcast "Sending orders by 10 minutes ..." | |
#Schedule orders to be sent in 10 min ... | |
setTimeout -> | |
lunchOrder robot | |
, 1000*60*10 | |
d.setDate(d.getDate()+1) | |
d.setHours(10) | |
d.setMinutes(50) | |
d.setSeconds(0) | |
setTimeout -> | |
broadcastLunchOrder robot | |
, d.getTime()-Date.now() | |
lunchOrder = (robot) -> | |
date_formated = dateFormat(new Date(), "dd/mm/yyyy, h:MM:ss TT"); | |
food_email = "[email protected]" | |
email_copy = "[email protected]" | |
downloadOrders (body, err, cb_name) -> | |
# option settings | |
options = { | |
host : "smtp.mycompany.com", | |
port : 465, | |
ssl : true, | |
domain : 'companydomain.com', | |
authentication : true, | |
username : "[email protected]", | |
password : "pass" | |
} | |
#if (options.host) | |
options.authentication = if options.authentication is true then 'login' else 'none' | |
options.to = food_email | |
options.cc = email_copy | |
options.from = '[email protected]' | |
options.subject = "Lunch Orders for CompanyName for #{date_formated}" | |
options.body = "Today Orders - #{date_formated} \n #{body} " | |
mail.send options, (err, result) -> | |
if (err) | |
console.log "failed to send orders" | |
robot.broadcast "Failed to send the orders. Please call -phone-" | |
else | |
robot.broadcast "Orders sent. If someone missed it please call -phone- " | |
d = new Date() | |
d.setDate(d.getDate()+1) | |
d.setHours(11) | |
d.setMinutes(0) | |
d.setSeconds(0) | |
setTimeout -> | |
lunchOrder robot | |
, d.getTime()-Date.now() | |
downloadOrders = (cb) -> | |
#console.log "Downloading orders..." | |
util = require('util') | |
spawn = require('child_process').spawn | |
# Set this variable on enviroment | |
processOrders = spawn('python', ['']); // Insert the path to download lunch orders script | |
body = undefined | |
err = undefined | |
processOrders.stdout.on('data', (data) -> | |
#console.log('stdout: ' + data); | |
body = data | |
) | |
processOrders.stderr.on('data', (data) -> | |
#console.log('stderr: ' + data); | |
err = data | |
) | |
processOrders.on('exit',(code) -> | |
#console.log('child process exited with code ' + code); | |
# Somente manda email se houverem pedidos | |
if body.length > 1 | |
cb(body, err, "Download Orders") | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment