forked from fengnz/fengdoorbot
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Common.js
257 lines (212 loc) · 5.63 KB
/
Common.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
function debugCommons(){
var date = getChineseDateString(new Date());
console.log(date);
}
// Use this method to sort a array randomly
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function checkIsMyMessage(body){
var text = body.message.text;
if (text) {
var paras = text.split(" ");
if (paras[0] && paras[0].indexOf("@") >= 0){
if (paras[0].toLowerCase().indexOf(Const.myName) >= 0){
return true;
}
return false;
}
}
return true;
}
function isAdminOrCreator(userId, chatId) {
payload = {
"method": "getChatMember",
"chat_id": chatId,
"user_id": userId
};
// allow feng
if (userId == 539065210) {
return true;
}
var chatMember = postTelegram(payload);
if (chatMember && chatMember.ok === true) {
if (chatMember.result.status == "creator") {
return true;
}
if (chatMember.result.status == "administrator") {
return true;
}
}
return false;
}
function isValidChannelId(id){
return /^[\dA-z_-]{24}$/.test(id);
}
function isValidTelegramChatId(id){
var isValidId = false;
if (isNaN(id)) {
if (id.indexOf("@") != 0) {
} else {
isValidId = true;
}
} else {
// Number is valid Id
if (/[\d-]{8,}/.test(id + "")) {
isValidId = true;
} else {
}
}
return isValidId;
}
function getShareObject(body) {
var payload;
var from;
var chat;
var messageId;
var callback_query_id;
var origText;
var share = {};
// use editMessage method instead if it's a callback
if (body.callback_query) {
chat = body.callback_query.message.chat;
chat.id = chat.id + "";
messageId = body.callback_query.message.message_id;
from = body.callback_query.from;
callback_query_id = body.callback_query.id;
origText = body.callback_query.message.text;
payload = {
"method": "editMessageText",
"chat_id": chat.id,
"message_id": messageId,
"text": "",
"parse_mode": "markdown",
"disable_web_page_preview": true,
};
} else {
from = body.message.from;
chat = body.message.chat;
chat.id = chat.id + "";
messageId = body.message.message_id;
origText = body.message.text;
payload = {
"method": "sendMessage",
"chat_id": chat.id,
"text": "",
"parse_mode": "markdown",
"disable_web_page_preview": true,
};
if (body.message.new_chat_member) {
share.new_chat_member = body.message.new_chat_member;
}
}
share.payload = payload;
share.from = from;
share.chat = chat;
share.messageId = messageId;
share.callback_query_id = callback_query_id;
share.origText = origText;
return share;
}
function extractVideoIds(text) {
var VideoIds = text.replace(/https:\/\/youtu.be\//gi, "");
return VideoIds;
}
function escapeMarkDown(toEscapeMsg) {
var escapedMsg = toEscapeMsg
.replace(/_/g, "\\_")
.replace(/\*/g, "\\*")
.replace(/\[/g, "\\[")
.replace(/\]/g, "\\]")
.replace(/`/g, "\\`");
return escapedMsg;
}
function addDeleteMeButton(payload) {
var buttons = [];
var button1 = {
text: "点此删除本消息",
callback_data: "delme"
};
buttons.push(button1);
var inlineKeyboardMarkup = generateInlineKeyboardMarkup(buttons, 3);
payload.reply_markup = inlineKeyboardMarkup;
}
function extractChannelId(text) {
var channelId = text.replace(/https:\/\/www.youtube.com\/channel\//i, "");
return channelId;
}
function getGroupName(chat){
if(chat.username) {
return getMarkDownGroupUrl(chat.title, chat.username);
}
return escapeMarkDown(chat.title);
}
function getMentionName(user) {
var mentionName = "";
var name = getName(user);
if (!name) {
name = "神秘人";
}
mentionName = getMarkDownUserUrl(name, user.id);
return mentionName;
}
function getMarkDownGroupUrl(title, chatUserName) {
return getMarkDownUrl(title, "https://t.me/" + chatUserName);
}
function getMarkDownUserUrl(userName, userId) {
return getMarkDownUrl(userName, "tg://user?id=" + userId);
}
function getMarkDownUrl(text, url) {
return "[" + escapeMarkDown(text) + "](" + url + ")";
}
function getChannelUrlById(id) {
return "https://www.youtube.com/channel/" + id;
}
function getChineseDateString(nzTime){
var utcTime = new Date(nzTime.getTime() + nzTime.getTimezoneOffset() * 60000);
var chinaTime = new Date(utcTime.getTime() + 8 * 60 * 60 * 1000);
var year = chinaTime.getFullYear()+'年';
var month = chinaTime.getMonth()+1+'月';
var date = chinaTime.getDate()+'日';
var hours = chinaTime.getHours() + '时';
var minutes = chinaTime.getMinutes() + '分';
return [year,month,date, hours,minutes].join('');
}
function getName(user) {
var name = user.first_name;
if (user.last_name) {
name += " " + user.last_name;
}
return name;
}
function escapeRegex(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
function pad(pad, str, padLeft) {
if (typeof str === 'undefined')
return pad;
if (padLeft) {
return (pad + str).slice(-pad.length);
} else {
return (str + pad).substring(0, pad.length);
}
}
function isPrivate(body) {
var share = getShareObject(body);
var chat = share.chat;
if (chat.type === "private") {
return true;
}
return false;
}