forked from WidowSSS/Telegram-ShuangXiangBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
146 lines (129 loc) · 5.98 KB
/
bot.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
require('dotenv').config();
const TelegramBot = require('node-telegram-bot-api');
const sqlite3 = require('sqlite3').verbose();
const { TELEGRAM_BOT_TOKEN, ADMIN_ID, COMMAND_BAN, COMMAND_UNBAN, COMMAND_UNBANTEXT, WELCOME_MESSGAE, COMMAND_SEND } = process.env;
const bot = new TelegramBot(TELEGRAM_BOT_TOKEN, { polling: true, request: { family: 4 } });
const db = new sqlite3.Database('ShuangXiang.db');
db.run("CREATE TABLE IF NOT EXISTS user (id INTEGER PRIMARY KEY, telegram_id INTEGER UNIQUE, telegram_name TEXT, is_blacklisted INTEGER)");
if (WELCOME_MESSGAE == null || WELCOME_MESSGAE == "") {
const message = `请检查env环境配置文件.配置欢迎消息`;
bot.sendMessage(chatId, message, { parse_mode: 'HTML' });
return;
}
if (COMMAND_UNBANTEXT == null || COMMAND_UNBANTEXT == "") {
const message = `请检查env环境配置文件.配置封禁消息`;
bot.sendMessage(chatId, message, { parse_mode: 'HTML' });
return;
}
bot.on('message', (msg) => {
const { id: chatId, type: chatType, title: groupName } = msg.chat;
const { id: telegramId, username: telegramName } = msg.from;
if (msg.text != null) {
if ('/start'.includes(msg.text)) {
if (chatId != ADMIN_ID) {
const message = `${WELCOME_MESSGAE}`;
bot.sendMessage(chatId, message, { parse_mode: 'HTML' });
}
return;
}
if (msg.text.startsWith(COMMAND_SEND)) {
const parts = msg.text.split(' ');
const args = parts.slice(1);
if (chatId == ADMIN_ID) {
if (args.length < 1) {
const errorMessage = `参数错误,正确格式为:${COMMAND_SEND} + 消息`;
bot.sendMessage(chatId, errorMessage);
} else {
db.all('SELECT telegram_id FROM user', (err, rows) => {
if (err) {
console.error('查询数据库时出错:', err);
return;
}
rows.forEach((row) => {
const userId = row.telegram_id;
if (userId != ADMIN_ID) {
sendMessageToUser(userId, args.join(' '));
}
});
});
}
}
}
if (['/ID', '/id'].includes(msg.text)) {
const message = (chatType === 'group' || chatType === 'supergroup') ?
`群组用户名: <b>${groupName}</b>\n群组ID: <b>${chatId}</b>` :
`电报用户名: <b>${telegramName}</b>\n电报ID: <b>${telegramId}</b>`;
bot.sendMessage(chatId, message, { parse_mode: 'HTML' });
return;
}
}
const insertQuery = `INSERT OR IGNORE INTO user (telegram_id, telegram_name, is_blacklisted) VALUES (?, ?, 0)`;
if (chatId != ADMIN_ID) {
db.run(insertQuery, [telegramId, telegramName], err => err && console.error('插入数据库时出错:', err));
}
if (chatId == ADMIN_ID && msg.reply_to_message && msg.reply_to_message.forward_from) {
const originalUserId = msg.reply_to_message.forward_from.id;
let actionMessage;
let blacklistStatus;
if (msg.text != null) {
if (msg.text === COMMAND_BAN) {
actionMessage = `用户 ID: ${originalUserId} 已被封禁.`;
blacklistStatus = 1;
} else if (msg.text === COMMAND_UNBAN) {
actionMessage = `用户 ID: ${originalUserId} 已被解封.`;
blacklistStatus = 0;
}
if (actionMessage) {
const updateQuery = `UPDATE user SET is_blacklisted = ${blacklistStatus} WHERE telegram_id = ?`;
db.run(updateQuery, [originalUserId], err => {
const feedback = err ? '发生错误,请稍后重试.' : actionMessage;
bot.sendMessage(chatId, feedback);
});
return;
}
bot.sendMessage(originalUserId, `<b>${msg.text}</b>`, { parse_mode: 'HTML' });
} else if (msg.photo) {
const fileId = msg.photo[msg.photo.length - 1].file_id;
bot.sendPhoto(originalUserId, fileId, { caption: msg.caption })
} else if (msg.document) {
const fileId = msg.document.file_id;
bot.sendDocument(originalUserId, fileId, { caption: msg.caption })
} else if (msg.video) {
const fileId = msg.video.file_id;
bot.sendVideo(originalUserId, fileId, { caption: msg.caption })
}else if (msg.audio) {
const fileId = msg.audio.file_id;
bot.sendAudio(originalUserId, fileId, { caption: msg.caption })
}
return;
}
isUserBlacklisted(telegramId, (err, blacklisted) => {
if (err) {
console.error('查询黑名单状态时出错:', err);
bot.sendMessage(chatId, '发生错误,请稍后重试.');
} else if (blacklisted) {
bot.sendMessage(chatId, `${COMMAND_UNBANTEXT}`);
} else if (chatId != ADMIN_ID) {
bot.forwardMessage(ADMIN_ID, chatId, msg.message_id);
}
});
});
function isUserBlacklisted(telegramId, callback) {
const query = 'SELECT is_blacklisted FROM user WHERE telegram_id = ?';
db.get(query, [telegramId], (err, row) => {
if (err) {
callback(err);
} else {
callback(null, row && row.is_blacklisted === 1);
}
});
}
function sendMessageToUser(userId, messageText) {
bot.sendMessage(userId, messageText)
.then(() => {
console.log(`已向用户 ${userId} 发送消息: ${messageText}`);
})
.catch((error) => {
console.error(`向用户 ${userId} 发送消息时出错:`, error);
});
}