Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Base currency menu #72

Merged
merged 13 commits into from
Nov 24, 2017
97 changes: 97 additions & 0 deletions commands/keyboards/coins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
var Keyboard = require('./keyboard').Keyboard;
var rp = require('request-promise');
var _ = require('lodash');

var buttons = [];
var followed_coins = [];
var buttons_line = [];
var userSettings;
var coins;

var PAGE_COLS = 5;
var PAGE_ROWS = 5;
var PAGE_SIZE = PAGE_COLS * PAGE_ROWS;

var current_page = 0;

var getCurrentPage = (page_num = 0) => {
buttons = [];

current_page = parseInt(page_num);
var total_number_of_pages = coins.length / PAGE_SIZE;

// 0 < 4
var next_page = current_page < total_number_of_pages - 1 ? current_page + 1 : 0;
var prev_page = current_page > 0 ? current_page - 1 : total_number_of_pages - 1;

var arrows = [{ text: "<<", callback_data: `settings.NAV:COI_${prev_page}` }, { text: ">>", callback_data: `settings.NAV:COI_${next_page}` }];
var cancel = [{ text: "Cancel", callback_data: "settings.NAV:MAIN" }];

//get PAGE_SIZE elements for each page (25)
var page_buttons = buttons_line.slice(current_page * PAGE_SIZE, current_page * PAGE_SIZE + PAGE_SIZE);

for (i = 0; i <= total_number_of_pages; i++) {
var page_buttons_row = page_buttons.slice(i * PAGE_COLS, i * PAGE_COLS + PAGE_COLS);
buttons.push(page_buttons_row);
}
buttons.push(arrows);
//I redefined the getButtons method, therefore I have to add CANCEL manually
buttons.push(kb.cancelButton);
return buttons;
}

var updateFollowedButtons = () => {
buttons_line = [];
coins.forEach(coin => {
coin.followed = followed_coins.indexOf(coin.symbol) >= 0;
buttons_line.push({ text: `${coin.followed ? '• ':''}${coin.symbol}`, callback_data: `settings.DB:COI_${coin.symbol}_${coin.followed ? 'False' : 'True'}` });
});
}

var loadCoins = (page_num) => {
var node_services_endpoint = process.env.ITT_NODE_SERVICES;

if (userSettings) {
followed_coins = userSettings.coins ? userSettings.coins : [];
//! Testing
followed_coins.push('ETH', 'NEO', 'OMG');
}

if (coins) {
return new Promise((resolve, reject) => {
updateFollowedButtons();
resolve(getCurrentPage(page_num));
});
}
else {
return rp(`${node_services_endpoint}/tickers`)
.then((json_tickers) => {
var tickers = JSON.parse(json_tickers);
coins = [];

Object.keys(tickers).forEach((key) => {
var ticker = { symbol: tickers[key].info.symbol, followed: false};
coins.push(ticker);
});

updateFollowedButtons();
})
.then(() => {
return getCurrentPage();
})
.catch((reason) => {
console.log(reason)
});
}
}


var msg = "Please select the *coins* to follow/unfollow in order to receive related signals.";
var kb = new Keyboard(msg, buttons);

kb.updateSettings = (settings) => {
userSettings = settings;
};
kb.getButtons = (page) => loadCoins(page);

exports.kb = kb;
14 changes: 14 additions & 0 deletions commands/keyboards/exchange.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var Keyboard = require('./keyboard').Keyboard;

var buttons = [
[
{ text: "BTC", callback_data: "settings.DB:CUR_BTC" },
{ text: "USDT", callback_data: "settings.DB:CUR_USD" },
{ text: "ETH", callback_data: "settings.DB:CUR_ETH" }
]
];

var msg = "Please select the *exchange currencies* in order to receive related signals.";
var kb = new Keyboard(msg, buttons);
kb.showCancelButton = true;
exports.kb = kb;
24 changes: 24 additions & 0 deletions commands/keyboards/keyboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var _ = require('lodash');

function Keyboard(message, buttons, extraButtons = null, showCancelButton = true) {
this.message = message;
this.buttons = buttons;
this.extraButtons = extraButtons;
this.showCancelButton = showCancelButton;
this.cancelButton = [{ text: "Cancel", callback_data: "settings.NAV:MAIN" }];
}

Keyboard.prototype.getButtons = function (keyboard_page) {
return new Promise((resolve, reject) => {
var kb_buttons = _.clone(this.buttons);
if (this.showCancelButton)
kb_buttons.push(this.cancelButton);
resolve(kb_buttons);
});
}

Keyboard.prototype.updateSettings = (userSettings) => {
// do something
}

exports.Keyboard = Keyboard;
22 changes: 22 additions & 0 deletions commands/keyboards/keyboards.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var main_kb = require('./main.js').kb;
var risk_kb = require('./risk.js').kb;
var trader_kb = require('./trader.js').kb;
var exchange_kb = require('./exchange.js').kb;
var coins_kb = require('./coins.js').kb;

