diff --git a/CHANGELOG.md b/CHANGELOG.md index caba94d..d300b79 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,54 +2,22 @@ All notable changes to this project will be documented in this file,ChangeLog information is generated by the [CommitMessage plugin](https://plugins.jetbrains.com/plugin/12256-commit-message-create) -## [v1.1.8](https://github.com/javadib/v2-xbot/compare/v1.1.8...master) +## [v1.1.9.0](https://gitlab.com/javadib/v2proxy/compare/v1.1.9.0...master) + + +### feat + +* refactor message workflow ([b37fa3f](https://gitlab.com/javadib/v2proxy/commit/b37fa3f)) +* import v2xBot submodule ([2e843f4](https://gitlab.com/javadib/v2proxy/commit/2e843f4)) +* implement update & extend account ([d0b0217](https://gitlab.com/javadib/v2proxy/commit/d0b0217)) +* hiddify - getAccout info ([286a493](https://gitlab.com/javadib/v2proxy/commit/286a493)) ### fix -* Remove url from payment creation ([debb4c2](https://github.com/javadib/v2-xbot/commit/debb4c2)) -* basic validation for crete methods ([713d181](https://github.com/javadib/v2-xbot/commit/713d181)) -* some bugs patched ([72985ef](https://github.com/javadib/v2-xbot/commit/72985ef)) -* comment in create account fixed ([4054b01](https://github.com/javadib/v2-xbot/commit/4054b01)) -* buy workflow refactor ([5f05c3a](https://github.com/javadib/v2-xbot/commit/5f05c3a)) -* server.name to .title ([bba60d9](https://github.com/javadib/v2-xbot/commit/bba60d9)) -* plan deletebyId ([96ffde7](https://github.com/javadib/v2-xbot/commit/96ffde7)) -* create plan buttons ([f8709e5](https://github.com/javadib/v2-xbot/commit/f8709e5)) -* some bug & refactor ([746291a](https://github.com/javadib/v2-xbot/commit/746291a)) -* createPlan data map ([c1a9bfb](https://github.com/javadib/v2-xbot/commit/c1a9bfb)) -* async bug in findall method ([47f32f3](https://github.com/javadib/v2-xbot/commit/47f32f3)) -* getButtons preVmd ([c55d6c2](https://github.com/javadib/v2-xbot/commit/c55d6c2)) -* textIcon ([db7c20d](https://github.com/javadib/v2-xbot/commit/db7c20d)) -* dist path ([ffac0fb](https://github.com/javadib/v2-xbot/commit/ffac0fb)) -* Remove junk data ([b648ec9](https://github.com/javadib/v2-xbot/commit/b648ec9)) -* account name patched ([cf809a1](https://github.com/javadib/v2-xbot/commit/cf809a1)) -* better account name ([2497631](https://github.com/javadib/v2-xbot/commit/2497631)) -* better account name ([3b3dab3](https://github.com/javadib/v2-xbot/commit/3b3dab3)) -* some logical bug ([50ba3f7](https://github.com/javadib/v2-xbot/commit/50ba3f7)) -* getNewOrderButtons fucky fixed ([fa394ae](https://github.com/javadib/v2-xbot/commit/fa394ae)) -* some uig ([f5c1341](https://github.com/javadib/v2-xbot/commit/f5c1341)) -* payment ino -- welcomeMessage in config ([dc00e6a](https://github.com/javadib/v2-xbot/commit/dc00e6a)) -* payment findById ([0d3a147](https://github.com/javadib/v2-xbot/commit/0d3a147)) -* rejected order ([5cc9b1e](https://github.com/javadib/v2-xbot/commit/5cc9b1e)) -* userSession nll ref ([c98c5c8](https://github.com/javadib/v2-xbot/commit/c98c5c8)) -* confirm_order key fixed ([097b0c9](https://github.com/javadib/v2-xbot/commit/097b0c9)) -* admin buttons & reloa buttons ([fe845c8](https://github.com/javadib/v2-xbot/commit/fe845c8)) -* admin buttons ([3dce7bd](https://github.com/javadib/v2-xbot/commit/3dce7bd)) -* some bug2 ([5d7639c](https://github.com/javadib/v2-xbot/commit/5d7639c)) -* some bug2 ([034498c](https://github.com/javadib/v2-xbot/commit/034498c)) -* some bug ([4890538](https://github.com/javadib/v2-xbot/commit/4890538)) -* invoice message ([491ad27](https://github.com/javadib/v2-xbot/commit/491ad27)) -* b29f882 some bug fixed ([b29f882](https://github.com/javadib/v2-xbot/commit/b29f882)) -* model buttons ([8e5e8d8](https://github.com/javadib/v2-xbot/commit/8e5e8d8)) - - -### refactor - -* move telegram api to module ([c27163b](https://github.com/javadib/v2-xbot/commit/c27163b)) -* remove payProofMessageId ([f180456](https://github.com/javadib/v2-xbot/commit/f180456)) -* remove logs ([8b397ca](https://github.com/javadib/v2-xbot/commit/8b397ca)) -* delete extra modules ([6a4989b](https://github.com/javadib/v2-xbot/commit/6a4989b)) -* payment.ts to js ([95c909b](https://github.com/javadib/v2-xbot/commit/95c909b)) -* server.ts to js ([5cd3de5](https://github.com/javadib/v2-xbot/commit/5cd3de5)) -* plan.ts to js ([e26fd1e](https://github.com/javadib/v2-xbot/commit/e26fd1e)) +* some bugs fixed ([8ff8702](https://gitlab.com/javadib/v2proxy/commit/8ff8702)) +* update doc ([0827829](https://gitlab.com/javadib/v2proxy/commit/0827829)) +* fix parseInput for all db models ([da68934](https://gitlab.com/javadib/v2proxy/commit/da68934)) +* adminId to number & some bug fixed ([325f358](https://gitlab.com/javadib/v2proxy/commit/325f358)) +* daat outpput in create accout api ([4b4a334](https://gitlab.com/javadib/v2proxy/commit/4b4a334)) diff --git a/README.md b/README.md index b915d42..f8059b3 100644 --- a/README.md +++ b/README.md @@ -15,13 +15,15 @@ v2-xBot is multi panel, serverless (cloudflare worker) bot to order management & - Manage (CRUD) `Servers` in bot - Manage (CRUD) `Payments` methods in bot - [x] Apps & link (for users) +- [x] Custom Welcome text +- [x] Seed data for `clientApps` - [ ] User Profile - [ ] Affiliate system # # Get Started 0) Create a cloudflare worker, KV namespace & bind to `db` variable -1) Got to worker settings, variable section, and add following variables: +1) Got to worker `settings`, `variables` section, and add following variables: - `adminId` : your Telegram Admin ID (get from [MyIdInfoBot](https://t.me/Myidinfobot)). - `botToken` : your Telegram bot token. - `tlgSupport` : your support Telegram username (Remember put `@` at the first). diff --git a/package.json b/package.json index ba8a7f8..13c1cc9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,7 @@ { "name": "v2-xbot", - "version": "0.0.0.0", + "description": "A multi panel, serverless (cloudflare worker) bot to order management & VPN Accounting", + "version": "1.1.9.0", "author": "javadib", "repository": "https://github.com/javadib/v2-xbot.git", "scripts": { diff --git a/src/config.js b/src/config.js index 3e7551c..ff7ffac 100644 --- a/src/config.js +++ b/src/config.js @@ -1,5 +1,7 @@ "use strict"; +const pkg = require('../package.json'); + const config = { commands: { silentButton: "/silentButton", @@ -12,8 +14,21 @@ const config = { token: botToken, webHook: '/endpoint', secret: "123456789wertyuiopxcvbnmDGHJKRTYIO", // A-Z, a-z, 0-9, _ and - - welcomeMessage(customText) { - return customText || `سلام به شما دوست گرامی! 🌟 + aboutBot() { + return `✳️✳✳️✳️ + +${pkg.name} +${pkg.description} + +Current version: ${pkg.version} + +${pkg.repository} + +Join channel 🤙: https://t.me/v2_xBot +` + }, + welcomeMessage() { + return `سلام به شما دوست گرامی! 🌟 به ربات V2xBot خوش آمدید! 🎉 از اینکه اینجا هستید، بسیار خوشحالیم. ما اینجا هستیم تا به شما کمک کنیم و هر سوال یا درخواستی که دارید را رفع کنیم. diff --git a/src/index.js b/src/index.js index e44fe25..94235b8 100644 --- a/src/index.js +++ b/src/index.js @@ -10,12 +10,14 @@ const Admin = require('./models/admin'); const ClientApp = require('./models/client-app'); const Command = require('./models/command'); const App = require('./models/app'); -const app = new App(db); - -const DataModel = {Plan, Order, Payment, Server, ClientApp}; const wKV = require('./modules/wkv'); const wkv = new wKV(db); +const app = new App(wkv); + + +const DataModel = {Plan, Order, Payment, Server, ClientApp, app}; + const Hiddify = require("./modules/hiddify"); const Telegram = require("./modules/telegram"); @@ -25,9 +27,10 @@ const WEBHOOK = Config.bot.webHook const SECRET = Config.bot.secret; const TlgBot = new Telegram(Config.bot.token); -const env = typeof env !== 'undefined' && env ? env : "production"; -const enableLog = typeof enableLog !== 'undefined' ? enableLog : false; +const env = typeof env !== 'undefined' && env ? env : "development"; +const enableLog = typeof enableLog !== 'undefined' && enableLog ? Boolean(enableLog) : false; const Logger = !enableLog || env === 'production' ? console : TlgBot; +// const Logger = TlgBot; Object.prototype.transform = function (text) { @@ -73,8 +76,7 @@ addEventListener('fetch', async event => { event.respondWith(check(event)); break; case SEED: - //TODO: disable after execute (exec once) - // event.respondWith(seedDb(event)) + event.respondWith(seedDb(event)) break; case WEBHOOK: event.respondWith(handleWebhook(event)) @@ -92,12 +94,16 @@ addEventListener('fetch', async event => { }) async function seedDb(event) { + let seedClientApp = await app.getSeedClientApp(); + + if (seedClientApp) return new Response('Seed already executed.') + let clientApps = await ClientApp.seedDb(wkv); + await app.updateSeedClientApp({input: true}) await Logger.log(`seedDb clientApps: ${JSON.stringify(clientApps)}`) return new Response(JSON.stringify(clientApps)) - } /** @@ -199,14 +205,17 @@ async function onMessage(message, options = {}) { let [cmdId, input] = message.text.split(';'); let handler = {db: wkv, input: input || message.text, message, usrSession, isAdmin}; - await Logger.log(`DEBUG MODE -message.text: ${message.text}`, {}) - await Logger.log(`DEBUG MODE - user Session: ${JSON.stringify(usrSession)}`, {}) + // await Logger.log(`DEBUG MODE -message.text: ${message.text}`, {}) + // await Logger.log(`DEBUG MODE - user Session: ${JSON.stringify(usrSession)}`, {}) switch (cmdId) { case cmdId.match(/\/silentButton/)?.input: return await Promise.resolve(); // case cmdId.match(/\//)?.input : + case "aboutBot" : + return await aboutBot(message, {method: 'editMessageText', messageId: message.message_id}); + case "/editedStart" : let opt = {method: 'editMessageText', messageId: message.message_id}; return await sendStartMessage(message, isAdmin, opt); @@ -252,7 +261,7 @@ async function onMessage(message, options = {}) { case cmdId.match(/plan\/(.?)*\/update/)?.input: // case cmdId.match(/plan\/(.?)*\/doUpdate/)?.input: case cmdId.match(/plan\/.*\/delete/)?.input: - return await Plan.adminRoute(cmdId, wkv, message, TlgBot); + return await Plan.adminRoute(cmdId, wkv, message, TlgBot, {Logger}); case cmdId.match(/server\/(.?)*\/details/)?.input: case cmdId.match(/server\/(.?)*\/update/)?.input: @@ -262,13 +271,13 @@ async function onMessage(message, options = {}) { case cmdId.match(/payment\/(.?)*\/details/)?.input: case cmdId.match(/payment\/(.?)*\/update/)?.input: case cmdId.match(/payment\/.*\/delete/)?.input: - return await Payment.adminRoute(cmdId, wkv, message, TlgBot); + return await Payment.adminRoute(cmdId, wkv, message, TlgBot, {Logger}); case cmdId.match(/clientApp\/(.?)*\/details/)?.input: case cmdId.match(/clientApp\/(.?)*\/update/)?.input: case cmdId.match(/clientApp\/.*\/delete/)?.input: // await Logger.log(`ClientApp.adminRoute}: ${JSON.stringify(cmdId)}`, []); - return await ClientApp.adminRoute(cmdId, handler, TlgBot); + return await ClientApp.adminRoute(cmdId, handler, TlgBot, {Logger}); case cmdId.match(/order\/(.?)*\/details/)?.input: case cmdId.match(/order\/(.?)*\/continuation/)?.input: @@ -297,27 +306,29 @@ async function onMessage(message, options = {}) { if (cmd.preFunc) { let {model, func} = cmd.preFuncData(); - // await Logger.log(`cmd: {model, func}: ${JSON.stringify({model, func})}`, []); + await Logger.log(`cmd: {model, func}: ${JSON.stringify({model, func})}`, []); let result = await DataModel[model]?.[func](handler, {Logger}); + await Logger.log(`result: ${JSON.stringify(result)}`) vars = Object.assign({}, vars, typeof result === 'object' ? result : {}) - // await Logger.log(`vars: ${JSON.stringify(vars)}`) + await Logger.log(`vars: ${JSON.stringify(vars)}`) } let buttons = await buildButtons(cmd, isAdmin, {Logger, nextCmd: `${cmd.nextId}`}); - // await Logger.log(`buttons: ${JSON.stringify(buttons)}`, []); + await Logger.log(`buttons: ${JSON.stringify(buttons)}`, []); let opt = {Logger} opt = cmd.resultInNew ? opt : Object.assign({}, opt, { method: 'editMessageText', messageId: message.message_id }) - await Logger.log(`opt: ${JSON.stringify(opt)}`, {}); let text1 = (typeof vars === 'object' ? vars : {}).transform(`${cmd.body}\n${cmd.helpText}`); let response = await TlgBot.sendInlineButtonRow(chatId, text1, buttons, opt); // if (cmd.savedInSession) { - await wkv.update(chatId, {currentCmd: cmd.nextId}) + let updated = await wkv.update(chatId, {currentCmd: cmd.nextId}) + // await Logger.log(`wkv.update: ${JSON.stringify(updated)}`, {}); + // } return response @@ -329,7 +340,7 @@ async function onMessage(message, options = {}) { if (currentCmd) { if (currentCmd.preFunc) { let {model, func} = currentCmd.preFuncData(); - // await Logger.log(`currentCmd: {model, func}: ${JSON.stringify({model, func})}`, []); + await Logger.log(`currentCmd: {model, func}: ${JSON.stringify({model, func})}`, []); handler.input = uInput || handler.input; let preFunc = await DataModel[model]?.[func](handler, { @@ -346,12 +357,14 @@ async function onMessage(message, options = {}) { text = (typeof vars === 'object' ? vars : {}).transform(text); let opt = {Logger} - await Logger.log(`currentCmd opt: ${JSON.stringify({vars, text, opt})}`, {}); + // await Logger.log(`currentCmd opt: ${JSON.stringify({vars, text, opt})}`, {}); let sentMessageRes = await TlgBot.sendInlineButtonRow(chatId, text, buttons, opt); // if (currentCmd.savedInSession) { - await wkv.update(chatId, {currentCmd: currentCmd.nextId}) + let updated = await wkv.update(chatId, {currentCmd: currentCmd.nextId}) + // await Logger.log(`wkv.update: ${JSON.stringify(updated)}`, {}); + // } return sentMessageRes @@ -380,16 +393,27 @@ function pushAdminButtons(buttons = [], isAdmin = false) { return buttons; } +async function aboutBot(message, options = {}) { + let chatId = message.chat_id || message.chat.id; + let text = Config.bot.aboutBot() + let buttons = []; + buttons.push(Command.backButton("/editedStart")); + + return await TlgBot.sendInlineButtonRow(chatId, text, buttons, options) +} + async function sendStartMessage(message, isAdmin, options = {}) { let chatId = message.chat_id || message.chat.id; let buttonRow = [ [{text: '📦 خرید اشتراک', callback_data: 'selectServer'}], [{text: '🛒 سوابق خرید', callback_data: 'order_history'}], [{text: '🔗 مشاهده نرم‌افزار', callback_data: Command.list.selectClientApp.id}], + [{text: '🌐 درباره ربات', callback_data: "aboutBot"}], ]; buttonRow = pushAdminButtons(buttonRow, isAdmin) - return await TlgBot.sendInlineButtonRow(chatId, Config.bot.welcomeMessage(), buttonRow, options) + let welcomeText = await app.getCustomWelcome() + return await TlgBot.sendInlineButtonRow(chatId, welcomeText, buttonRow, options) } async function editButtons(message, buttons = []) { diff --git a/src/models/admin.js b/src/models/admin.js index 1e37750..4105757 100644 --- a/src/models/admin.js +++ b/src/models/admin.js @@ -10,6 +10,7 @@ module.exports = { [{text: Command.list.managePlan.textIcon(), callback_data: Command.list.managePlan.id}], [{text: Command.list.manageServer.textIcon(), callback_data: Command.list.manageServer.id}], [{text: Command.list.managePayment.textIcon(), callback_data: Command.list.managePayment.id}], + [{text: Command.list.changeWelcome.textIcon(), callback_data: Command.list.changeWelcome.id}], ] }, diff --git a/src/models/app.js b/src/models/app.js index 5bd99b9..68b5dc5 100644 --- a/src/models/app.js +++ b/src/models/app.js @@ -1,20 +1,47 @@ 'use strict'; const Config = require('../config'); -const Command = require('./command'); class app { _db; dbKey = "app" + keys = { + seedCaliApp: {id: "seedCaliApp", default: false}, + } constructor(db) { this._db = db; } + async getAll() { + let data = await this._db.get(this.dbKey, {type: "json"}) || {}; + data[this.customWelcome] = data[this.customWelcome] || Config.bot.welcomeMessage(); + + return data; + } + + async getSeedClientApp() { + let app = await this._db.get(this.dbKey, {type: "json"}) || {}; + let data = app[this.keys.seedCaliApp.id] || this.keys.seedCaliApp.default; + + return data; + } + + async updateSeedClientApp({input}) { + await this._db.update(this.dbKey, {[this.keys.seedCaliApp.id]: input}) + } + customWelcome = 'customWelcome'; - async saveCustomWelcome(text) { - await this._db.update(this.dbKey, {[this.customWelcome]: text}) + async getCustomWelcome() { + let app = await this._db.get(this.dbKey, {type: "json"}) || {}; + let text = app[this.customWelcome] || Config.bot.welcomeMessage(); + + return text; + } + + async saveCustomWelcome({db, input, message, usrSession, isAdmin}) { + await this._db.update(this.dbKey, {[this.customWelcome]: input}) } } diff --git a/src/models/base-model.js b/src/models/base-model.js deleted file mode 100644 index 69a656c..0000000 --- a/src/models/base-model.js +++ /dev/null @@ -1,9 +0,0 @@ -"use strict"; - -export class BaseModel { - constructor() { - if (this.constructor == BaseModel) { - throw new Error("Abstract classes can't be instantiated."); - } - } -} diff --git a/src/models/client-app.js b/src/models/client-app.js index 47822c3..0dfaa26 100644 --- a/src/models/client-app.js +++ b/src/models/client-app.js @@ -160,9 +160,9 @@ const clientApp = { let result = input.split('\n').reduce((pv, cv, i) => { let split = cv.split(':'); - if (split.length < 1) return pv; + if (split.length < 2) return pv; - pv[split[0].trim()] = split[1].trimLeft().trimRight(); + pv[split[0].trim()] = split.slice(1).join(':').trim(); return pv; }, {}) @@ -228,7 +228,7 @@ const clientApp = { return newData; }, - async adminRoute(cmdId, handler, tlgBot) { + async adminRoute(cmdId, handler, tlgBot, {Logger}) { let {db, message, usrSession, isAdmin} = handler; let chatId = message.chat_id || message.chat.id; let [model, id, action] = cmdId.split('/'); @@ -266,7 +266,9 @@ ${this.toInput(dbModel)} `; var res = await tlgBot.sendInlineButtonRow(chatId, text, actions, opt); - await db.update(chatId, {currentCmd: doUpdate}) + let updated = await db.update(chatId, {currentCmd: doUpdate}) + await Logger.log(`wkv.update: ${JSON.stringify(updated)}`, {}); + return res diff --git a/src/models/command.js b/src/models/command.js index 866b84c..62358aa 100644 --- a/src/models/command.js +++ b/src/models/command.js @@ -165,6 +165,60 @@ const Cmd = { "buttons": "Order" }, + "changeWelcome": { + "prevId": "/editedStart", + "id": "changeWelcome", + "title": "تغییر متن خوش آمدگویی", + "icon": `🌹`, + textIcon() { + return `${this.icon} ${this.title}` + }, + "asButton": true, + "body": `متن فعلی: + +{customWelcome} + + +برای تغییر متن دلخواه خود را ارسال کنید: +`, + "successText": ``, + "helpText": ``, + "preFunc": 'app;getAll', + preFuncData() { + let [model, func] = this.preFunc.split(';'); + + return {model, func} + }, + "nextId": "submitNewWelcome", + "savedInSession": false, + "buttons": [] + }, + "submitNewWelcome": { + "prevId": "changeWelcome", + "id": "submitNewWelcome", + // "title": "ساخت پلن جدید", + // "icon": `📦 ➕`, + // textIcon() { + // return `${this.icon} ${this.title}` + // }, + "asButton": false, + "body": `✅متن شما با موفقیت ثبت شد.`, + "successText": ``, + "helpText": ``, + "preFunc": "app;saveCustomWelcome", + preFuncData() { + let [model, func] = this.preFunc.split(';'); + + return {model, func} + }, + "nextId": "", + "savedInSession": false, + "resultInNew": true, + "buttons": ["manage"] + }, + + + "manage": { "prevId": "/editedStart", "id": "manage", @@ -181,7 +235,7 @@ const Cmd = { "helpText": ``, "preFunc": '', "nextId": "", - "buttons": ["managePlan", "manageServer", "managePayment", "manageClientApp"] + "buttons": ["managePlan", "manageServer", "managePayment", "manageClientApp", "changeWelcome"] }, "managePlan": { "prevId": "manage", diff --git a/src/models/order.js b/src/models/order.js index c89e7b8..f5e052f 100644 --- a/src/models/order.js +++ b/src/models/order.js @@ -72,6 +72,8 @@ const order = { 📦 نام پلن: ${sPlan?.name} +📦 مدت زمان: ${sPlan?.maxDays} روز + 💎 قیمت :${Number(sPlan?.totalPrice).toLocaleString()} ${unitPrice} 🔰 ${sPayment?.appSecret} بنام ${sPayment?.appKey} diff --git a/src/models/payment.js b/src/models/payment.js index 40feefe..22b5b09 100644 --- a/src/models/payment.js +++ b/src/models/payment.js @@ -80,9 +80,9 @@ module.exports = { let result = input.split('\n').reduce((pv, cv, i) => { let split = cv.split(':'); - if (split.length < 1) return pv; + if (split.length < 2) return pv; - pv[split[0].trim()] = split[1].trimLeft().trimRight(); + pv[split[0].trim()] = split.slice(1).join(':').trim(); return pv; }, {}) @@ -146,7 +146,7 @@ module.exports = { return newData; }, - async adminRoute(cmdId, db, message, tlgBot) { + async adminRoute(cmdId, db, message, tlgBot, {Logger}) { let chatId = message.chat_id || message.chat.id; let [model, id, action] = cmdId.split('/'); let payment = await this.findByIdDb(db, id); @@ -185,7 +185,9 @@ ${this.toInput(payment)} `; var res = await tlgBot.sendInlineButtonRow(chatId, text, actions, opt); - await db.update(chatId, {currentCmd: doUpdate}) + let updated = await db.update(chatId, {currentCmd: doUpdate}) + await Logger.log(`wkv.update: ${JSON.stringify(updated)}`, {}); + return res diff --git a/src/models/plan.js b/src/models/plan.js index 0565350..b44b773 100644 --- a/src/models/plan.js +++ b/src/models/plan.js @@ -66,7 +66,7 @@ module.exports = { }, - async adminRoute(cmdId, db, message, tlgBot) { + async adminRoute(cmdId, db, message, tlgBot, {Logger}) { let chatId = message.chat_id || message.chat.id; let isAdmin = chatId === Config.bot.adminId; let [model, id, action] = cmdId.split('/'); @@ -74,17 +74,10 @@ module.exports = { let confirmDeleteId = Command.list.confirmDelete.id; let managePlanId = Command.list.managePlan.id; - - // await tlgBot.sendInlineButtonRow(chatId, `adminRoute plan: ${JSON.stringify(plan)}`); - - if (!plan) { return await tlgBot.sendInlineButtonRow(chatId, `${this.modelName} مربوطه پیدا نشد! 🫤`); } - - // await tlgBot.sendInlineButtonRow(chatId, `adminRoute actions: ${JSON.stringify(actions)} && action: ${action} `); - let text, actions; let opt = {method: 'editMessageText', messageId: message.message_id} @@ -113,7 +106,9 @@ ${this.toInput(plan)} `; var res = await tlgBot.sendInlineButtonRow(chatId, text, actions, opt); - await db.update(chatId, {currentCmd: doUpdate}) + let updated = await db.update(chatId, {currentCmd: doUpdate}) + await Logger.log(`wkv.update: ${JSON.stringify(updated)}`, {}); + return res @@ -162,11 +157,14 @@ ${this.toInput(plan)} async findAll(db, cmd, options = {}) { let {addBackButton = true, nextCmd} = options; - let data = await db.get(this.dbKey, {type: "json"}) || [] - // let cbData = (p) => cmd.savedInSession ? `${nextCmd};${p.id}` : nextCmd || `${this.dbKey}/${p.id}/details`; let cbData = (p) => nextCmd ? p.transform(cmd.nextId) : `${this.dbKey}/${p.id}/details`; - let result = data.map(p => [Command.ToTlgButton(p.name, cbData(p))]); + let result = data.map(p => { + let text = `${p.name}`; + // let text = `پنل ${p.name} - ${p.maxDays} روزه - ${Number(p.totalPrice).toLocaleString()} تومان `; + + return [Command.ToTlgButton(text, cbData(p))]; + }); let canShowAdminButtons = !cmd.hasOwnProperty("appendAdminButtons") || cmd.appendAdminButtons === true; if (canShowAdminButtons && options.forAdmin == true) { @@ -198,9 +196,9 @@ ${this.toInput(plan)} let result = input.split('\n').reduce((pv, cv, i) => { let split = cv.split(':'); - if (split.length < 1) return pv; + if (split.length < 2) return pv; - pv[split[0].trim()] = split.slice(1).join(":").trimLeft().trimRight(); + pv[split[0].trim()] = split.slice(1).join(':').trim(); return pv; }, {}) diff --git a/src/models/server.js b/src/models/server.js index 226b21b..9729096 100644 --- a/src/models/server.js +++ b/src/models/server.js @@ -87,9 +87,9 @@ module.exports = { let result = input.split('\n').reduce((pv, cv, i) => { let split = cv.split(':'); - if (split.length < 1) return pv; + if (split.length < 2) return pv; - pv[split[0].trim()] = split.slice(1).join(':').trimLeft().trimRight(); + pv[split[0].trim()] = split.slice(1).join(':').trim(); return pv; }, {}) @@ -131,7 +131,7 @@ module.exports = { }, async create({db, input}, options = {}) { - // await options.Logger?.log(`before create: ${typeof input} && ${JSON.stringify(input)}`); + await options.Logger?.log(`before create: ${typeof input} && ${JSON.stringify(input)}`); let data = await this.parseInput(input, options); if (!data.title || !data.remark || !data.url) { @@ -139,8 +139,7 @@ module.exports = { } let oldData = await db.get(this.dbKey, {type: "json"}) || []; - - // await options.Logger?.log(`oldData: ${JSON.stringify(oldData)}`); + await options.Logger?.log(`oldData: ${JSON.stringify(oldData)}`); let newData = { "id": new Date().toUnixTIme(), @@ -155,7 +154,7 @@ module.exports = { return newData; }, - async adminRoute(cmdId, db, message, tlgBot) { + async adminRoute(cmdId, db, message, tlgBot, {Logger}) { let chatId = message.chat_id || message.chat.id; let [model, id, action] = cmdId.split('/'); let server = await this.findByIdDb(db, id); @@ -194,7 +193,9 @@ ${this.toInput(server)} `; var res = await tlgBot.sendInlineButtonRow(chatId, text, actions, opt); - await db.update(chatId, {currentCmd: doUpdate}) + let updated = await db.update(chatId, {currentCmd: doUpdate}) + await Logger.log(`wkv.update: ${JSON.stringify(updated)}`, {}); + return res diff --git a/src/modules/telegram.js b/src/modules/telegram.js index 6ff75b7..70a03ae 100644 --- a/src/modules/telegram.js +++ b/src/modules/telegram.js @@ -114,7 +114,8 @@ module.exports = class Telegram { } async log(text, options = {}) { - text = `log: ${text}`; + text = `${text}`; + // return Promise.resolve({}); //TODO: change adminId, have no idea return this.sendToAdmin(text, [], {}) diff --git a/src/modules/wkv.js b/src/modules/wkv.js index 08841b6..dce35ae 100644 --- a/src/modules/wkv.js +++ b/src/modules/wkv.js @@ -26,9 +26,12 @@ module.exports = class wKV { // let val = typeof value === 'object' ? JSON.stringify(value) : value; let metadata = options.metadata; - return metadata ? this.ns.put(key, JSON.stringify(value), { + let result = metadata ? await this.ns.put(key, JSON.stringify(value), { metadata: metadata, - }) : this.ns.put(key, JSON.stringify(value)) + }) : await this.ns.put(key, JSON.stringify(value)); + + + return value; }; async delete(key) {