This repository was archived by the owner on Jun 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathhelpscout.coffee
84 lines (64 loc) · 2.5 KB
/
helpscout.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Description:
# Interact with Helpscout
#
# Configuration:
# HUBOT_HELPSCOUT_API_KEY - Go to Your Profile -> API Keys
#
# Commands:
# hubot hs mailboxes - list Helpscout mailboxes
# hubot hs count MAILBOX_ID - Return the number of active tickets in a mailbox
# hubot hs users MAILBOX_ID - List the number of active tickets each user has in a mailbox
#
# Author:
# Brett Hardin (http://bretthard.in)
Util = require "util"
url = 'https://api.helpscout.net/v1'
api_key = process.env.HUBOT_HELPSCOUT_API_KEY
getRequest = (msg, path, callback) ->
auth = 'Basic ' + new Buffer("#{api_key}:x").toString('base64')
msg.robot.http("#{url}#{path}")
.headers("Authorization": auth, "Accept": "application/json")
.get() (err, res, body) ->
callback(err, res, body)
module.exports = (robot) ->
# hubot helpscout users FOLDER_ID
robot.respond /hs users\s?(@\w+)?(.*)/i, (msg) ->
if api_key
mailboxId = msg.match[2] # Second Term
getRequest msg, "/mailboxes/#{mailboxId}/conversations.json?status=active", (err, res, body) ->
response = JSON.parse body
users = []
conversations = []
for item in response.items
if item.owner != null
conversations.push { name: "#{item.owner.firstName} #{item.owner.lastName}", createdAt: item.createdAt }
else
conversations.push { name: 'Unassigned', createdAt: item.createdAt }
for conversation in conversations
for key, value of conversation
if key == 'name' && !users[value]
users[value] = 1
else if key == 'name'
users[value] += 1
for key, value of users
msg.send "#{key}: #{value}"
else
msg.send "Don't have the HUBOT_HELPSCOUT_API_KEY."
# hubot helpscout count FOLDER_ID
robot.respond /hs count\s?(@\w+)?(.*)/i, (msg) ->
if api_key
mailboxId = msg.match[2] # Second Term
getRequest msg, "/mailboxes/" + mailboxId + "/conversations.json?status=active", (err, res, body) ->
response = JSON.parse body
msg.send response.count
else
msg.send "Don't have the HUBOT_HELPSCOUT_API_KEY."
# hubot helpscout mailboxes
robot.respond /hs mailboxes\s?(.*)?/i, (msg) ->
if api_key
getRequest msg, "/mailboxes.json", (err, res, body) ->
response = JSON.parse body
for item in response.items
msg.send "#{item.id}: #{item.name}"
else
msg.send "Don't have the HUBOT_HELPSCOUT_API_KEY."