11
11
# Commands:
12
12
# hubot sendgrid total - total sendgrid usage for the account
13
13
# 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
15
18
#
16
19
# Author:
17
20
# sixfeetover
21
+ # drdamour
18
22
19
23
env = process .env
20
24
@@ -24,30 +28,76 @@ module.exports = (robot) ->
24
28
opts =
25
29
days : 0
26
30
query msg, opts, (json ) ->
27
- msg .send formatResponse (json[0 ])
31
+ msg .send formatResponse (json[0 ], json[ 0 ]. date )
28
32
29
33
robot .respond / (sendgrid)( me)? total/ i , (msg ) ->
30
34
opts =
31
35
aggregate : 1
32
36
query msg, opts, (json ) ->
33
- msg .send formatResponse (json)
37
+ msg .send formatResponse (json, " All Time " )
34
38
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"
37
54
msg .send " Category: #{ category} "
38
55
opts =
39
- days : 0
40
56
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"
41
84
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 ' )
43
89
44
90
query = (msg , opts , callback ) ->
45
91
opts .api_user = env .HUBOT_SENDGRID_USER
46
92
opts .api_key = env .HUBOT_SENDGRID_KEY
47
93
msg .http (" https://sendgrid.com/api/stats.get.json" )
48
94
.query (opts)
49
95
.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
51
101
52
102
stats =
53
103
requests : ' Requests'
@@ -73,8 +123,8 @@ stats =
73
123
spamreports : ' Spam Reports'
74
124
repeat_spamreports : ' Repeat Spam Reports'
75
125
76
- formatResponse = (json ) =>
126
+ formatResponse = (json , header ) =>
77
127
details = for stat, description of stats
78
128
" #{ description} : #{ json[stat]} "
79
- details .unshift " Stats for #{ json . date } :"
129
+ details .unshift " Stats for #{ header } :"
80
130
details .join (" \n " )
0 commit comments