-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintercom-rating.js
67 lines (54 loc) · 2.23 KB
/
intercom-rating.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
const TelegramBot = require('node-telegram-bot-api');
const Koa = require('koa');
const Router = require('koa-router');
const Parser = require('koa-bodyparser');
const token = 'telegram-token';
const url = 'where_send_webhooks(url)';
const id_team = 'id_telegram_chat';
const bot = new TelegramBot(token);
const app = new Koa();
bot.setWebHook(`${url}/${token}`);
const router = Router();
router.post('/', ctx => {
const { body } = ctx.request;
ctx.status = 200;
let rating, typeCustomer, customerId, web, ratingSmile, adminName, clientComment;
let final = body.topic + '😁'; // for PING webhooks
if (body.data.item.conversation_rating) {
rating = body.data.item.conversation_rating.rating;
clientComment = body.data.item.conversation_rating.remark;
if (rating == 1) {
ratingSmile = '😠 — Terrible';
} else if (rating == 2) {
ratingSmile = '🙁 — Bad';
} else if (rating == 3) {
ratingSmile = '😐 — OK';
} else if (rating >= 4 && clientComment != null) {
ratingSmile = '🥳';
} else {
return;
}
web = body.data.item.links.conversation_web;
adminName = body.data.item.assignee.name;
typeCustomer = body.data.item.conversation_rating.customer.type;
if (typeCustomer == 'user') {
customerId = body.data.item.conversation_rating.customer.id;
customerId = customerId.replace(/(.joinposter.com)/g, '');
} else {
customerId = 'Lead';
}
if (clientComment != null) {
final = `*${customerId}* написал нам: ${clientComment}\n\n[Прочитать чат](${web})`;
} else {
final = `*${customerId}* поставил нам ${ratingSmile}. Менеджер: *${adminName}*.\n\n👉[Проработать оценку](${web})👈 \n\nLet’s do it!`;
}
}
bot.sendMessage(id_team, final, { parse_mode: 'Markdown' });
});
app.use(Parser());
app.use(router.routes());
const port = 3000;
const host = 'localhost';
app.listen(port, host, () => {
console.log(`Listening on ${port}`)
});