-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
167 lines (148 loc) · 4.11 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
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
const express = require('express');
require('dotenv').config();
const bodyParser = require('body-parser');
const { TiledeskClient } = require('@tiledesk/tiledesk-client');
// const { TiledeskClient } = require('./tiledesk-client');
const app = express();
// app.use(bodyParser.json());
const mqttTest = require('./tiledesk-client/mqtt-route');
// const mqttTest = require('@tiledesk/tiledesk-client/mqtt-route');
const mqtt_route = mqttTest.router
app.use("/mqttTestRoute", mqtt_route);
mqttTest.startApp(
{
// CHAT_API_ENDPOINT: process.env.CHAT_API_ENDPOINT,
// MQTT_ENDPOINT: process.env.MQTT_ENDPOINT,
// API_ENDPOINT: process.env.API_ENDPOINT,
LOG_STATUS: false
}, () => {
console.log("mqttTestRoute route successfully started.");
var port = process.env.PORT || 3000;
app.listen(port, function () {
console.log('App listening on port ', port);
});
});
app.get('/', (req, res) => {
res.send('Hello Tiledesk nodejs APIs!')
});
app.post('/sendmessage', (req, res) => {
const project_id = req.body.project_id
const recipient_id = req.body.recipient_id
const token = req.body.token
const text = req.body.text
// TiledeskClient instance is the preferred method
const tdclient = new TiledeskClient(
{
projectId: project_id,
token: token
});
tdclient.sendMessage(
{
text: text
}, recipient_id
);
res.send({success: true});
})
app.post('/sendmessageraw', (req, res) => {
const project_id = req.body.project_id
const recipient_id = req.body.recipient_id
const token = req.body.token
const text = req.body.text
// Raw is an uncommon method but it's useful
// to have
TiledeskClient.sendMessageRaw(
"https://api.tiledesk.com/v2",
token,
project_id,
{
text: text + " raw"
},
recipient_id,
function(err, res) {
console.log("Message sent.");
}
);
res.send({success: true});
})
app.post('/anonymauth', (req, res) => {
console.log("/anonymtoken");
const project_id = req.body.project_id
// Or raw (uncommon method)
TiledeskClient.anonymousAuthentication(
project_id,
function(err, response, resbody) {
console.log("Got token", resbody);
if (!err && resbody) {
res.send(resbody);
}
else {
res.send({success: false, err: err});
}
}
);
})
app.post('/opennow', (req, res) => {
console.log("/anonymtoken");
const project_id = req.body.project_id
TiledeskClient.anonymousAuthentication(
project_id,
function(err, response, resbody) {
console.log("Got token", resbody);
if (!err && resbody.user) {
const tdclient = new TiledeskClient(
{
projectId: project_id,
token: resbody.token
});
tdclient.openNow(function(is_open) {
res.json({
success: true,
isOpen: is_open
});
});
}
else {
res.send({success: false, err: err});
}
}
);
})
app.post('/fireevent', (req, res) => {
console.log("/fireevent");
const project_id = req.body.project_id;
const event = req.body.event;
TiledeskClient.anonymousAuthentication(
project_id,
function(err, response, resbody) {
console.log("Got token", resbody);
if (!err && resbody.user) {
const tdclient = new TiledeskClient(
{
projectId: project_id,
token: resbody.token
});
// const event = {
// name: "faqbot.answer_not_found",
// attributes: {
// bot: {
// _id: bot_id,
// name: bot_name
// },
// message: {
// text: "help"
// }
// }
// };
tdclient.fireEvent(event, function(err, response, resbody) {
res.json(resbody);
});
}
else {
res.send({success: false, err: err});
}
}
);
})
// app.listen(3000, () => {
// console.log('server started');
// });