Skip to content

Commit

Permalink
add sendgrid script
Browse files Browse the repository at this point in the history
  • Loading branch information
ajb committed Jun 25, 2016
1 parent f376b3b commit 4cd6a6d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@
"hubot-help": "^0.2.0",
"hubot-scripts": "2.5.11",
"hubot-slack": "^3.4.2",
"moment": "^2.13.0",
"nightmare": "^1.7.0",
"prettyjson": "^1.1.3",
"request": "~2.34.0",
"right-pad": "^1.0.0",
"sendgrid": "^3.0.5",
"time": "0.11.4",
"underscore": "~1.6.0"
},
Expand Down
46 changes: 46 additions & 0 deletions scripts/sendgrid.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Configuration:
# SENDGRID_API_KEY
#
# Commands:
# hubot sendgrid logs for <email> #sendgrid

sg = require('sendgrid').SendGrid(process.env.SENDGRID_API_KEY)
rightpad = require('right-pad')
_ = require('underscore')
moment = require('moment')

module.exports = (robot) ->
padLength = (items, key) ->
lengths = _.map items, (item) ->
item[key].length

(_.max(lengths) || 0) + 3

robot.respond /sendgrid logs for (.*)/i, (msg) ->
request = sg.emptyRequest()
request.method = 'GET'
request.path = '/v3/email_activity'
query = msg.match[1].trim()
request.queryParams['email'] = query
request.queryParams['limit'] = 10

sg.API request, (res) ->
if res.statusCode != 200
msg.send('Sorry, an error occured when fetching logs from SendGrid.')
return

items = JSON.parse(res.body)
msgToSay = ''

msgToSay += "Found #{if items.length == 10 then '10+' else items.length} #{if items.length == 1 then 'log' else 'logs'} for #{query}."

if items.length > 0
msgToSay += "\n\n"

for item in items
paddedEvent = rightpad("[#{item.event}]", padLength(items, 'event'))
reason = if item.reason then "(#{item.reason})"
time = moment.unix(item.created).fromNow()
msgToSay += "#{paddedEvent} #{rightpad(item.email, padLength(items, 'email'))} #{rightpad(time, 15)} #{reason || ''}\n"

msg.send(msgToSay)

0 comments on commit 4cd6a6d

Please sign in to comment.