Commit 2217f20b authored by Ahmad Nemati's avatar Ahmad Nemati

init

parents
const db = require('./db')
const message = require('./message')
const constant = require('./const')
const TelegramBot = require('node-telegram-bot-api');
const token = '783628734:AAGHs5YcIGy4dhD3ZsKIQOKxcJVDD0ju4jY';
const bot = new TelegramBot(token, {polling: true});
bot.on('message', (msg) => {
processMessage(msg)
});
async function processMessage(msg) {
await isAdmin(msg)
if (msg.document) {
commitDoc(msg)
return
}
if (msg.text)
msg.text = fa2en(msg.text)
if (msg.text.includes("start"))
message.startMessage(bot, msg)
else if (msg.text.includes("لیست اعضا"))
listuser(msg)
else
elseMessage(msg)
}
bot.on('callback_query', function onCallbackQuery(msg) {
processCallback(msg)
});
async function processCallback(msg) {
await isAdmin(msg)
if (msg.data.startsWith("user_")) showUser(msg)
else if (msg.data.startsWith("request_")) request(msg)
else if (msg.data.startsWith("buy_")) showCurrency(msg, 'buy')
else if (msg.data.startsWith("sell_")) showCurrency(msg, 'sell')
else if (msg.data.startsWith("toman_")) showCount(msg, 'toman')
else if (msg.data.startsWith("dollar_")) showCount(msg, 'dollar')
else if (msg.data.startsWith("euro_")) showCount(msg, 'euro')
else if (msg.data.startsWith("lir_")) showCount(msg, 'lir')
else if (msg.data.startsWith("aed_")) showCount(msg, 'aed')
else if (msg.data.startsWith("Rtoman_")) commitRequest(msg, 'toman')
else if (msg.data.startsWith("Rdollar_")) commitRequest(msg, 'dollar')
else if (msg.data.startsWith("Reuro_")) commitRequest(msg, 'euro')
else if (msg.data.startsWith("Rlir_")) commitRequest(msg, 'lir')
else if (msg.data.startsWith("Raed_")) commitRequest(msg, 'aed')
else if (msg.data.startsWith("yes_")) yesNoDoc(msg, 'yes')
else if (msg.data.startsWith("no_")) yesNoDoc(msg, 'no')
else if (msg.data.startsWith("reportD")) reportRequest(msg, 1)
else if (msg.data.startsWith("reportW")) reportRequest(msg, 7)
else if (msg.data.startsWith("reportM")) reportRequest(msg, 30)
else if (msg.data.startsWith("reqs_")) showRequest(msg)
}
async function listuser(msg) {
let users = await db.getAllUsers()
message.listUserMessage(bot, msg, users)
}
async function showUser(msg) {
msg.data = msg.data.replace("user_", "")
let user = await db.findUser(msg.data)
message.userShowMessage(bot, msg, user)
}
async function request(msg) {
msg.data = msg.data.replace("request_", "")
let user = await db.findUser(msg.data)
message.userSubmitRequest(bot, msg, user)
}
async function showCurrency(msg, type) {
switch (type) {
case 'buy':
msg.data = msg.data.replace("buy_", "")
break
case 'sell':
msg.data = msg.data.replace("sell_", "")
break
}
let user = await db.findUser(msg.data)
let data = {action: type, user: user.id}
await db.updateData(msg, JSON.stringify(data))
message.showCurrencyMessage(bot, msg, user)
}
async function showCount(msg, unit) {
switch (unit) {
case 'toman':
msg.data = msg.data.replace("toman_", "")
break
case 'dolar':
msg.data = msg.data.replace("dollar_", "")
break
case 'euro':
msg.data = msg.data.replace("euro_", "")
break
case 'lir':
msg.data = msg.data.replace("lir_", "")
break
case 'aed':
msg.data = msg.data.replace("aed_", "")
break
}
let admin = await db.findAdmin(msg)
admin.data = JSON.parse(admin.data)
admin.data.vahed = unit
await db.updateData(msg, JSON.stringify(admin.data))
message.showCountMessage(bot, msg)
}
async function elseMessage(msg) {
let admin = await db.findAdmin(msg)
if (admin.status === constant.input1)
input1(msg)
else if (admin.status === constant.input2)
input2(msg)
}
async function input1(msg) {
let isvalid = numberValidation(msg)
if (!isvalid) {
message.simpleMessage(bot, msg, 'لطفا مقدار درست را وارد نمایید')
return
}
let admin = await db.findAdmin(msg)
admin.data = JSON.parse(admin.data)
admin.data.count = msg.text
admin.data = JSON.stringify(admin.data)
await db.updateData(msg, admin.data)
message.showFeeMessage(bot, msg)
}
async function input2(msg) {
let isvalid = numberValidation(msg)
if (!isvalid) {
message.simpleMessage(bot, msg, 'لطفا مقدار درست را وارد نمایید')
return
}
let admin = await db.findAdmin(msg)
admin.data = JSON.parse(admin.data)
let data = admin.data
admin.data.fee = msg.text
await db.updateData(msg, JSON.stringify(admin.data))
message.forwardCurrencyMessage(bot, msg, data)
}
async function yesNoDoc(msg, type) {
console.log("Yes?????")
if (type.includes('yes')) {
message.simpleMessage(bot, msg, 'لطفا فایل ضمیمه را ارسال کنید دقت شود که به عنوان فایل ارسال شود.')
return
}
let admin = await db.findAdmin(msg)
admin.data = JSON.parse(admin.data)
await db.createRequest(admin.data)
await db.updateData(msg, null)
message.simpleMessage(bot, msg, 'فیش با موفقیت ثبت شد.')
}
async function commitRequest(msg, type) {
console.log("Here")
let admin = await db.findAdmin(msg)
admin.data = JSON.parse(admin.data)
admin.data.to = type
let userId = admin.data.user
// await db.createRequest(admin.data)
await db.updateData(msg, JSON.stringify(admin.data))
message.sendDocMessage(bot, msg, userId)
}
async function commitDoc(msg) {
let admin = await db.findAdmin(msg)
admin.data = JSON.parse(admin.data)
admin.data.file_id = msg.document.file_id
await db.createRequest(admin.data)
await db.updateData(msg, null)
message.simpleMessage(bot, msg, 'فیش با موفقیت ثبت شد.')
}
function numberValidation(msg) {
const st = fa2en(msg.text);
const number = st;
let phoneRGEX = RegExp('^[1-9]\\d*$');
let phoneResult = phoneRGEX.test(number);
return phoneResult !== false;
}
function fa2en(txt) {
return txt.replace(/۱/g, "1").replace(/۲/g, "2").replace(/۳/g, "3").replace(/۴/g, "4").replace(/۵/g, "5").replace(/۶/g, "6").replace(/۷/g, "7").replace(/۸/g, "8").replace(/۹/g, "9")
.replace(/۰/g, "0");
}
async function isAdmin(msg) {
let admin = await db.findAdmin(msg)
if (!admin) {
message.simpleMessage(bot, msg, 'شما دسترسی استفاده از ربات را ندارید.')
throw 'Not Permission'
}
return true
}
async function canOrder(data, msg) {
if (data.action === 'sell')
return true
let user = await db.findUser(data.user)
let num = user.fee * user.count
if (data.to === 'toman')
if (user.toman_balance <= 0 || num > user.toman_balance) {
generateText(user.toman_balance, num, msg)
}
if (data.to === 'dollar')
if (user.dollar_balance <= 0 || num > user.dollar_balance) {
generateText(user.dollar_balance, num, msg)
}
if (data.to === 'euro')
if (user.euro_balance <= 0 || num > user.euro_balance) {
generateText(user.euro_balance, num, msg)
}
if (data.to === 'lir')
if (user.lir_balance <= 0 || num > user.lir_balance) {
generateText(user.lir_balance, num, msg)
}
if (data.to === 'aed')
if (user.aed_balance <= 0 || num > user.aed_balance) {
generateText(user.aed_balance, num, msg)
}
}
async function showRequest(msg) {
msg.data = msg.data.replace("reqs_", "")
let req = await db.findRequest(msg.data)
message.showRequest(bot, msg, req)
}
async function reportRequest(msg, date) {
switch (date) {
case 1:
msg.data = msg.data.replace("reportD_", "")
break
case 7:
msg.data = msg.data.replace("reportW_", "")
break
case 30:
msg.data = msg.data.replace("reportM_", "")
break
}
let reqs = await db.getRequestByDate(msg.data, date)
message.listRequest(bot, msg, reqs)
}
async function generateText(current, num, msg) {
let text = 'امکان فروش وجود ندارد موجودی فعلی : '
text += current
text += ' و مقدار درخواستی '
text += num + ' '
text += ' میباشد درصورتی که میخواهید عملیات انجام شود لطفا دسترسی لازم را بدهید'
message.simpleMessage(bot, msg, text)
throw 'Error canot do transaction'
}
module.exports = Object.freeze({
start: 0,
input1: 100,
input2: 101,
input3: 102,
tomorrow: 2,
});
const Sequelize = require('sequelize');
const moment = require('moment')
const Op = Sequelize.Op;
const sequelize = new Sequelize('accounting', 'root', 'root', {
define: {
charset: 'utf8',
collate: 'utf8_general_ci'
},
host: 'localhost',
dialect: 'mysql',
logging: false,
operatorsAliases: false
});
const User = sequelize.define('user', {
first_name: Sequelize.STRING,
last_name: Sequelize.STRING,
pro: {type: Sequelize.BOOLEAN, allowNull: false, defaultValue: false},
toman_balance: {type: Sequelize.DOUBLE, allowNull: false, defaultValue: 0},
dollar_balance: {type: Sequelize.DOUBLE, allowNull: false, defaultValue: 0},
euro_balance: {type: Sequelize.DOUBLE, allowNull: false, defaultValue: 0},
lir_balance: {type: Sequelize.DOUBLE, allowNull: false, defaultValue: 0},
aed_balance: {type: Sequelize.DOUBLE, allowNull: false, defaultValue: 0},
});
const Admin = sequelize.define('admin', {
teleid: Sequelize.INTEGER,
status: Sequelize.INTEGER,
data: Sequelize.STRING
});
const Request = sequelize.define('request', {
user_request: Sequelize.INTEGER,
fee: Sequelize.DOUBLE,
count: Sequelize.DOUBLE,
unit: Sequelize.STRING,
toUnit: Sequelize.STRING,
is_today: Sequelize.BOOLEAN,
file_id: Sequelize.STRING,
type: Sequelize.STRING,
});
async function createRequest(data) {
let mRequest = await Request.create({
user_request: data.user,
fee: data.fee,
count: data.count,
unit: data.vahed,
file_id: data.file_id,
type:data.action,
toUnit: data.to,
is_today: true
})
return mRequest;
}
async function getAllUsers() {
return await User.findAll({
raw: true
})
}
async function getRequestByDate(userId, date) {
return await Request.findAll({
raw: true,
where: {
user_request: userId,
createdAt: {
[Op.gte]: moment().subtract(date, 'days').toDate()
}
}
})
}
async function updateData(msg, data) {
return await Admin.update(
{
data: data,
},
{where: {teleid: typeof msg.chat !== 'undefined' ? msg.chat.id : msg.message.chat.id}})
}
async function updateStatus(msg, status) {
return await Admin.update(
{
status: status,
},
{where: {teleid: typeof msg.chat !== 'undefined' ? msg.chat.id : msg.message.chat.id}})
}
async function findUser(id) {
return await User.findOne({raw: true, where: {id: id}})
}
async function findAdmin(msg) {
return await Admin.findOne({
raw: true,
where: {teleid: typeof msg.chat !== 'undefined' ? msg.chat.id : msg.message.chat.id}
})
}
async function findRequest(id) {
return await Request.findOne({
raw: true,
where: {id: id}
})
}
module.exports = {
getAllUsers: getAllUsers,
findUser: findUser,
updateData: updateData,
findAdmin: findAdmin,
updateStatus: updateStatus,
createRequest: createRequest,
getRequestByDate: getRequestByDate,
findRequest: findRequest
};
const db = require('./db')
const constant = require('./const')
function startMessage(bot, msg) {
simpleMessage(bot, msg, 'به ربات حسابداری خودش آمدید.')
}
async function simpleMessage(bot, msg, text) {
const opts = {
reply_markup: {
keyboard: [
['💷 لیست اعضا']
],
}
};
bot.sendMessage(typeof msg.chat !== 'undefined' ? msg.chat.id : msg.message.chat.id, text, opts)
}
async function listUserMessage(bot, msg, users) {
const opts = {
reply_markup: {
inline_keyboard: []
}
};
for (let i = 0; i < users.length; i++) {
let text = users[i].first_name + " " + users[i].last_name + ' ' + 'اعتبار:'
text += generateText('تومان', users[i].toman_balance)
if (users[i].dollar_balance !== 0)
text += generateText('دلار', users[i].dollar_balance)
if (users[i].euro_balance !== 0)
text += generateText('یورو', users[i].euro_balance)
if (users[i].lir_balance !== 0)
text += generateText('لیر', users[i].lir_balance)
if (users[i].aed_balance !== 0)
text += generateText('درهم', users[i].aed_balance)
opts.reply_markup.inline_keyboard.push([{
text: text,
callback_data: "user_" + users[i].id
}])
}
await db.updateStatus(msg, constant.start)
bot.sendMessage(msg.chat.id, 'لطفا کاربر مورد نظر را انتخاب کنید.', opts)
}
function generateText(type, balance) {
let text = ' '
text += balance < 0 ? (-1 * parseFloat(balance)) + "-" : balance
text += ' ' + type + ' '
return text
}
function userShowMessage(bot, msg, user) {
var opts = {
reply_markup: {
inline_keyboard: [
[
{
text: 'ثبت فیش',
callback_data: 'request_' + user.id
}
]
,
[
{
text: 'گزارش روزانه',
callback_data: 'reportD_' + user.id
}
]
,
[
{
text: 'گزارش هفتگی',
callback_data: 'reportW_' + user.id
}
],
[
{
text: 'گزارش ماهیانه',
callback_data: 'reportM_' + user.id
}
]
]
},
chat_id: msg.message.chat.id,
message_id: msg.message.message_id
};
bot.editMessageText('لطفا کاربر مورد نظر را انتخاب کنید.', opts)
}
async function showCurrencyMessage(bot, msg, user) {
var opts = {
reply_markup: {
inline_keyboard: [
[
{
text: 'تومان',
callback_data: 'toman_' + user.id
}
],
[
{
text: 'دلار',
callback_data: 'dollar_' + user.id
}
],
[
{
text: 'یورو',
callback_data: 'euro_' + user.id
}
],
[
{
text: 'لیر',
callback_data: 'lir_' + user.id
}
],
[
{
text: 'درهم',
callback_data: 'aed_' + user.id
}
]
]
},
chat_id: msg.message.chat.id,
message_id: msg.message.message_id
};
await db.updateStatus(msg, constant.start)
bot.editMessageText('لطفا نوع ارز را مشخص کنید.', opts)
}
async function userSubmitRequest(bot, msg, user) {
var opts = {
reply_markup: {
inline_keyboard: [
[
{
text: 'خرید',
callback_data: 'buy_' + user.id
}
]
,
[
{
text: 'فروش',
callback_data: 'sell_' + user.id
}
]
]
},
chat_id: msg.message.chat.id,
message_id: msg.message.message_id
};
await db.updateStatus(msg, constant.start)
bot.editMessageText('لطفا خرید یا فروش بودن معامله را مشخص کنید.', opts)
}
async function showCountMessage(bot, msg) {
var opts = {
chat_id: msg.message.chat.id,
message_id: msg.message.message_id
};
await db.updateStatus(msg, constant.input1)
bot.editMessageText('لطفا مقدار مورد نیاز را وارد نمایید.', opts)
}
async function showFeeMessage(bot, msg) {
await db.updateStatus(msg, constant.input2)
simpleMessage(bot, msg, 'لطفا مقدار قیمت ارز را وارد نمایید.')
}
async function forwardCurrencyMessage(bot, msg, data) {
let arr = [{
vahed: 'toman',
text: 'تومان',
callback_data: 'Rtoman_' + data.user
},
{
vahed: 'dollar',
text: 'دلار',
callback_data: 'Rdollar_' + data.user
},
{
vahed: 'euro',
text: 'یورو',
callback_data: 'Reuro_' + data.user
},
{
vahed: 'lir',
text: 'لیر',
callback_data: 'Rlir_' + data.user
},
{
vahed: 'aed',
text: 'درهم',
callback_data: 'Raed_' + data.user
}
]
var opts = {
reply_markup: {
inline_keyboard: []
}
};
for (let i = 0; i < arr.length; i++) {
if (arr[i].vahed.includes(data.vahed))
continue
opts.reply_markup.inline_keyboard.push([{
text: arr[i].text,
callback_data: arr[i].callback_data
}])
}
await await db.updateStatus(msg, constant.start)
bot.sendMessage(msg.chat.id, 'لطفا نوع ارز بیس مبادله را مشخص کنید.', opts)
}
async function sendDocMessage(bot, msg, user) {
var opts = {
reply_markup: {
inline_keyboard: [
[
{
text: 'بله',
callback_data: 'yes_' + user
}
],
[
{
text: 'خیر',
callback_data: 'no_' + user.id
}
],
]
}
};
await await db.updateStatus(msg, constant.input3)
bot.sendMessage(typeof msg.chat !== 'undefined' ? msg.chat.id : msg.message.chat.id, 'آیا فیش همراه با فایل ضمیمه است؟', opts)
}
async function listRequest(bot, msg, reqs) {
const opts = {
reply_markup: {
inline_keyboard: []
}
};
for (let i = 0; i < reqs.length; i++) {
let text = "فیش شماره "
text += reqs[i].id
opts.reply_markup.inline_keyboard.push([{
text: text,
callback_data: "reqs_" + reqs[i].id
}])
}
await db.updateStatus(msg, constant.start)
bot.sendMessage(typeof msg.chat !== 'undefined' ? msg.chat.id : msg.message.chat.id, 'لطفا فیش مورد نظر را انتخاب کنید.', opts)
}
async function showRequest(bot, msg, req) {
let text = "فیش شماره "
text += req.id
text += '\n'
text += 'تعداد '
text += req.type.replace("sell", 'فروش').replace("buy", "خرید")
text += ' '
text += req.count
text += ' '
text += req.unit.replace('toman', 'تومان').replace('dollar', 'دلار').replace('euro', 'یورو').replace('lir', 'لیر').replace('aed', 'درهم')
text += ' '
text += 'با مبلغ '
text += ' '
text += req.fee
text += ' '
text += req.toUnit.replace('toman', 'تومان').replace('dollar', 'دلار').replace('euro', 'یورو').replace('lir', 'لیر').replace('aed', 'درهم')
text += ' '
await await db.updateStatus(msg, constant.input3)
bot.sendMessage(typeof msg.chat !== 'undefined' ? msg.chat.id : msg.message.chat.id, text)
}
module.exports = {
startMessage: startMessage,
userShowMessage: userShowMessage,
listUserMessage: listUserMessage,
userSubmitRequest: userSubmitRequest,
showCurrencyMessage: showCurrencyMessage,
showCountMessage: showCountMessage,
simpleMessage: simpleMessage,
showFeeMessage: showFeeMessage,
showCurrencyMessage: showCurrencyMessage,
forwardCurrencyMessage: forwardCurrencyMessage,
sendDocMessage: sendDocMessage,
listRequest: listRequest,
showRequest: showRequest
};
This diff is collapsed.
{
"name": "accounting",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"moment": "^2.24.0",
"mysql2": "^1.6.1",
"node-telegram-bot-api": "^0.30.0",
"sequelize": "^4.38.1"
}
}
const db=require('./db')
test()
async function test() {
let d=await db.getRequestByDate(1,1)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment