Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Commit

Permalink
Merge branch 'release/1.1.9.0'
Browse files Browse the repository at this point in the history
# Conflicts:
#	CHANGELOG.md
#	src/config.js
#	src/index.js
#	src/models/app.js
  • Loading branch information
zill committed Oct 29, 2023
2 parents b707d82 + f3b948a commit d7ff91f
Show file tree
Hide file tree
Showing 16 changed files with 209 additions and 117 deletions.
60 changes: 14 additions & 46 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
19 changes: 17 additions & 2 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"use strict";

const pkg = require('../package.json');

const config = {
commands: {
silentButton: "/silentButton",
Expand All @@ -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 خوش آمدید! 🎉 از اینکه اینجا هستید، بسیار خوشحالیم.
ما اینجا هستیم تا به شما کمک کنیم و هر سوال یا درخواستی که دارید را رفع کنیم.
Expand Down
68 changes: 46 additions & 22 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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) {
Expand Down Expand Up @@ -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))
Expand All @@ -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))

}

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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, {
Expand All @@ -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
Expand Down Expand Up @@ -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 = []) {
Expand Down
1 change: 1 addition & 0 deletions src/models/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}],
]
},

Expand Down
33 changes: 30 additions & 3 deletions src/models/app.js
Original file line number Diff line number Diff line change
@@ -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})
}
}

Expand Down
9 changes: 0 additions & 9 deletions src/models/base-model.js

This file was deleted.

Loading

0 comments on commit d7ff91f

Please sign in to comment.