-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
170 lines (156 loc) · 6.62 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
168
169
170
const Telegram = require('node-telegram-bot-api');
const sendEmail = require('./sendEmail');
const TOKEN = require('./config').token;
const bot = new Telegram(TOKEN, {polling: true});
const isEmail = (email) => /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(email);
let tickets = {};
function actionToText(text) {
switch (text) {
case 'Support': return 'Техническая поддержка';
case 'developers': return 'Разработчики';
case 'lowPriority': return 'Низкий';
case 'middlePriority': return 'Нормальный';
case 'topPriority': return 'Срочный';
default: return 'what?'
}
}
bot.on('callback_query', query => {
let action = query.data;
let userFrom = query.message.chat.id;
let msgId = query.message.message_id;
if(action === 'newTicker') {
tickets[userFrom] = {};
let opts = {
chat_id: userFrom,
message_id: msgId,
reply_markup: {
inline_keyboard: [[{
text: 'Техническая поддержка',
callback_data: 'Support'
}],[{
text: 'Разработчики',
callback_data: 'developers'
}]]
}
};
bot.editMessageText('Выберите тип заявки', opts)
} else if(action === 'Support' || action === 'developers') {
tickets[userFrom].whom = actionToText(action);
let opts = {
chat_id: userFrom,
message_id: msgId,
reply_markup: {
inline_keyboard: [[{
text: 'Низкий',
callback_data: 'lowPriority'
}],[{
text: 'Нормальный',
callback_data: 'middlePriority'
}],[{
text: 'Срочный',
callback_data: 'topPriority'
}]]
}
};
bot.editMessageText('Выберите приоритет заявки', opts)
} else if(action === 'lowPriority' || action === 'middlePriority' || action === 'topPriority') {
tickets[userFrom].priority = actionToText(action);
let opts = {
chat_id: userFrom,
message_id: msgId,
};
bot.editMessageText('Опишите тему обращения в двух словах:', opts)
} else if(action === 'yes' || action === 'no') {
let opts = {
chat_id: userFrom,
message_id: msgId,
reply_markup: {
inline_keyboard: [[{
text: 'Обратиться снова',
callback_data: 'newTicker'
}]]
}
};
if(action === 'yes') {
let username = query.from.username;
if(!username) { // if undefined
username = query.from.first_name || query.from.last_name;
} else {
username = '@' + username // place @ before username
}
let text = `<html><b>От кого:</b> ${username} [${query.from.id}]<br>`;
text += `<b>Кому:</b> ${tickets[userFrom].whom}<br>`;
text += `<b>Приоритет:</b> ${tickets[userFrom].priority}<br><br>`;
text += `——————————<br><br></html>`;
text += `${tickets[userFrom].issue}`;
sendEmail(text, tickets[userFrom].email, tickets[userFrom].subject)
.then(() => {
delete tickets[userFrom];
bot.editMessageText('Заявка отправлена', opts)
})
.catch(err => {
delete tickets[userFrom];
console.log(err);
bot.editMessageText('Заявка не была отправлена по техническим причинам. Повторите позже.', opts)
})
} else {
bot.editMessageText('Заявка не была отправлена', opts)
}
}
});
bot.on('message', msg => {
let userFrom = msg.chat.id;
if(msg.text === '\/start') {
if(tickets[userFrom]) delete tickets[userFrom];
let opts = {
reply_markup: {
inline_keyboard: [[{
text: 'Новая заявка',
callback_data: 'newTicker'
}]]
}
};
bot.sendMessage(userFrom, 'Меню', opts)
} else {
if(!tickets[userFrom].subject) {
if(msg.text.length > 40) {
bot.sendMessage(userFrom, `Слишком длинная тема обращения. Покороче, пожалуйста. Повторите попытку:`)
} else {
tickets[userFrom].subject = msg.text;
bot.sendMessage(userFrom, `Теперь подробно опишите вашу проблему`)
}
} else if(!tickets[userFrom].issue) {
tickets[userFrom].issue = msg.text;
bot.sendMessage(userFrom, `Теперь введите свой email`)
} else if(!tickets[userFrom].email) {
if(isEmail(msg.text) === false) {
bot.sendMessage(userFrom, `Введен некорректный email. Повторите.`)
} else {
tickets[userFrom].email = msg.text;
let text = 'Подтвердите:\n\n';
text += `Кому: ${tickets[userFrom].whom}\n`;
text += `Приоритет: ${tickets[userFrom].priority}\n`;
text += `Тема: ${tickets[userFrom].subject}\n`;
text += `Проблема: ${tickets[userFrom].issue}\n`;
text += `Email: ${tickets[userFrom].email}`;
let opts = {
reply_markup: {
inline_keyboard: [[{
text: 'Да',
callback_data: 'yes'
},{
text: 'Нет',
callback_data: 'no'
}]]
}
};
bot.sendMessage(userFrom, text, opts)
}
} else {
bot.sendMessage(userFrom, 'Надо нажать на кнопку.')
}
}
});
bot.on('polling_error', err => {
console.log(`[tgError] ${new Date().toString().slice(16, -9)}: ${err.message}`)
});