-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
115 lines (104 loc) · 3 KB
/
index.js
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
var express = require("express");
var morgan = require("morgan");
var bodyParser = require("body-parser");
var request = require("request-promise");
var app = express();
app.use(morgan("combined"));
app.use(bodyParser.urlencoded({ extended: false }));
app.post("/", function(req, res, next) {
res.type("text");
switch (req.body.command) {
case "/whereis":
var user = req.body.text;
if (user[0] === "@") user = user.slice(1);
var url =
"https://calendar.google.com/calendar/embed?mode=AGENDA&height=600&wkst=1&bgcolor=%23FFFFFF&src=" +
user +
"%40goodeggs.com&color=%2329527A&ctz=America%2FLos_Angeles";
res
.status(200)
.send("Click to open <" + url + "|@" + user + "'s calendar>");
break;
case "/helpscout":
var newConvo = req.body.text.split(" ");
var customer = newConvo[0];
var mailbox = newConvo[1];
var removeItems = newConvo.splice(0, 2);
var subject = newConvo.join(" ");
if (mailbox === "it") {
mailbox = 19176;
} else if (mailbox === "devops") {
mailbox = 98082;
} else if (mailbox === "ops-eng") {
mailbox = 23490;
} else if (mailbox === "dg") {
mailbox = 159333;
} else if (mailbox === "shopping") {
mailbox = 35872;
} else {
throw Error;
}
var conversation = {
type: "email",
customer: {
email: customer,
type: "customer"
},
subject: subject,
mailbox: {
id: mailbox
},
tags: ["source-human", "source-slack"],
threads: [
{
type: "customer",
createdBy: {
email: customer,
type: "customer"
},
body: "This ticket was created via slack."
}
]
};
request
.post("https://api.helpscout.net/v1/conversations.json", {
auth: {
user: process.env.HELPSCOUT,
password: process.env.HELPSCOUT_PASSWORD
},
body: conversation,
json: true,
resolveWithFullResponse: true
})
.then(function(response) {
//do some stuff
var url = response.headers.location;
ticket = url
.split("/")
.pop()
.split(".")
.shift();
return res.send(
"Helpscout Ticket: " +
"https://secure.helpscout.net/conversation/" +
ticket
);
})
.catch(function(err) {
res.send(err);
});
break;
default:
next();
}
});
app.use(function(req, res, next) {
res.status(404).send("command not found");
});
var server = app.listen(process.env.PORT || "3000", "0.0.0.0", function() {
console.log(
"listening on " + server.address().address + ":" + server.address().port
);
});
process.once("SIGTERM", server.close.bind(server));
process.once("SIGINT", server.close.bind(server));