-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
144 lines (136 loc) · 4.21 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// index.js
//
require("dotenv").config();
const { Telegraf, session, Markup, Scenes } = require("telegraf");
const { customScenes, customCommands } = require("./bot");
const { customPath } = require("./services");
const { model } = require("./model");
const { updateUsersDb, readUsersDb, checkUsersDb } = model;
const fs = require("fs");
// Global Constants
const BOT_TOKEN = process.env.BOT_TOKEN;
const DATA = JSON.parse(fs.readFileSync(customPath("data/data.json")));
//Functions
// Wizard Scenes
const startWizard = customScenes.startWizard;
const checkWizard = customScenes.checkWizard;
const deleteWizard = customScenes.deleteWizard;
// Creating bot instance
const bot = new Telegraf(BOT_TOKEN);
const stage = new Scenes.Stage([startWizard, checkWizard, deleteWizard]);
bot.use(session());
bot.use(stage.middleware());
// bot actions and commands
bot.action("_START", (ctx, next) => {
ctx.scene.enter("START_WIZARD");
});
bot.action("_CHECK", (ctx, next) => {
ctx.scene.enter("CHECK_WIZARD");
});
bot.action("_DELETE", (ctx, next) => {
ctx.scene.enter("DELETE_WIZARD");
});
bot.command("/test", ctx => {
console.log("test command", ctx.state);
});
// /^(\/addsICX+\s)+\d{0,}[\.]?\d{0,}$/
bot.hears(/^(\/\w+\s+\d*\.?\d*)$/, ctx => {
// bot listen to any command followed by a number (i.e /addsICX 123);
let command = ctx.message.text.split(" ");
if (command[0].substring(1) === "addsICX") {
// command sent: addsICX
console.log("command: ", JSON.stringify(command));
ctx.reply(customCommands.addsICX(command, ctx.from.id));
} else if (command[0].substring(1) === "addUSD") {
// command sent: addsICX
console.log("command: ", JSON.stringify(command));
ctx.reply(customCommands.addUSD(command, ctx.from.id));
}
});
bot.command("start", ctx => {
ctx.reply(
customCommands.startCommandReplyText,
Markup.inlineKeyboard([
Markup.button.callback("Add Wallet", "_START"),
Markup.button.callback("Check Wallet", "_CHECK"),
Markup.button.callback("Delete Wallet", "_DELETE")
])
);
});
bot.command("/info", ctx => {
ctx.reply(customCommands.infoCommandReplyText);
});
bot.command("/assets", async ctx => {
ctx.session = checkUsersDb(ctx.session, ctx.from.id);
if (
ctx.session[ctx.from.id].hasInitialized &&
ctx.session[ctx.from.id].wallets.length > 0
) {
ctx.reply("Running check, please wait a few seconds...");
let reply = await customCommands.checkPricesCreateReply(
ctx.session[ctx.from.id].wallets
);
ctx.reply(reply);
} else {
ctx.reply(
"There are no wallets to check, use /start command to add wallets"
);
}
});
bot.command("/pnl", async ctx => {
ctx.session = checkUsersDb(ctx.session, ctx.from.id);
if (
ctx.session[ctx.from.id].hasInitialized &&
ctx.session[ctx.from.id].wallets.length > 0
) {
ctx.reply("Running check, please wait a few seconds...");
let reply = await customCommands.checkPNL(ctx.session[ctx.from.id].wallets);
ctx.reply(reply);
} else {
ctx.reply(
"There are no wallets to check, use /start command to add wallets"
);
}
});
bot.command("/summary", async ctx => {
ctx.session = checkUsersDb(ctx.session, ctx.from.id);
if (
ctx.session[ctx.from.id].hasInitialized &&
ctx.session[ctx.from.id].wallets.length > 0
) {
ctx.reply("Running check, please wait a few seconds...");
let replyBreak = "\n=====================\n";
let replies = await customCommands.checkSummary(
ctx.session[ctx.from.id].wallets,
ctx.from.id
);
let reply =
replyBreak +
"PNL check:\n" +
replies.pnl +
replyBreak +
"Assets check\n" +
replies.prices;
ctx.reply(reply);
} else {
ctx.reply(
"There are no wallets to check, use /start command to add wallets"
);
}
});
// running bot
bot.launch();
bot.catch(err => {
console.log("Bot Error:");
console.log(err);
console.log("Bot error, throwing unhandled exception");
throw "Bot error, throwing exception";
});
// Catching uncaught exceptions
process.on("uncaughtException", err => {
console.error("Uncaught error: ", err);
process.exit(1);
});
// Enable graceful stop
process.once("SIGINT", () => bot.stop("SIGINT"));
process.once("SIGTERM", () => bot.stop("SIGTERM"));