public
Created

LunchOrders

  • Download Gist
almoço.coffee
CoffeeScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
# 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 = "restaurant@email.com"
email_copy = "mygenerallist@mycompany.com.br"
downloadOrders (body, err, cb_name) ->
# option settings
options = {
host : "smtp.mycompany.com",
port : 465,
ssl : true,
domain : 'companydomain.com',
authentication : true,
username : "boot@email.com",
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@email.com'
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")
)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.