Skip to content

Commit

Permalink
add stock price indicator and confirmation
Browse files Browse the repository at this point in the history
  • Loading branch information
imans777 committed Apr 20, 2020
1 parent ad53a4c commit fbbd744
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions commands/plan.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const numConverter = require('../utility/number_converter');
const donation = {
user_id: "msg.from.id",
plan_id: "getFromDB",
plan_price: 0,
stock: 0,
};

Expand All @@ -16,7 +17,10 @@ module.exports = (bot) => {
donations = []

bot.on('/dbcon', msg => {
console.log("db object is: ", db());
bot.sendMessage(msg.from.id, "خب اینم از <strong>متن پرقدرت</strong> که اینجا داریم", {
// bot.sendMessage(msg.from.id, "this is a <strong>strong</strong> text", {
parseMode: "HTML"
});
});

bot.on("/plancheck", msg => {
Expand Down Expand Up @@ -72,6 +76,7 @@ module.exports = (bot) => {
donations.push({
user_id: msg.from.id,
plan_id: res["_id"],
plan_price: res["price"],
stock: 0,
});
return bot.sendMessage(msg.from.id, decodeURI(messages.advanced.send_stock), {
Expand Down Expand Up @@ -103,11 +108,14 @@ module.exports = (bot) => {
}
don['stock'] = parseInt(numConverter.toEnglishNumber(msg.text));

bot.sendMessage(msg.from.id, decodeURI(messages.advanced.send_mobile_number), {
bot.sendMessage(msg.from.id, getStockConfirmationStatement(decodeURI(messages.advanced.send_mobile_number), don), {
parseMode: 'HTML',
replyMarkup: bot.keyboard([
[bot.button('contact', 'ارسال شماره همراه')],
replies.plan_return_back[0],
], {resize: true}),
}).catch(err => {
console.error("error asking for phone number: ", err);
});
});

Expand Down Expand Up @@ -164,6 +172,28 @@ module.exports = (bot) => {
return false;
}

function getStockConfirmationStatement(msg, plan) {
return `
تعداد ${numConverter.toPersianNumber(plan['stock'])} سهم به ارزش <strong>${getPersianLetteredNumber(plan['stock'] * plan['plan_price'] * 1000)}</strong> وارد شده است 🌹
در صورت صحیح بودن، ${msg}
`;
}

function getPersianLetteredNumber(num) {
const per = numConverter.toPersianNumber(num);
const per_th = numConverter.toPersianNumber(parseInt(num / 1000));
const per_mil = numConverter.toPersianNumber(parseInt(num / 1000000));

if (num < 1000) {
return `${per} تومان`;
} else if (num < 1000000) {
return `${per_th} هزار تومان`;
} else {
return `${per_mil} میلیون تومان`;
}
}

function getStatement(plan_name, donated_stocks, remaining_stocks) {
let st = '';
// st += encodeURI("📔 ") + encodeURI(plan_name); // TODO: should be "button_name" and "showing_name"
Expand Down

0 comments on commit fbbd744

Please sign in to comment.