Last active
November 9, 2015 08:53
-
-
Save 1syo/5ba42d2bea4b60e7dfb9 to your computer and use it in GitHub Desktop.
This file contains 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
# Description: | |
# Gmail messege. | |
# | |
google = require('googleapis') | |
googleAuth = require('google-auth-library') | |
CronJob = require('cron').CronJob | |
_ = require('underscore') | |
SLACK_USER_NAME = process.env.SLACK_USER_NAME || '' | |
CLIENT_SECRET = process.env.CLIENT_SECRET || '' | |
CLIENT_ID = process.env.CLIENT_ID || '' | |
ACCESS_TOKEN = process.env.ACCESS_TOKEN || '' | |
REFRESH_TOKEN = process.env.REFRESH_TOKEN || '' | |
EXPIRE_DATE = process.env.EXPIRE_DATE || 0 | |
class Postman | |
constructor: (res) -> | |
@id = res.id | |
@headers = _.indexBy(res.payload.headers, 'name') | |
subject: -> | |
@headers.Subject.value | |
date: -> | |
@headers.Date.value | |
from: -> | |
@headers.From.value | |
url: -> | |
"https://mail.google.com/mail/u/0/#inbox/#{@id}" | |
message: -> | |
""" | |
*#{@from()}* (#{@date()}) | |
``` | |
#{@subject()} | |
``` | |
#{@url()} | |
""" | |
module.exports = (robot) -> | |
new CronJob('* * */1 * * *', () -> | |
auth = new (new googleAuth()).OAuth2(CLIENT_ID, CLIENT_SECRET, '') | |
auth.credentials = {access_token: ACCESS_TOKEN, refresh_token: REFRESH_TOKEN, expiry_date: EXPIRE_DATE, token_type: "Bearer" } | |
getMessage = (err, res) -> | |
if err | |
console.log err | |
return | |
postman = new Postman(res) | |
robot.send {room: SLACK_USER_NAME}, postman.message() | |
getList = (err, res) -> | |
if err | |
console.log err | |
return | |
_.each res.messages, (message) -> | |
gmail.users.messages.get({auth: auth, userId: 'me', id: message.id}, getMessage) | |
gmail = google.gmail('v1') | |
gmail.users.messages.list({auth: auth, userId: 'me', labelIds: ['INBOX', 'UNREAD']}, getList) | |
).start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment