Skip to content
This repository was archived by the owner on Jun 8, 2023. It is now read-only.

Commit 8bf83d1

Browse files
author
Chris DaMour
committed
added some new commands to sendgrid to let you pick number of days to go back, list categories, and more category level options
1 parent 7aaecab commit 8bf83d1

File tree

1 file changed

+60
-10
lines changed

1 file changed

+60
-10
lines changed

src/scripts/sendgrid.coffee

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111
# Commands:
1212
# hubot sendgrid total - total sendgrid usage for the account
1313
# hubot sendgrid today - Total usage for today
14-
# hubot sendgrid c[ategory] <category> - Today's usage for the given category
14+
# hubot sendgrid last X [days] - Total usage for the last X days
15+
# hubot sendgrid c[ategory] [total] <category> - Today or all time usage for the given category
16+
# hubot sendgrid c[ategory] last X [days] <category> - Total usage for the last X days for the given category
17+
# hubot sendgrid categories - list all categories for account
1518
#
1619
# Author:
1720
# sixfeetover
21+
# drdamour
1822

1923
env = process.env
2024

@@ -24,30 +28,76 @@ module.exports = (robot) ->
2428
opts =
2529
days: 0
2630
query msg, opts, (json) ->
27-
msg.send formatResponse(json[0])
31+
msg.send formatResponse(json[0], json[0].date)
2832

2933
robot.respond /(sendgrid)( me)? total/i, (msg) ->
3034
opts =
3135
aggregate: 1
3236
query msg, opts, (json) ->
33-
msg.send formatResponse(json)
37+
msg.send formatResponse(json, "All Time")
3438

35-
robot.respond /(sendgrid)( me)? c(ategory)? (.*)/i, (msg) ->
36-
category = msg.match[4]
39+
robot.respond /(sendgrid)( me)? last (\d+)( days)?/i, (msg) ->
40+
opts =
41+
days: msg.match[3]
42+
aggregate: 1
43+
query msg, opts, (json) ->
44+
msg.send formatResponse(json, "Last #{opts.days} days")
45+
46+
robot.respond /(sendgrid)( me)? c(ategory)?( total)? (.*)/i, (msg) ->
47+
category = msg.match[5].trim()
48+
match = /last (\d+)( days)?/i
49+
#this response matches the next respond, so we need to short circuit it
50+
#anyone got a better way?
51+
if match.test(category)
52+
return
53+
isAllTime = msg.match[4] is " total"
3754
msg.send "Category: #{category}"
3855
opts =
39-
days: 0
4056
category: [category]
57+
58+
if(isAllTime)
59+
opts.aggregate = 1
60+
else
61+
opts.days = 0
62+
query msg, opts, (json) ->
63+
if(isAllTime)
64+
#surprisingly when you set a cateogry, the aggregate is sent in an array
65+
msg.send formatResponse(json[0], "All Time")
66+
else
67+
msg.send formatResponse(json[0], json[0].date)
68+
69+
robot.respond /(sendgrid)( me)? c(ategory)? last (\d+)( days)? (.*)/i, (msg) ->
70+
category = msg.match[6].trim()
71+
msg.send "Category: #{category}"
72+
opts =
73+
category: [category]
74+
aggregate: 1
75+
days: msg.match[4]
76+
query msg, opts, (json) ->
77+
#surprisingly when you set a cateogry, the aggregate is sent in an array
78+
msg.send formatResponse(json[0], "Last #{opts.days} days")
79+
80+
81+
robot.respond /(sendgrid)( me)? categories/i, (msg) ->
82+
opts =
83+
list: "true"
4184
query msg, opts, (json) ->
42-
msg.send formatResponse(json[0])
85+
msg.send json[0].category
86+
categories = for cat in json
87+
" #{cat.category}"
88+
msg.send categories.join('\n')
4389

4490
query = (msg, opts, callback) ->
4591
opts.api_user = env.HUBOT_SENDGRID_USER
4692
opts.api_key = env.HUBOT_SENDGRID_KEY
4793
msg.http("https://sendgrid.com/api/stats.get.json")
4894
.query(opts)
4995
.get() (err, res, body) ->
50-
callback JSON.parse(body)
96+
parsedBody = JSON.parse(body)
97+
if parsedBody.error
98+
msg.send parsedBody.error
99+
return
100+
callback parsedBody
51101

52102
stats =
53103
requests: 'Requests'
@@ -73,8 +123,8 @@ stats =
73123
spamreports: 'Spam Reports'
74124
repeat_spamreports: 'Repeat Spam Reports'
75125

76-
formatResponse = (json) =>
126+
formatResponse = (json, header) =>
77127
details = for stat, description of stats
78128
" #{description}: #{json[stat]}"
79-
details.unshift "Stats for #{json.date}:"
129+
details.unshift "Stats for #{header}:"
80130
details.join("\n")

0 commit comments

Comments
 (0)