-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
103 lines (84 loc) · 3.44 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
/* Here is the explanation for the code below:
1. Intercom sends a webhook to your app
2. We create a GitHub issue from Intercom ticket payload
3. We update Intercom ticket with GitHub issue URL
4. GitHub sends a webhook to your app
5. We update Intercom ticket status to resolved */
require("dotenv").config();
const express = require("express");
const bodyParser = require("body-parser");
const { Octokit } = require("octokit");
// Initialize express and define a port
const app = express();
const PORT = 3000;
// Tell express to use body-parser's JSON parsing
app.use(bodyParser.json());
app.get("/", (_, res) => {
res.send("App is running");
});
app.post("/webhooks/intercom", async (req, res) => {
// Acknowledge Intercom webhook to prevent retries
res.status(200).end();
// Create a GitHub issue from Intercom ticket payload:
var notification = req.body;
var ticket_title = notification.data.item.ticket_attributes._default_title_;
var ticket_description =
notification.data.item.ticket_attributes._default_description_;
var ticket_id = notification.data.item.id;
const octokit = new Octokit({ auth: process.env.GITHUB_ACCESS_TOKEN });
const github_issue = await octokit.rest.issues.create({
owner: "your_github_org", // TODO replace with your GitHub org e.g. intercom
repo: "your_github_repo", // TODO replace with your GitHub repo e.g. intercom-github-integration
// Note that we store Intercom ticket id within the issue title, we'll need it in Step 5 of this tutorial: In real world scenarios you would probably store this elsewhere.
title: `${ticket_title} [Intercom ticket number: ${ticket_id}]`,
body: ticket_description,
});
const github_issue_url = github_issue.data.html_url;
// Update Intercom ticket with GitHub issue URL
const intercom_ticket_endpoint = `https://api.intercom.io/tickets/${ticket_id}`;
const intercom_payload = { ticket_attributes: { github_issue_url } };
const options = {
method: "PUT",
body: JSON.stringify(intercom_payload),
headers: {
accept: "application/json",
"content-type": "application/json",
"Intercom-Version": "Unstable",
authorization: `Bearer ${process.env.INTERCOM_ACCESS_TOKEN}`,
},
};
fetch(intercom_ticket_endpoint, options)
.then((response) => response.json())
.then((response) => console.log(response))
.catch((err) => console.error(err));
});
app.post("/webhooks/github", async (req, res) => {
res.status(200).end();
const notification = req.body;
if (notification.action === "closed") {
console.log("github issue closed");
const gh_issue_title = notification.issue.title;
const ticket_id_regex = /Intercom ticket number:\s*(\d+)\]/;
const intercom_ticket_id = gh_issue_title.match(ticket_id_regex)[1];
const options = {
method: "PUT",
body: JSON.stringify({
state: "resolved",
}),
headers: {
accept: "application/json",
"content-type": "application/json",
"Intercom-Version": "Unstable",
authorization: `Bearer ${process.env.INTERCOM_ACCESS_TOKEN}`,
},
};
fetch(`https://api.intercom.io/tickets/${intercom_ticket_id}`, options)
.then((response) => response.json())
.then((response) => console.log(response))
.catch((err) => console.error(err));
return;
}
console.log(notification.action);
});
// Start express on the defined port
app.listen(PORT, () => console.log(`🚀 Server running on port ${PORT}`));