-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
395 lines (364 loc) · 15 KB
/
main.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
const { Telegraf, session, Scenes } = require("telegraf");
require("dotenv").config();
// import helper functions
const { getStartKb, getBalanceString, getCEXOrderTypeKb, staticKbs, staticStrings } = require("./utils");
const marketOrder = require("./trade/marketOrder.js");
// Creates a new Telegraf instance. Telegraf is a wrapper for the Telegram APIs. Another popular library is node-telegram-bot-api
const bot = new Telegraf(process.env.BOT_TOKEN);
/************************************** WIZARD, SCENE, STAGE **************************************/
// a Wizard Scene allows you to construct a step-by-step path when users clicks a menu button. For example, if user clicks Button A,
// they will see Button Y and Button Z. If they click Button B, they will see Button W and Button X.
// Here, we create a Wizard Scene called "WizCEXMarket". This Wizard launches when user clicks "Binance Spot" in the /start menu
const CEXmarketWizardScene = new Scenes.WizardScene(
"CEXmarket",
async (ctx) => {
console.log("entering CEXmarket Wizard Scene");
// extract variable
const orderType = ctx.callbackQuery.data.split("_")[0];
const side = ctx.callbackQuery.data.split("_")[1];
// write to local session storage
ctx.session.trade.orderType = orderType;
ctx.session.trade.side = side;
await ctx.reply(
`My order (incomplete)\n${ctx.session.trade.exchange} ${ctx.session.trade.market} ${ctx.session.trade.orderType} ${ctx.session.trade.side}\n\nEnter amount of token to ${ctx.session.trade.side} (example: 1000 DOGE)`,
{
reply_markup: {
inline_keyboard: [staticKbs.partial.backExit],
},
}
);
return ctx.wizard.next();
},
async (ctx) => {
if (ctx.callbackQuery?.data == "back") {
await ctx.scene.leave();
delete ctx.session.trade.orderType;
delete ctx.session.trade.side;
// order type keyboard
const CEXOrderTypeKb = await getCEXOrderTypeKb();
ctx.telegram.sendMessage(ctx.chat.id, `My order (incomplete)\n${ctx.session.trade.exchange} ${ctx.session.trade.market}\n\nNow, select an order type:`, {
reply_markup: {
inline_keyboard: CEXOrderTypeKb,
},
});
return;
} else if (ctx.callbackQuery?.data == "exit") {
ctx.session.trade = {};
await ctx.scene.leave();
// start message
const keyboard = await getStartKb();
ctx.telegram.sendMessage(ctx.chat.id, staticStrings.start, {
parse_mode: "HTML",
reply_markup: {
inline_keyboard: keyboard,
},
});
return;
}
////// User now enters amount of token to buy/sell
const text = ctx.message.text;
//// performing checks first
// check errors in text message
const isTwoWords = text.split(" ").length == 2; // check if message is exactly 2 words
const isFirstWordNumber = Number.isNaN(Number(text.split("_")[0])); // check if first word is a number
// const isBaseTokenAvailable = await checkBaseToken(ctx.session.trade.exchange); // check if tokesn are available on exchange
// throw error if checks do not pass
if (!isTwoWords || !isFirstWordNumber) {
ctx.reply("Please enter valid token amount and token name");
return;
}
// if (!isBaseTokenAvailable) {
// ctx.reply(`${text.split("_")[0]} is not an available token on ${ctx.session.trade.exchange.toUpperCase()} EXCHANGE`);
// return;
// }
// extract variables
const baseTokenAmount = text.split(" ")[0];
const baseToken = text.split(" ")[1];
// write to local session storage
ctx.session.trade.baseTokenAmount = baseTokenAmount;
ctx.session.trade.baseToken = baseToken;
// reply
await ctx.reply(
`My order (incomplete)\n${ctx.session.trade.exchange} ${ctx.session.trade.market} ${ctx.session.trade.orderType} ${ctx.session.trade.side} ${ctx.session.trade.baseTokenAmount} ${ctx.session.trade.baseToken}\n\nNow, enter your quote token or currency (e.g., USDT, USDC, USD, EUR, etc.)`,
{
reply_markup: {
inline_keyboard: [staticKbs.partial.backExit],
},
}
);
return ctx.wizard.next();
},
async (ctx) => {
// handle back and exit buttons
if (ctx.callbackQuery?.data == "back") {
// clear previous data
delete ctx.session.trade.baseToken;
delete ctx.session.trade.baseTokenAmount;
ctx.wizard.back();
// reply with previous message
await ctx.reply(
`My order (incomplete)\n${ctx.session.trade.exchange} ${ctx.session.trade.market} ${ctx.session.trade.orderType} ${ctx.session.trade.side}\n\nEnter amount of token to ${ctx.session.trade.side} (example: 1000 DOGE)`,
{
reply_markup: {
inline_keyboard: [staticKbs.partial.backExit],
},
}
);
return;
} else if (ctx.callbackQuery?.data == "exit") {
// clear previous data and leave wizard
ctx.session.trade = {};
await ctx.scene.leave();
// reply with start message
const keyboard = await getStartKb();
ctx.reply(staticStrings.start, {
parse_mode: "HTML",
reply_markup: {
inline_keyboard: keyboard,
},
});
return;
}
////// user now enters quote token
const text = ctx.message.text;
// checks
const isOneWord = text.split(" ").length == 1;
// const isQuoteTokenAvailable = await checkQuoteToken(ctx.session.trade.exchange);
if (!isOneWord) {
ctx.reply("Please enter a valid quote token or currency name");
return;
}
// if (!isQuoteTokenAvailable) {
// ctx.reply(`${text} is not an available quote token or currency on ${ctx.session.trade.exchange.toUpperCase()} EXCHANGE`);
// return;
// }
// validation=success and proceed
ctx.session.trade.quoteToken = text;
await ctx.reply(
`My Order (complete)\n${ctx.session.trade.exchange} ${ctx.session.trade.market} ${ctx.session.trade.orderType} ${ctx.session.trade.side} ${ctx.session.trade.baseTokenAmount} ${ctx.session.trade.baseToken} using ${ctx.session.trade.quoteToken}\n\nDo you want to submit the above order?`,
{
reply_markup: {
inline_keyboard: [staticKbs.partial.backExit, [{ text: "Submit Order", callback_data: "submit" }]],
},
}
);
return ctx.wizard.next();
},
async (ctx) => {
if (ctx.callbackQuery?.data == "back") {
// clear previous data
delete ctx.session.trade.quoteToken;
ctx.wizard.back();
// reply with previous message
await ctx.reply(
`My Order (incomplete)\n${ctx.session.trade.exchange} ${ctx.session.trade.market} ${ctx.session.trade.orderType} ${ctx.session.trade.side} ${ctx.session.trade.baseTokenAmount} ${ctx.session.trade.baseToken}\n\nNow, enter your quote token or currency (e.g., USDT, USDC, USD, EUR, etc.)`,
{
reply_markup: {
inline_keyboard: [staticKbs.partial.backExit],
},
}
);
return;
} else if (ctx.callbackQuery?.data == "exit") {
// clear previous data and leave wizard
ctx.session.trade = {};
await ctx.scene.leave();
// reply with start message
const keyboard = await getStartKb();
ctx.reply(staticStrings.start, {
parse_mode: "HTML",
reply_markup: {
inline_keyboard: keyboard,
},
});
return;
} else if (ctx.callbackQuery?.data == "submit") {
//submit order here
await marketOrder(ctx);
ctx.reply("Order executed");
ctx.session.trade = {};
await ctx.scene.leave();
// reply with start message after 3s
await new Promise((resolve) => setTimeout(resolve, 3000));
const keyboard = await getStartKb();
ctx.reply(staticStrings.start, {
parse_mode: "HTML",
reply_markup: {
inline_keyboard: keyboard,
},
});
}
}
);
const stage = new Scenes.Stage([CEXmarketWizardScene]); // append all "scenes" to "stage"
/******************** WIZARD, SCENE, STAGE (END) **************************************/
/****************************** BOT SETUP *********************************************/
console.log("listening on port:", process.env.PORT);
bot.use(session()); // use session middleware, so we can store local data in Telegram bot
bot.use(stage.middleware()); // integrate "stage" (thus our Wizard Scenes) as a middleware
// if not in development mode, then activate Telegram to start listening to webhooks
if (process.env.PORT != "8080") {
console.log("start listening to webhooks");
bot.telegram.setWebhook(process.env.URL); // set webhook URL to Heroku App URL
bot.startWebhook("/", null, process.env.PORT || 5000); // start listening for webhooks, can't listen to same port as nodejs app
}
/***************************** BOT SETUP (END) ****************************************/
/******************************* BOT COMMANDS *****************************************/
bot.command("start", async (ctx) => {
// if session.settings does not exist, then define it
if (!ctx.session.settings) {
ctx.session = { trade: {}, settings: { binance: {}, coinbase: {}, okx: {}, bybit: {} }, wallet: { address: {}, privateKey: {} } };
}
const keyboard = await getStartKb();
ctx.telegram.sendMessage(ctx.chat.id, staticStrings.start, {
parse_mode: "HTML",
reply_markup: {
inline_keyboard: keyboard,
},
});
});
bot.command("balance", async (ctx) => {
let balanceString = await getBalanceString();
// let keyboard = await getBalanceKeyboard();
ctx.telegram.sendMessage(ctx.chat.id, balanceString, {
parse_mode: "HTML",
});
});
bot.command("settings", async (ctx) => {
if (!ctx.session.settings) {
ctx.session.settings = {};
}
ctx.reply("Select an option", { reply_markup: { inline_keyboard: staticKbs.full.settings } });
});
// define the help command (this way of defining a command is different than the above)
bot.help((ctx) => {
ctx.reply(staticStrings.help, { parse_mode: "HTML" });
});
// commands are "/command" messages to trigger an action. When you press "Menu" button, all commands will show.
const commands = [
{
command: "start",
description: "main menu",
},
{
command: "balance",
description: "your balance",
},
{
command: "settings",
description: "view settings and keys",
},
{
command: "help",
description: "documentation",
},
];
bot.telegram.setMyCommands(commands);
/************************************** BOT COMMANDS (END) ************************************/
/*************************************** KEYBOARD BUTTONS ***********************************/
// Here is the logic whenever ANY keyboard button is pressed
// ctx.callbackQuery.data = query = the text in the "callback_data" associated any keyboard button
bot.on("callback_query", async (ctx) => {
const query = ctx.callbackQuery.data;
if (["Binance_spot", "Binance_futures", "Coinbase_spot", "Coinbase_futures", "OKX_spot", "OKX_futures", "ByBit_spot", "ByBit_futures"].includes(query)) {
// extract variables
const exchange = query.split("_")[0];
const market = query.split("_")[1];
// store variables into session storage
ctx.session.trade = { exchange: exchange };
ctx.session.trade.market = market;
// reply with message and keyboard
ctx.telegram.sendMessage(ctx.chat.id, `My order (incomplete)\n${ctx.session.trade.exchange} ${ctx.session.trade.market}\n\nNow, select an order type:`, {
reply_markup: {
inline_keyboard: staticKbs.full.CEXOrderType,
},
});
}
if (["matcha", "jupiter"].includes(query)) {
// store variables into session storage
ctx.session.trade = { exchange: query };
// reply with message and keyboard
const DEXOrderTypeKb = await getDEXOrderTypeKb();
ctx.telegram.sendMessage(ctx.chat.id, `My order (incomplete)\n${ctx.session.trade.exchange}`, {
reply_markup: {
inline_keyboard: DEXOrderTypeKb,
},
});
}
// enter the "CEXmarket" Wizard Scene
if (["market_buy", "market_sell", "limit_buy", "limit_sell"].includes(query)) {
const orderType = query.split("_")[0];
ctx.scene.enter(`CEX${orderType}`);
}
if (query == "settings") {
if (!ctx.session.settings) {
ctx.session.settings = {};
}
ctx.reply("Select an option", { reply_markup: { inline_keyboard: staticKbs.full.settings } });
}
if (query == "cexInfo") {
ctx.reply("Select an option", { reply_markup: { inline_keyboard: staticKbs.full.cexInfo } });
}
if (["binanceInfo", "coinbaseInfo", "OKXInfo", "bybitInfo"].includes(query)) {
const exchangeLower = query.split("Info")[0];
const exchange = exchangeLower.toUpperCase();
if (!ctx.session.settings[exchangeLower]) {
ctx.session.settings[exchangeLower] = {};
}
ctx.reply(
`Set your API Key, Secret Key, and Password by sending the following messages:\n${exchange}_API=xxxxx\n${exchange}_SECRET=xxxxx\n${exchange}_PASSWORD=xxxxx\n*xxxxx should be replaced by the appropriate value`
);
}
if (["walletInfo"].includes(query)) {
ctx.reply(
"set your wallet address and private key by sending the following messages:\nWALLET_ADDRESS=xxxxx\nWALLET_PRIVATE_KEY=xxxxx\nxxxxx should be replaced by the appropriate value"
);
}
if (query === "balance") {
let balanceString = await getBalanceString();
// let keyboard = await getBalanceKeyboard();
ctx.telegram.sendMessage(ctx.chat.id, balanceString, {
parse_mode: "HTML",
});
}
if (query == "exit") {
// copy start
ctx.session.trade = {};
const startKb = await getStartKb();
ctx.reply(staticStrings.start, {
parse_mode: "HTML",
reply_markup: {
inline_keyboard: startKb,
},
});
}
if (query == "help") {
ctx.reply(staticStrings.help, { parse_mode: "HTML" });
}
});
/****************************************** KEYBOARD BUTTONS (END) ************************************/
// // storing secrets in local storage
// bot.on("message", async (ctx) => {
// const text = ctx.message.text;
// if (text.split("=")[0] === "BINANCE_API") {
// ctx.session.settings.binance.api = text.split("=")[1];
// ctx.reply("Binance API Key set");
// }
// if (text.split("=")[0] === "BINANCE_SECRET") {
// ctx.session.settings.binance.secret = text.split("=")[1];
// ctx.reply("Binance Secret Key set");
// }
// if (text.split("=")[0] === "BINANCE_PASSWORD") {
// ctx.session.settings.binance.password = text.split("=")[1];
// ctx.reply("Binance password set");
// }
// });
/******************************************** FOR LOCAL DEVELOPMENT **********************************************/
// Can't use webhook in local development. If using bot.launch(), Telegram listens to incoming messsages through "polling".
// When starting the bot in local (node main.js), we can use bot.launch().
// Heroku will set port to number other than 8080. So, if PORT=8080, then use bot.launch().
if (process.env.PORT == "8080") {
console.log("activate bot.launch()");
bot.launch();
}
/******************************************************************************************************************/