forked from jakubsikora/chat-guard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
175 lines (152 loc) · 4.06 KB
/
index.js
File metadata and controls
175 lines (152 loc) · 4.06 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
require('dotenv/config')
const axios = require('axios')
const oauth = require('axios-oauth-client')
const express = require('express')
const app = express()
app.use(express.json())
const port = 3000
const apiClient = axios.create({
baseURL: process.env.API_URL,
})
// DON'T DO IT IN PROD
let accessToken
const enableWebhook = async () => {
try {
console.log(
'sending https://api.livechatinc.com/v3.3/configuration/action/enable_license_webhooks'
)
await apiClient.post(
'/v3.3/configuration/action/enable_license_webhooks',
{},
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
}
)
} catch (error) {
if (error.response && error.response.data) {
console.log('enableWebhook error', error.response.data.error)
}
}
}
const registerWebhook = async () => {
try {
console.log(
'sending https://api.livechatinc.com/v3.3/configuration/action/register_webhook'
)
const { data } = await apiClient.post(
'/v3.3/configuration/action/register_webhook',
{
url: `${process.env.INTEGRATION_URL}/chat`,
description: 'Test webhook',
action: 'incoming_chat',
secret_key: 'secret',
type: 'license',
owner_client_id: process.env.CLIENT_ID,
},
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
}
)
console.log('data', data)
return data
} catch (error) {
if (error.response && error.response.data) {
console.log('registerWebhook error', error.response.data.error)
}
}
}
const checkWebhookExists = async () => {
try {
console.log(
'sending https://api.livechatinc.com/v3.3/configuration/action/list_webhooks'
)
const { data } = await apiClient.post(
'/v3.3/configuration/action/list_webhooks',
{
owner_client_id: process.env.CLIENT_ID,
},
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
}
)
console.log('data', data)
return data.some(item => item.action === 'incoming_chat')
} catch (error) {
if (error.response && error.response.data) {
console.log('checkWebhook error', error.response.data.error)
}
}
}
const closeChat = async id => {
try {
console.log('closing chat', id)
await apiClient.post(
'/v3.3/agent/action/deactivate_chat',
{
id,
},
{
headers: {
Authorization: `Bearer ACCESS_TOKEN`,
},
}
)
} catch (error) {
if (error.response && error.response.data) {
console.log('closeChat error', error.response.data.error)
}
}
}
app.post('/chat', async (req, res) => {
try {
const { body } = req
const chat = body.payload.chat
const users = chat.users
const customer = users.find(user => user.type === 'customer')
const { user_agent } = customer.last_visit
console.log('user_agent', user_agent)
if (user_agent.includes('chat-guard')) {
await closeChat(chat.id)
}
res.status(200).send()
} catch (error) {
if (error.response && error.response.data) {
console.log('deactivate chat error', error.response.data.error)
}
}
})
app.get('/install', async (req, res) => {
const { code } = req.query
const getAuth = oauth.client(axios.create(), {
url: `${process.env.ACCOUNTS_URL}/token`,
grant_type: 'authorization_code',
client_id: `${process.env.CLIENT_ID}`,
client_secret: `${process.env.CLIENT_SECRET}`,
redirect_uri: `${process.env.INTEGRATION_URL}/install`,
code,
})
try {
const auth = await getAuth()
accessToken = auth.access_token
console.log('accessToken', accessToken)
const exists = await checkWebhookExists()
console.log('exists', exists)
if (!exists) {
await registerWebhook()
await enableWebhook()
}
res.send('<script>window.close();</script>')
} catch (error) {
console.log('error', error)
res.send(`Error: ${error.message}`)
}
})
app.listen(port, () => {
console.log(`Chat guard listening at http://localhost:${port}`)
})