Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google Calendar integration #10

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules
.DS_Store*
.hubot_history

.env
3 changes: 2 additions & 1 deletion external-scripts.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"hubot-google-images",
"hubot-pugme",
"hubot-rules",
"hubot-shipit"
"hubot-shipit",
"hubot-slack-google-auth"
]
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"description": "A simple helpful robot for your Company",
"dependencies": {
"bang": "^1.0.4",
"googleapis": "^2.1.5",
"hubot": "^2.16.0",
"hubot-diagnostics": "0.0.1",
"hubot-google-images": "^0.2.2",
Expand All @@ -16,8 +17,11 @@
"hubot-rules": "^0.1.1",
"hubot-scripts": "^2.16.2",
"hubot-shipit": "^0.2.0",
"hubot-slack": "~2.0.3",
"shellwords": "^0.1.0"
"hubot-slack": "^2.2.0",
"hubot-slack-google-auth": "^0.1.1",
"node-uuid": "^1.4.3",
"shellwords": "^0.1.0",
"underscore": "^1.8.3"
},
"engines": {
"node": "0.10.x"
Expand Down
11 changes: 5 additions & 6 deletions scripts/chapo.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# displays the list of the pending chapos
#
# Commands:
# listen for
# bot chapo user for reason
# bot show all pending chapos
#
Expand Down Expand Up @@ -46,17 +45,17 @@ module.exports = (robot)->
string+="#{key} for #{value}"
if string is "Pending chapos"
msg.send "There are no pending chapos"
else
msg.send string
else
msg.send string

robot.respond /(.+) gave chapo/i , (msg)->
user = msg.match[1]
chapo = []
chapo = chapoList()
if chapo[user]
if chapo[user]
delete chapo[user]
msg.send "Hmm.. #{user} fullfils his promises."
else
msg.send "There was no such pending chapo"
msg.send "There was no such pending chapo"



19 changes: 0 additions & 19 deletions scripts/events.coffee

This file was deleted.

10 changes: 8 additions & 2 deletions scripts/gcal-correction.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
module.exports = (robot) ->
module.exports = (robot) ->
robot.on 'slack.attachment', (payload)=>
robot.emit 'slack-attachment', payload
robot.emit 'slack-attachment',
message:
reply_to: '#'+payload.channel
content:
text: payload.text
fallback: ""
pretext: ""
170 changes: 170 additions & 0 deletions scripts/gcal.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
# Description:
# Google Calendar integration
#
# Configuration:
# SLACK_DEFAULT_CHANNEL
# GROUP_CALENDAR
#
# Commands:
# hubot show events <number> - Shows the <number> number list of events for GROUP_PRIMARY_CALENDAR, (default 10)
# hubot create event <event> - Creates event <event> using quick add in GROUP_PRIMARY_CALENDAR
# hubot enable calendar notifications
#
# Author:
# aryanraj
#

googleapis = require 'googleapis'
uuid = require 'node-uuid'
_ = require 'underscore'

last_update = new Date()
general_events = []

SLACK_DEFAULT_CHANNEL = process.env.SLACK_DEFAULT_CHANNEL
GROUP_CALENDAR = process.env.GROUP_CALENDAR

module.exports = (robot)->
root = exports ? this
authenticate = (cb)->
robot.emit 'google:authenticate', message:user:name:SLACK_DEFAULT_CHANNEL, (err, oauth) ->
if err
console.log 'No Authentication'
else
cb oauth

cleanDate = (date)->
new Date(new Date(date).valueOf()+5.5*60*60*1000).toISOString().replace(/T/, ' ').replace(/\..+/, '')

manageEvent = (event, count)->
if event.status is "cancelled"
old_event = _.findWhere general_events, id:event.id
if old_event
text = "#{count}. #{cleanDate old_event.start.dateTime} #{old_event.summary ? 'An Event'} at #{event.location ? process.env.HUBOT_SLACK_TEAM} got #{event.status}\n" if count?
general_events = _.without general_events, old_event
else
if new Date(event.start.dateTime) < last_update
event.reminded = true
general_events.push event
text = "#{count}. #{cleanDate event.start.dateTime} #{event.summary ? 'An Event'} at #{event.location ? process.env.HUBOT_SLACK_TEAM} got #{event.status}\n" if count?
return text ? ""

setTimeout func = (token)->
console.log "starting with #{token}"
authenticate (oauth)->
googleapis.calendar('v3').events.list
auth: oauth
calendarId: GROUP_CALENDAR
maxResults: 100
singleEvents: true
orderBy: 'startTime'
pageToken: token ? null
(err, res)->
if err
console.log "The API returned an error: ",err
else
# console.log res
events = res.items
root.defaultReminders = res.defaultReminders
manageEvent event for event in events
if res.nextPageToken then func res.nextPageToken
,5000

setInterval ()->
text = "The following events are going to start:\n"
flag = false
_.each general_events, (event)->
reminders = if event.reminders.useDefault then root.defaultRemindes else event.reminders.overrides
return "No reminder" if not reminders
reminder = _.find reminders, (r)-> r.method is "popup"
return "No popup reminder" if not reminder
start_date = new Date event.start.dateTime
difference = new Date() - start_date + reminder.minutes*60000
if difference > 0 and not event.reminded
text += "#{cleanDate event.start.dateTime} - #{event.summary} at #{event.location ? process.env.HUBOT_SLACK_TEAM} within #{reminder.minutes} minutes.\n"
event.reminded = true
flag = true
robot.emit 'slack.attachment', {channel: SLACK_DEFAULT_CHANNEL, text: text} if flag
,60000

robot.respond /show events (.+)/i, (msg) ->
authenticate (oauth) ->
googleapis.calendar('v3').events.list
auth: oauth
calendarId: GROUP_CALENDAR
timeMin: new Date().toISOString()
maxResults: parseInt msg.match[1]
singleEvents: true
orderBy: 'startTime'
(err, res)->
if err
console.log "The API returned an error:",err
else
events = res.items
console.log res.defaultReminders
console.log item.summary,item.reminders for item in events
if events.length is 0
text = "No upcoming events found"
else
text = "Upcoming #{msg.match[1]} events: \n"
text += "#{i+1}. #{cleanDate event.start.dateTime} - #{event.summary}\n" for event,i in events
robot.emit 'slack.attachment', {channel: msg.message.user.room, text: text}

robot.respond /create event (.+)/i, (msg) ->
authenticate (oauth) ->
googleapis.calendar('v3').events.quickAdd
auth: oauth
calendarId: GROUP_CALENDAR
text: msg.match[1]
(err, event) ->
if err
console.log "The API returned an error: #{err}"
else
if event.status is 'confirmed'
text = "The event was successfully created. Watch at #{event.htmlLink}"
robot.emit 'slack.attachment', {channel: msg.message.user.room, text: text}

robot.respond /enable calendar notifications/i, (msg)->
authenticate (oauth)->
googleapis.calendar('v3').events.watch
auth: oauth
resource:
id: uuid.v1()
type: 'web_hook'
address: "#{process.env.HUBOT_URL}/push"
calendarId: GROUP_CALENDAR
(err, res)->
if err
console.log "The API returned an error :",err
else
text = "You will get notification for calendar events."
root.channelID = res.id
robot.emit 'slack.attachment', {channel: SLACK_DEFAULT_CHANNEL, text: text}

robot.router.post '/push', (req, res)->
channelID = req.get "X-Goog-Channel-ID"
state = req.get "X-Goog-Resource-State"
res.send(201)
if state is "exists" and channelID is root.channelID
authenticate (oauth)->
googleapis.calendar('v3').events.list
auth: oauth
calendarId: GROUP_CALENDAR
singleEvents: true
orderBy: 'startTime'
updatedMin: last_update.toISOString()
(err, res)->
if err
console.log "The API returned an error:",err
else
last_update = new Date()
console.log res
events = res.items
if events.length is 0
console.log "No event updates"
else
text = "These Events got updated\n"
for event,i in events
console.log event
text += manageEvent event,i+1
robot.emit 'slack.attachment', {channel: SLACK_DEFAULT_CHANNEL, text: text} if text
31 changes: 15 additions & 16 deletions scripts/keys.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
# we developed this to use in our 'Slack' team instance
#
# Commands:
# listen for * has/have keys in chat text and displays users with the keys/updates the user having keys
# bot who has keys : returns current user having lab keys
# bot i have keys : set's the key-holder to the user who posted
# bot i dont have keys : unsets the user who posted from key-holders
# bot xyz has keys : sets xyz as the holder of keys
# bot xyz has keys : sets xyz as the holder of keys
#
# Examples:
# :bot who has keys
Expand All @@ -25,17 +24,17 @@ module.exports = (robot)->
key = ()->
Key = robot.brain.get("key") or ""
robot.brain.set("key" ,Key)
Key
Key



robot.respond /i have (a key|the key|key|keys)/i, (msg)->
name = msg.message.user.name
name = msg.message.user.name
user = robot.brain.userForName name
k = key()
if typeof user is 'object'
msg.send "Okay #{name} has keys"
k = user.name
robot.brain.set("key",k)
robot.brain.set("key",k)


robot.respond /i (don\'t|dont|do not) (has|have) (the key|key|keys|a key)/i , (msg)->
Expand All @@ -48,8 +47,8 @@ module.exports = (robot)->
if k is ""
msg.send "Okay #{name} doesn't have keys. Who got the keys then?"
else
msg.send "Yes , i know buddy, its because #{k} has got the keys"
robot.brain.set("key",k)
msg.send "Yes , i know buddy, its because #{k} has got the keys"
robot.brain.set("key",k)


robot.respond /(.+) (has|have) (the key|key|keys|a key)/i , (msg)->
Expand All @@ -67,9 +66,9 @@ module.exports = (robot)->
msg.send "I don't know anyone by the name #{othername}"
else
k = users.name
msg.send "Okay, so now the keys are with #{users.name}"
msg.send "Okay, so now the keys are with #{users.name}"

robot.brain.set("key",k)
robot.brain.set("key",k)

robot.respond /(i|I) (have given|gave|had given) (the key|key|keys|a key|the keys) to (.+)/i , (msg)->
othername = msg.match[4]
Expand All @@ -85,18 +84,18 @@ module.exports = (robot)->
msg.send "I don't know anyone by the name #{othername}"
else
k = users.name
msg.send "Okay, so now the keys are with #{users.name}"

msg.send "Okay, so now the keys are with #{users.name}"


robot.brain.set("key",k)

robot.brain.set("key",k)

robot.respond /(who|who all) (has|have) (the key|key|keys|a key)/i , (msg)->
k = key()
msgText = k
if msgText is ""
msg.send "Ah! Nobody informed me about the keys. Don't hold me responsible for this :expressionless:"
else
msgText+=" has keys"
msg.send msgText
robot.brain.set("key" ,k)
msg.send msgText
robot.brain.set("key" ,k)

6 changes: 3 additions & 3 deletions scripts/leaderboard.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# we developed this to use in our 'Slack' team instance
#
# Commands:
# listen for keyword++ or keyword-- in chat text and updates score for each
# keyword++ : Increase score for keyword
# bot score keyword : returns current score of 'keyword'
# bot alias abc xyz : sets xyz as an alias of abc
# bot unset xyz : unsets alias xyz
Expand Down Expand Up @@ -85,7 +85,7 @@ module.exports = (robot) ->
if aliases[alias]?
aliases[alias] = ""
message = "Unset alias " + alias
else
else
message = alias + " is not an alias"
message

Expand Down Expand Up @@ -134,7 +134,7 @@ module.exports = (robot) ->
name = name.toLowerCase()
if Aliases[name]?
name = Aliases[name]

# current score for keyword
ScoreField[name] = ScoreField[name] or 0
currentscore = ScoreField[name]
Expand Down