-
-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathusers.ts
230 lines (221 loc) · 6.6 KB
/
users.ts
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import { Context } from './interfaces';
import cache from './cache';
import * as db from './db';
import * as middleware from './middleware';
/** Message template helper
* @param {String} ticket
* @param {Object} message
* @param {Boolean} anon
* @param {String} autoReplyInfo
* @return {String} text
*/
function ticketMsg(
ticket: { toString: () => string },
message: {
from: { first_name: string | any[]; language_code: any };
text: string | any[];
},
tag: string,
anon = true,
autoReplyInfo: any,
) {
let link = '';
if (!anon) {
link = `tg://user?id=${cache.ticketID}`;
}
const esc: any = middleware.strictEscape;
return (
`${cache.config.language.ticket} ` +
`#T${ticket.toString().padStart(6, '0')} ${cache.config.language.from} ` +
`[${esc(message.from.first_name)}](${link})` +
` ${cache.config.language.language}: ` +
`${message.from.language_code} ${tag}\n\n` +
`${esc(message.text)}\n\n` +
(autoReplyInfo ? `_${autoReplyInfo}_` : '')
);
}
/** Ticket auto reply for common questions
* @param {context} ctx Bot context.
* @param {bot} bot Bot object.
* @param {chat} chat Bot chat.
* @return {boolean}
*/
function autoReply(ctx: Context) {
const strings = cache.config.autoreply;
for (const i in strings) {
if (ctx.message.text.toString().indexOf(strings[i]['question']) > -1) {
// Define message
const msg = cache.config.clean_replies ? strings[i]['answer'] :
`${cache.config.language.dear} ` +
`${ctx.message.from.first_name},\n\n` +
`${strings[i]['answer']}\n\n` +
`${cache.config.language.regards}\n` +
`${cache.config.language.automatedReplyAuthor}\n\n` +
`_${cache.config.language.automatedReply}_`;
// Send message with keyboard
middleware.reply(ctx, msg, { parse_mode: cache.config.parse_mode });
return true;
}
}
return false;
}
/**
* Ticket handling and spam protection.
* @param {context} ctx Bot context.
* @param {chat} chat Bot chat.
*/
function chat(ctx: Context, chat: { id: string }) {
cache.ticketID = ctx.message.from.id;
// Check if auto reply works
let isAutoReply = false;
if (autoReply(ctx)) {
isAutoReply = true;
if (!cache.config.show_auto_replied) {
return;
}
}
const autoReplyInfo = isAutoReply ?
cache.config.language.automatedReplySent :
undefined;
if (cache.ticketIDs[cache.ticketID] === undefined) {
cache.ticketIDs.push(cache.ticketID);
}
cache.ticketStatus[cache.ticketID] = true;
if (cache.ticketSent[cache.ticketID] === undefined) {
// Get Ticket ID from DB
db.getOpen(
chat.id,
ctx.session.groupCategory,
function (ticket: { id: string }) {
if (!isAutoReply && cache.config.autoreply_confirmation) {
middleware.msg(
chat.id,
cache.config.language.confirmationMessage + '\n' +
(cache.config.show_user_ticket ?
cache.config.language.ticket +
' #T' +
ticket.id.toString().padStart(6, '0') :
''),
);
}
// To staff
middleware.msg(
cache.config.staffchat_id,
ticketMsg(
ticket.id,
ctx.message,
ctx.session.groupTag,
cache.config.anonymous_tickets,
autoReplyInfo,
),
{ parse_mode: cache.config.parse_mode },
);
// Check if group flag is set and is not admin chat
if (
ctx.session.group !== '' &&
ctx.session.group != cache.config.staffchat_id
) {
// Send to group-staff chat
middleware.msg(
ctx.session.group,
ticketMsg(
ticket.id,
ctx.message,
ctx.session.groupTag,
cache.config.anonymous_tickets,
autoReplyInfo,
),
cache.config.allow_private ?
{
parse_mode: cache.config.parse_mode,
reply_markup: {
html: '',
inline_keyboard: [
[
{
text: cache.config.language.replyPrivate,
callback_data:
ctx.from.id +
'---' +
ctx.message.from.first_name +
'---' +
ctx.session.groupCategory +
'---' +
ticket.id,
},
],
],
},
} :
{
parse_mode: cache.config.parse_mode
},
);
}
},
);
// wait 5 minutes before this message appears again and do not
// send notification sounds in that time to avoid spam
setTimeout(function () {
cache.ticketSent[cache.ticketID] = undefined;
}, cache.config.spam_time);
cache.ticketSent[cache.ticketID] = 0;
} else if (cache.ticketSent[cache.ticketID] < cache.config.spam_cant_msg) {
cache.ticketSent[cache.ticketID]++;
db.getOpen(
cache.ticketID,
ctx.session.groupCategory,
function (ticket: { id: { toString: () => string } }) {
middleware.msg(
cache.config.staffchat_id,
ticketMsg(
ticket.id,
ctx.message,
ctx.session.groupTag,
cache.config.anonymous_tickets,
autoReplyInfo,
),
{ parse_mode: cache.config.parse_mode },
);
if (
ctx.session.group !== '' &&
ctx.session.group != cache.config.staffchat_id
) {
middleware.msg(
ctx.session.group,
ticketMsg(
ticket.id,
ctx.message,
ctx.session.groupTag,
cache.config.anonymous_tickets,
autoReplyInfo,
),
{ parse_mode: cache.config.parse_mode },
);
}
},
);
} else if (cache.ticketSent[cache.ticketID] === cache.config.spam_cant_msg) {
cache.ticketSent[cache.ticketID]++;
// eslint-disable-next-line new-cap
middleware.msg(chat.id, cache.config.language.blockedSpam, {
parse_mode: cache.config.parse_mode,
});
}
db.getOpen(
cache.ticketID,
ctx.session.groupCategory,
function (ticket: { id: { toString: () => string } }) {
console.log(
ticketMsg(
ticket.id,
ctx.message,
ctx.session.groupTag,
cache.config.anonymous_tickets,
autoReplyInfo,
),
);
},
);
}
export { chat };