index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. // const TelegramBot = require('node-telegram-bot-api');
  2. // const express = require('express');
  3. // const fs = require('fs');
  4. // const moment = require('moment');
  5. // const config = require('./config');
  6. // const { createGroup, updateGroup } = require('./admin/controllers/groupController');
  7. // // 初始化数据存储
  8. // let data = {
  9. // deposits: [], // 入款记录
  10. // withdrawals: [], // 下发记录
  11. // lastUpdate: null,
  12. // allowedGroups: ['4754375683'] // 允许使用的群组ID
  13. // };
  14. // // 加载数据
  15. // function loadData() {
  16. // try {
  17. // if (fs.existsSync(config.DB_FILE)) {
  18. // const savedData = JSON.parse(fs.readFileSync(config.DB_FILE));
  19. // data = { ...data, ...savedData };
  20. // }
  21. // } catch (error) {
  22. // console.error('Error loading data:', error);
  23. // }
  24. // }
  25. // // 保存数据
  26. // function saveData() {
  27. // try {
  28. // fs.writeFileSync(config.DB_FILE, JSON.stringify(data, null, 2));
  29. // } catch (error) {
  30. // console.error('Error saving data:', error);
  31. // }
  32. // }
  33. // // 创建机器人实例
  34. // const bot = new TelegramBot(config.BOT_TOKEN, { polling: true });
  35. // // 创建Express应用
  36. // const app = express();
  37. // // 检查群组权限
  38. // function isGroupAllowed(chatId) {
  39. // const chatIdStr = chatId.toString();
  40. // return data.allowedGroups.includes(chatIdStr) ||
  41. // data.allowedGroups.includes(chatIdStr.replace('-', ''));
  42. // }
  43. // // 检查是否是管理员
  44. // function isAdmin(userId) {
  45. // return config.ADMIN_IDS.includes(userId.toString());
  46. // }
  47. // // 处理新成员加入
  48. // bot.on('new_chat_members', async (msg) => {
  49. // const chatId = msg.chat.id;
  50. // const newMembers = msg.new_chat_members;
  51. // for (const member of newMembers) {
  52. // if (member.id === (await bot.getMe()).id) {
  53. // // 机器人被添加到群组
  54. // console.log(`机器人被添加到群组: ${chatId}`);
  55. // // 检查群组是否在允许列表中
  56. // const chatIdStr = chatId.toString();
  57. // if (!data.allowedGroups.includes(chatIdStr)) {
  58. // try {
  59. // // 使用 createGroup 创建新群组
  60. // const groupData = {
  61. // groupId: chatIdStr,
  62. // groupName: msg.chat.title || '未命名群组',
  63. // groupType: msg.chat.type === 'private' ? 'personal' : 'public'
  64. // };
  65. // const result = await createGroup({ body: groupData });
  66. // if (result) {
  67. // // 更新内存中的群组列表
  68. // data.allowedGroups.push(chatIdStr);
  69. // saveData();
  70. // sendMessage(chatId, '感谢添加我为群组成员!使用 /help 查看可用命令。');
  71. // } else {
  72. // sendMessage(chatId, '添加群组失败,请联系管理员。');
  73. // }
  74. // } catch (error) {
  75. // console.error('创建群组失败:', error);
  76. // sendMessage(chatId, '添加群组失败,请联系管理员。');
  77. // }
  78. // } else {
  79. // sendMessage(chatId, '感谢添加我为群组成员!使用 /help 查看可用命令。');
  80. // }
  81. // } else {
  82. // // 其他新成员
  83. // console.log(`新成员加入群组: ${member.username || member.first_name} (${member.id})`);
  84. // sendMessage(chatId, `欢迎 ${member.username || member.first_name} 加入群组!`);
  85. // }
  86. // }
  87. // });
  88. // // 处理成员离开
  89. // bot.on('left_chat_member', (msg) => {
  90. // const chatId = msg.chat.id;
  91. // const leftMember = msg.left_chat_member;
  92. // if (leftMember.id === bot.getMe().then(me => me.id)) {
  93. // // 机器人被移除
  94. // console.log(`机器人被移除出群组: ${chatId}`);
  95. // } else {
  96. // // 其他成员离开
  97. // console.log(`成员离开群组: ${leftMember.username || leftMember.first_name} (${leftMember.id})`);
  98. // sendMessage(chatId, `${leftMember.username || leftMember.first_name} 离开了群组。`);
  99. // }
  100. // });
  101. // // 处理群组标题更改
  102. // bot.on('new_chat_title', (msg) => {
  103. // console.log(`群组标题更改: ${msg.chat.title} (${msg.chat.id})`);
  104. // sendMessage(msg.chat.id, `群组标题已更改为: ${msg.chat.title}`);
  105. // });
  106. // // 处理群组照片更改
  107. // bot.on('new_chat_photo', (msg) => {
  108. // console.log(`群组照片更改: ${msg.chat.id}`);
  109. // sendMessage(msg.chat.id, '群组照片已更新!');
  110. // });
  111. // // 处理消息删除
  112. // bot.on('delete_chat_photo', (msg) => {
  113. // console.log(`群组照片被删除: ${msg.chat.id}`);
  114. // sendMessage(msg.chat.id, '群组照片已被删除。');
  115. // });
  116. // // 处理管理员命令
  117. // bot.onText(/\/addgroup (.+)/, async (msg, match) => {
  118. // if (!isAdmin(msg.from.id)) {
  119. // sendMessage(msg.chat.id, '您没有权限执行此命令。');
  120. // return;
  121. // }
  122. // const groupId = match[1].trim();
  123. // if (!data.allowedGroups.includes(groupId)) {
  124. // try {
  125. // // 使用 createGroup 创建新群组
  126. // const groupData = {
  127. // groupId: groupId,
  128. // groupName: '手动添加的群组',
  129. // groupType: 'public'
  130. // };
  131. // const result = await createGroup({ body: groupData });
  132. // if (result) {
  133. // data.allowedGroups.push(groupId);
  134. // saveData();
  135. // sendMessage(msg.chat.id, `群组 ${groupId} 已添加到允许列表。`);
  136. // } else {
  137. // sendMessage(msg.chat.id, '添加群组失败,请检查群组ID是否正确。');
  138. // }
  139. // } catch (error) {
  140. // console.error('创建群组失败:', error);
  141. // sendMessage(msg.chat.id, '添加群组失败,请稍后重试。');
  142. // }
  143. // } else {
  144. // sendMessage(msg.chat.id, '该群组已在允许列表中。');
  145. // }
  146. // });
  147. // bot.onText(/\/removegroup (.+)/, async (msg, match) => {
  148. // if (!isAdmin(msg.from.id)) {
  149. // sendMessage(msg.chat.id, '您没有权限执行此命令。');
  150. // return;
  151. // }
  152. // const groupId = match[1].trim();
  153. // try {
  154. // // 使用 updateGroup 更新群组状态
  155. // const result = await updateGroup({
  156. // params: { id: groupId },
  157. // body: { isActive: false }
  158. // });
  159. // if (result) {
  160. // const index = data.allowedGroups.indexOf(groupId);
  161. // if (index > -1) {
  162. // data.allowedGroups.splice(index, 1);
  163. // saveData();
  164. // sendMessage(msg.chat.id, `群组 ${groupId} 已从允许列表中移除。`);
  165. // } else {
  166. // sendMessage(msg.chat.id, '该群组不在允许列表中。');
  167. // }
  168. // } else {
  169. // sendMessage(msg.chat.id, '移除群组失败,请稍后重试。');
  170. // }
  171. // } catch (error) {
  172. // console.error('更新群组状态失败:', error);
  173. // sendMessage(msg.chat.id, '移除群组失败,请稍后重试。');
  174. // }
  175. // });
  176. // bot.onText(/\/listgroups/, (msg) => {
  177. // if (!isAdmin(msg.from.id)) {
  178. // sendMessage(msg.chat.id, '您没有权限执行此命令。');
  179. // return;
  180. // }
  181. // const groupsList = data.allowedGroups.join('\n');
  182. // sendMessage(msg.chat.id, `允许的群组列表:\n${groupsList}`);
  183. // });
  184. // // 处理所有消息
  185. // bot.on('message', (msg) => {
  186. // console.log('收到消息:', {
  187. // chatId: msg.chat.id,
  188. // chatType: msg.chat.type,
  189. // chatTitle: msg.chat.title,
  190. // fromId: msg.from.id,
  191. // fromUsername: msg.from.username,
  192. // text: msg.text,
  193. // date: new Date(msg.date * 1000).toLocaleString()
  194. // });
  195. // });
  196. // // 处理错误
  197. // bot.on('polling_error', (error) => {
  198. // console.error('轮询错误:', error);
  199. // if (error.message.includes('bot was kicked from the group chat')) {
  200. // const chatId = error.message.match(/chat (\d+)/)?.[1];
  201. // if (chatId) {
  202. // // 从允许列表中移除被踢出的群组
  203. // const index = data.allowedGroups.indexOf(chatId);
  204. // if (index > -1) {
  205. // data.allowedGroups.splice(index, 1);
  206. // saveData();
  207. // console.log(`群组 ${chatId} 已被移除出允许列表`);
  208. // }
  209. // }
  210. // }
  211. // });
  212. // bot.on('webhook_error', (error) => {
  213. // console.error('Webhook错误:', error);
  214. // });
  215. // // 处理消息发送错误
  216. // bot.on('error', (error) => {
  217. // console.error('机器人错误:', error);
  218. // if (error.message.includes('bot was kicked from the group chat')) {
  219. // const chatId = error.message.match(/chat (\d+)/)?.[1];
  220. // if (chatId) {
  221. // // 从允许列表中移除被踢出的群组
  222. // const index = data.allowedGroups.indexOf(chatId);
  223. // if (index > -1) {
  224. // data.allowedGroups.splice(index, 1);
  225. // saveData();
  226. // console.log(`群组 ${chatId} 已被移除出允许列表`);
  227. // }
  228. // }
  229. // }
  230. // });
  231. // // 处理消息发送
  232. // async function sendMessage(chatId, text, options = {}) {
  233. // try {
  234. // return await bot.sendMessage(chatId, text, options);
  235. // } catch (error) {
  236. // console.error('发送消息失败:', error);
  237. // if (error.message.includes('bot was kicked from the group chat')) {
  238. // const index = data.allowedGroups.indexOf(chatId.toString());
  239. // if (index > -1) {
  240. // data.allowedGroups.splice(index, 1);
  241. // saveData();
  242. // console.log(`群组 ${chatId} 已被移除出允许列表`);
  243. // }
  244. // }
  245. // return null;
  246. // }
  247. // }
  248. // // 生成账单消息
  249. // function generateBillMessage() {
  250. // const now = moment();
  251. // const deposits = data.deposits || [];
  252. // const withdrawals = data.withdrawals || [];
  253. // const totalDeposit = deposits.reduce((sum, d) => sum + d.amount, 0);
  254. // const totalWithdrawal = withdrawals.reduce((sum, w) => sum + w.amount, 0);
  255. // const depositFee = totalDeposit * config.DEPOSIT_FEE_RATE;
  256. // const withdrawalFee = totalWithdrawal * config.WITHDRAWAL_FEE_RATE;
  257. // const remaining = totalDeposit - depositFee - totalWithdrawal - withdrawalFee;
  258. // let message = `入款(${deposits.length})笔:\n`;
  259. // // 添加入款记录
  260. // deposits.forEach(deposit => {
  261. // message += `${moment(deposit.time).format('HH:mm:ss')} ${deposit.amount.toFixed(2)}\n`;
  262. // });
  263. // message += `\n下发(${withdrawals.length})笔:\n`;
  264. // // 添加下发记录
  265. // withdrawals.forEach(withdrawal => {
  266. // message += `${moment(withdrawal.time).format('HH:mm:ss')} ${withdrawal.amount.toFixed(2)}\n`;
  267. // });
  268. // message += `\n总入款:${totalDeposit.toFixed(2)}\n`;
  269. // message += `入款费率:${(config.DEPOSIT_FEE_RATE * 100).toFixed(1)}%\n`;
  270. // message += `下发费率:${(config.WITHDRAWAL_FEE_RATE * 100).toFixed(1)}%\n`;
  271. // message += `应下发:${(totalDeposit - depositFee).toFixed(2)}\n`;
  272. // message += `总下发:${totalWithdrawal.toFixed(2)}\n`;
  273. // message += `下发单笔附加费:0.0\n`;
  274. // message += `单笔附费加总计:0.0\n`;
  275. // message += `余:${remaining.toFixed(2)}`;
  276. // return message;
  277. // }
  278. // // 生成内联键盘
  279. // function generateInlineKeyboard() {
  280. // return {
  281. // inline_keyboard: [
  282. // [
  283. // {
  284. // text: '点击跳转完整账单',
  285. // url: `${config.BILL_PAGE_BASE_URL}?groupId=${data.allowedGroups[0]}`
  286. // }
  287. // ],
  288. // [
  289. // {
  290. // text: '24小时商务对接',
  291. // url: 'https://t.me/your_business_account' // 请替换为实际的商务账号
  292. // }
  293. // ]
  294. // ]
  295. // };
  296. // }
  297. // // 处理入款命令
  298. // bot.onText(/\/deposit (.+)/, (msg, match) => {
  299. // // if (!isGroupAllowed(msg.chat.id)) {
  300. // // bot.sendMessage(msg.chat.id, '此群组未授权使用此机器人');
  301. // // return;
  302. // // }
  303. // const amount = parseFloat(match[1]);
  304. // if (isNaN(amount)) {
  305. // sendMessage(msg.chat.id, '请输入有效的金额');
  306. // return;
  307. // }
  308. // data.deposits.push({
  309. // time: new Date(),
  310. // amount: amount
  311. // });
  312. // data.lastUpdate = new Date();
  313. // saveData();
  314. // sendMessage(msg.chat.id, generateBillMessage(), {
  315. // reply_markup: generateInlineKeyboard()
  316. // });
  317. // });
  318. // // 处理下发命令
  319. // bot.onText(/\/withdraw (.+)/, (msg, match) => {
  320. // // if (!isGroupAllowed(msg.chat.id)) {
  321. // // bot.sendMessage(msg.chat.id, '此群组未授权使用此机器人');
  322. // // return;
  323. // // }
  324. // const amount = parseFloat(match[1]);
  325. // if (isNaN(amount)) {
  326. // sendMessage(msg.chat.id, '请输入有效的金额');
  327. // return;
  328. // }
  329. // data.withdrawals.push({
  330. // time: new Date(),
  331. // amount: amount
  332. // });
  333. // data.lastUpdate = new Date();
  334. // saveData();
  335. // sendMessage(msg.chat.id, generateBillMessage(), {
  336. // reply_markup: generateInlineKeyboard()
  337. // });
  338. // });
  339. // // 处理查看账单命令
  340. // bot.onText(/\/bill/, (msg) => {
  341. // // if (!isGroupAllowed(msg.chat.id)) {
  342. // // bot.sendMessage(msg.chat.id, '此群组未授权使用此机器人');
  343. // // return;
  344. // // }
  345. // sendMessage(msg.chat.id, generateBillMessage(), {
  346. // reply_markup: generateInlineKeyboard()
  347. // });
  348. // });
  349. // // 更新帮助命令
  350. // bot.onText(/\/help/, (msg) => {
  351. // const helpMessage = `
  352. // 可用命令:
  353. // /deposit <金额> - 记录入款
  354. // /withdraw <金额> - 记录下发
  355. // /bill - 查看当前账单
  356. // /help - 显示此帮助信息
  357. // 管理员命令:
  358. // /addgroup <群组ID> - 添加允许的群组
  359. // /removegroup <群组ID> - 移除允许的群组
  360. // /listgroups - 列出所有允许的群组
  361. // `;
  362. // sendMessage(msg.chat.id, helpMessage);
  363. // });
  364. // // 启动Express服务器
  365. // app.listen(8080, () => {
  366. // console.log(`Server is running on port 8080`);
  367. // loadData();
  368. // console.log('Bot is ready to use!');
  369. // });