-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
331 lines (290 loc) · 11.6 KB
/
app.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
const path = require('path');
require('dotenv').config();
process.env.TZ = 'Asia/Shanghai';
const crypto = require('crypto');
const Web3 = require('web3').Web3;
const MTProto = require('@mtproto/core');
const { sleep } = require('@mtproto/core/src/utils/common');
const { Telegraf } = require('telegraf');
const redis = require("redis");
const fs = require('fs');
const web3 = new Web3(process.env.WEB3_URL);
const bot = new Telegraf(process.env.BOT_TOKEN);
const client = redis.createClient({
url: process.env.REDIS_URL
});
client.on('error', (err) => {
console.log('Redis ' + err);
});
client.on('ready', () => {
console.log('Redis Ready');
});
client.connect().then(() => {
console.log('Redis Connected');
});
function dateFormat(fmt, date) {
let ret;
const opt = {
"Y+": date.getFullYear().toString(),
"m+": (date.getMonth() + 1).toString(),
"d+": date.getDate().toString(),
"H+": date.getHours().toString(),
"M+": date.getMinutes().toString(),
"S+": date.getSeconds().toString()
};
for (let k in opt) {
ret = new RegExp("(" + k + ")").exec(fmt);
if (ret) {
fmt = fmt.replace(ret[1], (ret[1].length === 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")));
}
}
return fmt;
}
const getTodayDateString = () => {
return dateFormat('YYYY-mm-dd', new Date());
}
const SPECIAL_CHARS = [
'\\',
'_',
'*',
'[',
']',
'(',
')',
'~',
'`',
'>',
'<',
'&',
'#',
'+',
'-',
'=',
'|',
'{',
'}',
'.',
'!'
]
const escapeMarkdown = (text) => {
text += '';
SPECIAL_CHARS.forEach(char => (text = text.replaceAll(char, `\\${char}`)))
return text
}
class API {
constructor() {
this.mtproto = new MTProto({
api_id: process.env.API_ID,
api_hash: process.env.API_HASH,
storageOptions: {
path: path.resolve(__dirname, './data/mtp.json'),
}
});
this.isDcId = false;
}
async call(method, params, options = {}) {
try {
if (this.isDcId) {
Object.assign(options, { dcId: this.isDcId });
}
const result = await this.mtproto.call(method, params, options);
return result;
} catch (error) {
console.log(`${method} error:`, error);
const { error_code, error_message } = error;
if (error_code === 420) {
const seconds = Number(error_message.split('FLOOD_WAIT_')[1]);
const ms = seconds * 1000;
await sleep(ms);
return this.call(method, params, options);
}
if (error_code === 303) {
const [type, dcIdAsString] = error_message.split('_MIGRATE_');
const dcId = Number(dcIdAsString);
this.isDcId = dcId;
// If auth.sendCode call on incorrect DC need change default DC, because
// call auth.signIn on incorrect DC return PHONE_CODE_EXPIRED error
if (type === 'PHONE') {
await this.mtproto.setDefaultDc(dcId);
} else {
Object.assign(options, { dcId });
}
return this.call(method, params, options);
}
return Promise.reject(error);
}
}
}
const api = new API();
const checkisAuthKey = () => {
const storage = JSON.parse(fs.readFileSync(path.resolve(__dirname, './data/mtp.json')));
if(storage['1authKey'] || storage['2authKey'] || storage['3authKey'] || storage['4authKey'] || storage['5authKey']) return true;
return false;
}
if(checkisAuthKey) {
api.mtproto.syncAuth(0).catch((e) => {
console.log('syncAuth error:', e);
});
} else {
api.call('auth.importBotAuthorization', {
flags: 0,
api_id: process.env.API_ID,
api_hash: process.env.API_HASH,
bot_auth_token: process.env.BOT_TOKEN,
}).then(result => {
console.log('result:', result);
}
).catch(error => {
console.log('error:', error);
});
}
const addSha256ToUserList = (list) => {
const Return = [];
for (const user of list) {
if(user.bot) continue;
const hash = crypto.createHash('sha256');
hash.update(`${user.id}.${getTodayDateString()}`);
user.sha256 = hash.digest('hex');
Return.push(user);
}
return Return;
}
const getChatMembers = async (chatId, chatType, userName) => {
const rawRedis = await client.get('waifu:chatMembers:' + getTodayDateString() + ':' + chatId);
if (rawRedis) return JSON.parse(rawRedis);
if(chatType === 'group') {
const FullChat = await api.call('messages.getFullChat', {
chat_id: chatId.toString().replace('-', ''),
});
const addSha256 = addSha256ToUserList(FullChat.users);
await client.set('waifu:chatMembers:' + getTodayDateString() + ':' + chatId, JSON.stringify(addSha256), 'EX', 86400);
return addSha256;
}
else {
const Channel = await api.call('channels.getChannels', {
id: [{
_: 'inputChannel',
channel_id: chatId.toString().replace('-100', ''),
access_hash: 0,
}],
});
const memberCount = await bot.telegram.getChatMembersCount(chatId);
let offset = 0;
const Return = [];
do {
const getParticipants = await api.call('channels.getParticipants', {
channel: {
_: 'inputChannel',
channel_id: Channel.chats[0].id,
access_hash: Channel.chats[0].access_hash,
},
filter: {
_: 'channelParticipantsRecent',
},
offset: offset,
limit: 200,
hash: 0,
});
for (const user of getParticipants.users) {
Return.push(user);
offset++;
}
}
while(offset < memberCount && offset < 1000);
const addSha256 = addSha256ToUserList(Return);
await client.set('waifu:chatMembers:' + getTodayDateString() + ':' + chatId, JSON.stringify(addSha256), 'EX', 86400);
return addSha256;
}
};
bot.command('eth_waifu', async (ctx) => {
ctx.originReply = ctx.reply;
ctx.reply = (text, extra) => {
ctx.originReply(text, extra).catch((e) => {
console.log(e);
});
}
console.log(ctx.chat)
if (ctx.chat.type === 'private') {
ctx.reply('请在群组中使用!', {
reply_to_message_id: ctx.message.message_id
});
return;
}
const chatId = ctx.chat.id;
const senderId = ctx.message.from.id;
const chatType = ctx.chat.type;
const userName = ctx.chat.username;
let chatMembers = await getChatMembers(chatId, chatType, userName);
let senderSha256;
try {
senderSha256 = chatMembers.find((user) => {
return Number(user.id) === senderId;
}).sha256;
}
catch (e) {
senderSha256 = crypto.createHash('sha256').update(`${senderId}.${getTodayDateString()}`).digest('hex');
}
const waifuResult = await client.get('waifu:result:' + getTodayDateString() + ':' + chatId + ':' + senderId);
try {
if (waifuResult) {
const waifuResultJSON = JSON.parse(waifuResult);
if(waifuResultJSON.waifu.photo) {
console.log(waifuResultJSON.waifu.id)
try {
const photos = await bot.telegram.getUserProfilePhotos(Number(waifuResultJSON.waifu.id));
if(photos.total_count > 0) {
const photo = photos.photos[0][photos.photos[0].length - 1];
ctx.replyWithPhoto(photo.file_id, {
caption: `今日的老婆是:[${escapeMarkdown(waifuResultJSON.waifu.first_name)} ${waifuResultJSON.waifu.last_name ? escapeMarkdown(waifuResultJSON.waifu.last_name) : ''}](tg://user?id=${waifuResultJSON.waifu.id})\n\n*结果溯源*\n你的Hash:\`${'0x' + waifuResultJSON.senderSha256}\`\nETH最新区块:\`${waifuResultJSON.blockNumber}\`\n最新区块Hash:\`${waifuResultJSON.blockHash}\`\n抽取基数:\`${waifuResultJSON.memberLength}\`\n时间戳:\`${waifuResultJSON.timestamp}\``,
parse_mode: 'MarkdownV2',
reply_to_message_id: ctx.message.message_id
});
return;
}
}
catch(e) {
}
}
ctx.reply(`今日的老婆是:[${escapeMarkdown(waifuResultJSON.waifu.first_name)} ${waifuResultJSON.waifu.last_name ? escapeMarkdown(waifuResultJSON.waifu.last_name) : ''}](tg://user?id=${waifuResultJSON.waifu.id})\n\n*结果溯源*\n你的Hash:\`${'0x' + waifuResultJSON.senderSha256}\`\nETH最新区块:\`${waifuResultJSON.blockNumber}\`\n最新区块Hash:\`${waifuResultJSON.blockHash}\`\n抽取基数:\`${waifuResultJSON.memberLength}\`\n时间戳:\`${waifuResultJSON.timestamp}\``, {
parse_mode: 'MarkdownV2',
reply_to_message_id: ctx.message.message_id
});
} else {
const blockNumber = await web3.eth.getBlockNumber();
const timestamp = new Date().getTime();
const block = await web3.eth.getBlock(blockNumber);
const decimalBlockHash = BigInt(block.hash).toString(10);
const decimalSenderHash = BigInt('0x' + senderSha256).toString(10);
const waifuId = (Number(decimalBlockHash) + Number(decimalSenderHash)) % chatMembers.length;
const waifu = chatMembers[waifuId];
await client.set('waifu:result:' + getTodayDateString() + ':' + chatId + ':' + senderId, JSON.stringify({waifu,blockNumber: blockNumber.toString(10),blockHash: block.hash,timestamp,senderSha256,memberLength: chatMembers.length}), 'EX', 86400);
console.log(senderId, waifu);
if(waifu.photo) {
try {
await bot.telegram.getChatMember(chatId, waifu.id);
const photos = await bot.telegram.getUserProfilePhotos(Number(waifu.id));
if(photos.total_count > 0) {
const photo = photos.photos[0][photos.photos[0].length - 1];
ctx.replyWithPhoto(photo.file_id, {
caption: `今日的老婆是:[${escapeMarkdown(waifu.first_name)} ${waifu.last_name ? escapeMarkdown(waifu.last_name) : ''}](tg://user?id=${waifu.id})\n\n*结果溯源*\n你的Hash:\`${'0x' + senderSha256}\`\nETH最新区块:\`${blockNumber.toString(10)}\`\n最新区块Hash:\`${block.hash}\`\n抽取基数:\`${chatMembers.length}\`\n时间戳:\`${timestamp}\``,
parse_mode: 'MarkdownV2',
reply_to_message_id: ctx.message.message_id
});
return;
}
}
catch(e) {
console.log(e);
}
}
ctx.reply(`今日的老婆是:[${escapeMarkdown(waifu.first_name)} ${waifu.last_name ? escapeMarkdown(waifu.last_name) : ''}](tg://user?id=${waifu.id})\n\n*结果溯源*\n你的Hash:\`${'0x' + senderSha256}\`\nETH最新区块:\`${blockNumber.toString(10)}\`\n最新区块Hash:\`${block.hash}\`\n抽取基数:\`${chatMembers.length}\`\n时间戳:\`${timestamp}\``, {
parse_mode: 'MarkdownV2',
reply_to_message_id: ctx.message.message_id
});
}
}
catch (e) {
console.log(e);
}
});
bot.launch();