-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
107 lines (96 loc) · 2.88 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
const TelegramBot = require("node-telegram-bot-api");
const sdk = require("api")("@tron/v4.4.2#39lxa3hl0qj5m48");
const cron = require("node-cron");
const HandyStorage = require("handy-storage");
require("dotenv").config();
const storage = new HandyStorage({
beautify: true,
});
// Telegram's token
const token = process.env.TOKEN;
const address = process.env.ADDRESS;
const file_path = "./my_data.json";
storage.connect(file_path);
// Created instance of TelegramBot
const bot = new TelegramBot(token, {
polling: true,
});
// Listener (handler) for telegram's /start event
// This event happened when you start the conversation with both by the very first time
bot.onText(/\/start/, (msg) => {
const chatId = msg.chat.id;
bot.sendMessage(
chatId,
`Welcome to <b>Crypto Tron Tracker</b>, thank you for using my service, we will send you daily updates `,
{
parse_mode: "HTML",
}
);
storage.setState({
Ids: [...new Set([...storage.state.Ids, chatId])],
});
});
//running task daily
cron.schedule("0 0 0 * * *", () => {
console.log("running a task every day");
sendNotifcationForBalance(address);
sendNotifcationForTransctionDetails(address);
//
});
/**
* send notification for balance
* @param {string} address The address to check
* @return {void}
*/
function sendNotifcationForBalance(address) {
sdk["get-account-info-by-address"]({ address: address })
.then((res) => {
const { balance } = res.data[0];
const message = `<b> Balance</b>: <b>${balance}</b>`;
getStoredUsers(message);
})
.catch((err) => console.error(err));
}
/**
* send notification for transaction details
* @param {string} address The address to check
* @return {void}
*/
function sendNotifcationForTransctionDetails(address) {
sdk["get-transaction-info-by-account-address"]({ address: address })
.then((res) => {
const { data } = res;
let message = "";
data.forEach((el) => {
message += `<pre><b> txID</b>: <b>${el.txID}</b>
<b> NetFee</b>: <b>${el.net_fee}</b>
<b> To</b>: <b>${el.raw_data.contract[0].parameter.value.to_address}</b>
<b> From</b>: <b>${el.raw_data.contract[0].parameter.value.owner_address}</b>
<b> Amount</b>: <b>${el.raw_data.contract[0].parameter.value.amount}</b> </pre> \n
`;
});
getStoredUsers(message);
})
.catch((err) => console.error(err));
}
/**
* Get stored chat ids from users and send bot message
* @param {string} message The message to send
* @return {void}
*/
function getStoredUsers(message) {
try {
const users = storage.state.Ids;
if (users.length > 0) {
for (let i = 0; i < users.length; i++) {
bot.sendMessage(users[i], message, {
parse_mode: "HTML",
});
}
} else {
console.log("no user registered");
}
} catch (e) {
throw new Error("oops " + e);
}
}