-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
260 lines (214 loc) · 8.67 KB
/
bot.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
const Bot = require('telegram-bot-api');
const { token, provider } = require('./conf-env.js')
const { send, getBalance, nodeInfo, getConfimationStatus } = require('./helix.js')
const { getRows, updateSheet, getStats } = require('./gsheets.js')
const { promiseTimeout, TimeoutException } = require('./utils.js')
const bot = new Bot({
token: token
});
function UserException(userText, error) {
this.userText = userText;
this.error = error;
this.name = 'UserException';
}
const FAUCET_REGEX = /.*\/givememoney[\n\r\s]+([A-Fa-f0-9]+).*/g
const TX_INFO_REGEX = /.*\/gettxinfo[\n\r\s]+([A-Fa-f0-9]+).*/g
const NODE_INFO_REGEX = /.*\/getnodeinfo[\n\r\s]+([\/\.:A-Za-z0-9]+).*/g
const BALANCE_REGEX = /.*\/checkbalance[\n\r\s]+([A-Fa-f0-9]+).*/g
async function handleWithTimeout(context, ts) {
let waitMsg = await botMessage(context, `Processing`);
console.log(`Wait msg: ${JSON.stringify(waitMsg)}`);
try {
context.replyTo = context.message_id;
context.editMessage = waitMsg.message_id;
await promiseTimeout(5000, handleBotMessage(context))
} catch (err) {
if (err instanceof TimeoutException) {
return await botMessage(context, `Sorry, I had to abort the request as\ ` +
`as it was taking too long to complete. Please, try again.`);
}
if (err instanceof UserException) {
console.error(err.error);
return await botMessage(context, err.userText);
}
console.log(err);
return await botMessage(context, `Oooops, an unexpected error occured`);
}
}
async function handleBotMessage(message) {
console.log(`Handling message ${JSON.stringify(message)}`);
let context = message;
let command = message.text.toString().toLowerCase();
context.command = command;
/**
*
* TODO: refactor with a command map
*/
if (command === '/start') {
return await startDialog(context)
} else if (command.indexOf('/checkbalance') !== -1) {
return await checkBalance(context)
} else if (command.indexOf('/givememoney') !== -1) {
return await giveMeMoney(context)
} else if (command.indexOf('/gettxinfo') !== -1) {
return await getTxInfo(context)
} else if (command.indexOf('/getnodeinfo') !== -1) {
return await getNodeInfo(context)
} else if (command.indexOf('/pleasehelpme') !== -1) {
return await getHelpInfo(context)
}
return await botMessage(context, "Sorry, I can't recognize this command");
}
async function botMessage(context, text) {
let chatId = context.chat.id;
if (context.editMessage) {
return bot.editMessageText({
chat_id: context.chat.id,
message_id: context.editMessage,
parse_mode: "Markdown",
text: text
});
}
let msg = {
chat_id: chatId,
parse_mode: "Markdown",
text: text.replace("\n", "")
}
if (context.replyTo || context.message_id) {
msg.reply_to_message_id = (context.replyTo) ?
context.replyTo : context.message_id;
}
//if (context.replyMarkup) {
// msg.reply_markup: {
// resize_keyboard: true,
// one_time_keyboard: true,
// keyboard: context.replyMarkup.map(s => [{ text: s }])
// }
//}
return bot.sendMessage(msg);
}
async function getHelpInfo(context) {
return botMessage(context, `Usage: ${usageString()}\n` +
`If you believe something doesn't work or you are simply up up\ ` +
`for a chat, please join us [here](https://discord.gg/MN6e6Je)\ ` +
`and we will do our best. Yours truly, Helix \u{1F916}.`);
}
async function startDialog(context) {
//context.replyMarkup = ["Check Balance", "Give me HLX!",
// "Check transaction", "Node info", "Help"];
//return botMessage(context, "*HLXtestBot* started" + "\u{1F916}\n");
return botMessage(context, "*HLXtestBot* started" + "\u{1F916}\n" + usageString());
}
async function checkBalance(context) {
let command = context.command;
console.log(`checkBalance: ${command}`);
let parse = BALANCE_REGEX.exec(command);
if (parse === null) {
throw new UserException(`Hmm, the string _${command}_ you provided\ ` +
`does not look like an address. \nUsage: /checkBalance <address>`, 'PARSE_ERROR');
}
let addr = parse[1];
try {
let balance = await getBalance(addr);
return botMessage(context, `Current balance of _${addr}_ is *${balance.balances[0]}* HLX`);
} catch (err) {
throw new UserException(`Oooops, something nasty happened while\ ` +
`checking the balance. Please, try again later and contact us on\ ` +
`our [discord channel](https://discord.gg/MN6e6Je)`, err);
}
}
async function giveMeMoney(context) {
let command = context.command;
console.log(`giveMeMoney: ${command}`);
let parse = FAUCET_REGEX.exec(command);
if (parse === null) {
throw new UserException(`Hmm, the string _${command}_ you provided\ ` +
`does not look like an address. \nUsage: /giveMeMoney <address>`, 'PARSE_ERROR');
}
let rcv = parse[1];
let stats = await getStats();
if (stats.available < 0.2 * stats.total) {
throw new UserException(`Sorry, the faucet is running out of funds.\ `+
`Please notify us on [discord channel](https://discord.gg/MN6e6Je)\ ` +
`and we'll top it up`);
}
let indices = Array.from({length: 10},
() => Math.floor(Math.random() * stats.total));
try {
let rows = await getRows(indices);
console.log(rows);
// take the first available, there will be one with overwhelming probability
let { seed, address, updateCell } = rows.filter(r => r.available)[0];
let sendRes = await send(seed, address, rcv, 10000);
console.log(sendRes);
let tx = sendRes.txs[0];
// update the sheet with the address of the looter
await updateSheet(updateCell, `User: ${context.chat.username}, addr: ${rcv}`);
return botMessage(context, `All done, your HLX is on it's way. You can check the\ ` +
`transaction status by the hash: *${tx}*`)
} catch (err) {
throw new UserException(`Oooops, something nasty happened while transferring\ ` +
`HLX to your address. Please, try again later and contact us on\ ` +
`our [discord channel](https://discord.gg/MN6e6Je)`, err);
}
}
async function getTxInfo(context) {
let command = context.command;
console.log("getTxInfo: " + command)
let parse = TX_INFO_REGEX.exec(command)
console.log(parse)
if (parse === null) {
throw new UserException(`Hmm, the string _${command}_ you provided\ ` +
`does not look like a transaction hash. \nUsage: /getTxInfo <tx hash>`, 'PARSE_ERROR');
}
let tx = parse[1]
console.log(`Transaction hash: ${tx}`);
try {
let status = await getConfimationStatus(tx);
let text = status ? `*${tx}* is confirmed! ` : `*${tx}* is not confirmed`;
return botMessage(context, text);
} catch (err) {
throw new UserException(`Ooops, something went wrong try again later and\ `+
`ping us on our [discord channel](https://discord.gg/MN6e6Je)`, err);
}
}
async function getNodeInfo(context) {
//let chatId = context.chat.id;
//console.log(context);
let command = context.command;
let parse = NODE_INFO_REGEX.exec(command)
console.log(parse)
if (parse === null) {
throw new UserException(`Hmm, the string _${command}_ you provided\ ` +
`does not look like a valid URL. \nUsage: /getNodeInfo <url>`, 'PARSE_ERROR');
}
let nodeUrl = parse[1];
try {
console.log(`Requesting node info: ${nodeUrl}`);
let info = await nodeInfo(nodeUrl);
return botMessage(context, toCodeSnippet(JSON.stringify(info, null, 2)));
} catch (err) {
throw new UserException(`Oooops, failed to connect to ${nodeUrl}, check if\ `+
`the URL is correct (should look like https://my.node.com:8065) and\ `+
`ping us on our [discord channel](https://discord.gg/MN6e6Je)`, err);
}
}
/**
*
* Helper Functions
*/
function usageString() {
// TODO: compile from the command map
return `
/getnodeinfo <url>: request info about your favorite Helix node
/gettxinfo <hash>: request confirmation status of a transcation
/checkbalance <address>: check the current balance of an address
/givememoney <address>: request 10000 HLX to the specified address
/pleasehelpme: in case you think something is broken or you feel lonely`
}
function toCodeSnippet(str, cmd) {
return "``` " + str + "\n ```"
}
module.exports = {
handleBotMessage, handleWithTimeout
}