-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
124 lines (95 loc) · 3.36 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
const TelegramBot = require('node-telegram-bot-api');
const { intervalDuration } = require('./intervalDuration');
// telegram bot token
const token = '*************';
// Create a bot that uses 'polling' to fetch new updates
const bot = new TelegramBot(token, { polling: true });
// example of saving data
// {
// chatId: 0,
// rate: 10,
// intervals: [],
// }
const chats = [];
bot.setMyCommands([
{ command: '/start', description: 'To begin press /start' },
{ command: '/rate', description: 'To get current rate press /rate' },
{ command: '/finish', description: 'To finish press /finish' },
]);
// Listen for any kind of message. There are different kinds of messages.
bot.on('message', (msg) => {
const chatId = msg.chat.id;
// when user send /start
if (msg.text === '/start') {
// save in chats array chatId
chats.push({ chatId, rate: null, intervals: [] });
console.log(chats);
}
if (msg.text.startsWith('rate')) {
const rate = msg.text.split(' ')[1];
// convert rate to number
const rateNumber = +rate;
// check if rate is a number
if (isNaN(rateNumber)) {
bot.sendMessage(chatId, 'Please enter a number');
return;
}
// check if rate is a positive number
else if (rateNumber <= 0) {
bot.sendMessage(chatId, 'Please enter a positive number');
return;
}
// modify the rate of the user in the chats array
// loop through the chats array
// if the chatId of the current chat is the same as the chatId of the current user
// then modify the rate of the current chat
for (let i = 0; i < chats.length; i++) {
if (chats[i].chatId === chatId) {
chats[i].rate = rate;
}
}
console.log(chats);
bot.sendMessage(chatId, `Your rate is ${rate}`);
}
if (msg.text.startsWith('interval')) {
// get the interval from the message
const interval = msg.text.substring(9).trim();
// add the interval to the intervals array of the current user
for (let i = 0; i < chats.length; i++) {
if (chats[i].chatId === chatId) {
chats[i].intervals.push(interval);
}
}
console.log(chats);
}
if (msg.text === '/finish') {
// get the current chatId intervals
let intervals = [];
for (let i = 0; i < chats.length; i++) {
if (chats[i].chatId === chatId) {
intervals = chats[i].intervals;
}
}
// calculate the total time in minutes with loop through the intervals array
let totalTime = 0;
for (let i = 0; i < intervals.length; i++) {
totalTime += intervalDuration(intervals[i]);
}
// get the current chatId total amount of money
let rate = 0; // rate per hour
for (let i = 0; i < chats.length; i++) {
if (chats[i].chatId === chatId) {
rate = chats[i].rate;
}
}
// calculate the total amount of money and round it to 2 decimal places
const totalAmount = +(rate * totalTime / 60).toFixed(2);
// get hours and minutes from total time
const hours = Math.floor(totalTime / 60);
const minutes = totalTime % 60;
// send message to the user
bot.sendMessage(chatId, `You worked ${hours} hours and ${minutes} minutes and earned ${totalAmount} dollars`);
console.log(chats);
}
}
);