var keyboards = {
main_keyboard: main_kb,
risk_keyboard: risk_kb,
trader_keyboard: trader_kb,
base_currency_keyboard: exchange_kb,
coins_keyboard: coins_kb
}

keyboards.updateKeyboardsSettings = (settings) => {
Object.keys(keyboards).forEach(kb => {
if (kb != 'updateKeyboardsSettings')
keyboards[kb].updateSettings(settings);
});
}

exports.keyboards = keyboards;
58 changes: 58 additions & 0 deletions commands/keyboards/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
var _ = require('lodash');
var Keyboard = require('./keyboard').Keyboard;

var buttons = [
[{ text: "Subscribe", callback_data: "settings.DB:ISSUB_" }],
[{ text: "Turn alerts", callback_data: "settings.DB:ISMUTED_" }]
];

var extraButtons = [
[{ text: "Exchange currencies", callback_data: "settings.NAV:CUR" }],
[{ text: "Coins list", callback_data: "settings.NAV:COI" }],
[{ text: "Edit Risk Profile", callback_data: "settings.NAV:RSK" }],
[{ text: "Edit Trader Profile", callback_data: "settings.NAV:HRZ" }]
];

var kb = new Keyboard('', buttons, extraButtons, false);
kb.showExtraButtons = function (show) {
if (show) {
this.buttons.length == 2 ? this.buttons = _.concat(this.buttons, this.extraButtons) : {};
}
else {
this.buttons.splice(2);
};
}

kb.updateButtons = (message, isSubscribed, isMuted) => {

kb.message = message;

buttons[0][0].text = isSubscribed ? 'Unsubscribe' : 'Subscribe';
buttons[0][0].callback_data = isSubscribed
? buttons[0][0].callback_data.split('_')[0] + '_False'
: buttons[0][0].callback_data.split('_')[0] + '_True';

// Dynamic Alert button
buttons[1][0].text = isMuted ? 'Turn alerts ON' : 'Turn alerts OFF';
buttons[1][0].callback_data = isMuted
? buttons[1][0].callback_data.split('_')[0] + '_False'
: buttons[1][0].callback_data.split('_')[0] + '_True';
}

kb.updateSettings = (userSettings) => {
var isMuted = userSettings.is_muted;
var isSubscribed = userSettings.is_subscribed;

var msg = `Your profile is set on *${userSettings.horizon}* horizon, *${userSettings.risk}* risk.
You are ${isSubscribed ? '*subscribed*' : '*not subscribed*'} to signals and your notifications are ${isMuted ? '*muted*' : '*active*'}.
Tap below to edit your settings:`;

kb.updateButtons(msg, isSubscribed, isMuted);

//! let's keep some features private

kb.showExtraButtons(userSettings.is_ITT_team);
}


exports.kb = kb;
16 changes: 16 additions & 0 deletions commands/keyboards/risk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var Keyboard = require('./keyboard').Keyboard;

var risks = ['low', 'medium', 'high'];
var buttons = [];

var msg = "Please select which _risk profile_ suits you best.\nI will adjust your signals accordingly in conjunction with your trader profile.";
var kb = new Keyboard(msg, buttons);
kb.updateSettings = (settings) => {
kb.buttons = [];
kb.buttons.push([{ text: `${settings.risk == risks[0] ? '• ' : ''} Only the safest trade signals (Beginners)`, callback_data: `settings.DB:RSK_${risks[0]}` }]);
kb.buttons.push([{ text: `${settings.risk == risks[1] ? '• ' : ''}Any signal, reputable coins only (Standard)`, callback_data: `settings.DB:RSK_${risks[1]}` }]);
kb.buttons.push([{ text: `${settings.risk == risks[2] ? '• ' : ''}Any signal, including low value coins (High risk high reward)`, callback_data: `settings.DB:RSK_${risks[2]}` }]);

}

exports.kb = kb;
16 changes: 16 additions & 0 deletions commands/keyboards/trader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var Keyboard = require('./keyboard').Keyboard;

var horizons = ['long', 'medium', 'short'];
var buttons = [];

var msg = "Please select which _trader profile_ suits you best.\nI will adjust your signals accordingly in conjunction with your risk profile.";
var kb = new Keyboard(msg, buttons);
kb.updateSettings = (settings) => {
kb.buttons = [];
kb.buttons.push([{ text: `${settings.horizon == horizons[0] ? '• ' : ''}Investor: Long term trade signals. Exit and entry points for HODL. (Low risk)`, callback_data: `settings.DB:HRZ_${horizons[0]}` }]);
kb.buttons.push([{ text: `${settings.horizon == horizons[1] ? '• ' : ''}Swingtrader: Short/near term trade signals. Profit from volatility. (Medium risk)`, callback_data: `settings.DB:HRZ_${horizons[1]}` }]);
kb.buttons.push([{ text: `${settings.horizon == horizons[2] ? '• ' : ''}Daytrader: Very short term trade signals. Getting in and out trades. (High risk)`, callback_data: `settings.DB:HRZ_${horizons[2]}` }]);

}

exports.kb = kb;
Loading