-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.mjs
222 lines (182 loc) · 7.44 KB
/
app.mjs
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
import { Markup, Telegraf, session } from 'telegraf';
import axios, { Axios } from 'axios';
import { getCoinURL } from './URL/getCoinURL.mjs';
import { getStartMenu, getStopTimer, selectTimercoin, selectAlarmcoin, getStopAlarm } from './funcshions/function.mjs';
import { writeFile, readFileSync, writeFileSync } from 'node:fs';
import dotenv from 'dotenv'
dotenv.config();
const bot = new Telegraf(process.env.TOKEN);
const TIMERS = ['15min', '30min', '1h']
const ALARM = ['up', 'down'];
bot.use(session())
bot.use((ctx, next) => {
if (ctx.session == undefined) {
ctx.session = { data: [] };
}
ctx.replayStartMenu = () => {
ctx.reply('to see the rate of your cryptocurrency, just write!', getStartMenu())
}
ctx.startTimer = async (coin, interval) => {
if (coin === undefined) {
ctx.reply('something is wrong !');
}
if (coin !== undefined) {
ctx.session.data.timerId = setInterval(async () => {
let receiveCoin = await getCoinURL(coin);
ctx.reply(`${coin}| USD ${receiveCoin} $`, getStopTimer());
}, interval)
} else {
ctx.reply(`first write "/timer"⏱ and write your "coin" !\n\n example below: 👇`, selectTimercoin())
}
}
return next()
})
bot.telegram.setMyCommands([
{ command: 'alarm', description: 'first write "/alarm"⏰ and write your "coin"🪙 and "Numbers" !' },
{ command: 'timer', description: 'first write "/timer"⏱ and write your "coin" !' },
{ command: 'help', description: 'описание команды ' }
])
bot.start(async ctx => {
const chat_id = ctx.message.chat.id;
const foundUser = ctx.session.data.find(i => i.id == chat_id);
if (foundUser == undefined) {
ctx.session.data.push({
id: chat_id,
});
}
await ctx.replyWithHTML('Hello my friend ! 😏\n\n' +
'to see 👁 the rate of your cryptocurrency 🪙, just write!', getStartMenu());
return ctx.reply('💻')
});
bot.on('edited_message', async ctx => {
await ctx.reply('You have successfully modified the message 👍');
return ctx.replyWithSticker()
});
//***************************************************************************************************
bot.hears('⏱ start crypto timer', async ctx => {
await Promise.all([
ctx.reply('first write "/timer"⏱ and write your "coin" !', selectTimercoin()),
ctx.replyWithSticker("CAACAgIAAxkBAAEcc3Zj051QFBnH2JYGW5Z2uTE3csBHXAACJgMAApzW5wpVzm400GJTXi0E"),
])
return ctx.reply('example below: 👇')
});
bot.command('/timer', async ctx => {
if (ctx.message.text === '/timer') {
ctx.reply('something is wrong ! try once more\n\n "first write "/timer"⏱ and write your "coin" !"')
} else {
let array = ctx.message.text.split(' ');
ctx.session.data.timecoin = array[1]
ctx.session.data.timecoin = ctx.session.data.timecoin.toUpperCase()
console.log(ctx.session.data.timecoin)
if (await getCoinURL(ctx.session.data.timecoin) === undefined) {
ctx.reply('something is wrong ! try once more\n\n "first write "/timer"⏱ and write your "coin" ! "')
} else {
ctx.reply(`select a timer⏱ ${ctx.session.data.timecoin}USD $ 🪙`, {
reply_markup: {
inline_keyboard: [[
{ text: "15min⏳", callback_data: '15min' },
{ text: "30min⏳", callback_data: '30min' },
{ text: "1h⏳", callback_data: '1h' }
]]
}
})
}
}
});
bot.action(TIMERS, async ctx => {
const intervalString = ctx.callbackQuery.data;
let intervalNumber = Number(intervalString.match(/\d+/));
if (intervalNumber !== undefined) {
ctx.replyWithSticker("CAACAgIAAxkBAAEcc5Zj06Dgl73TJhnSKLGX-HGVt1ZSbgAC4QADVp29ClvBlItA-NOgLQQ")
// ctx.reply('🚀 start timer ⏱')
}
if (intervalString.endsWith('min')) {
intervalNumber = intervalNumber * 60 * 1000;
} else if (intervalString.endsWith('h')) {
intervalNumber = intervalNumber * 60 * 60 * 1000;
}
ctx.startTimer(ctx.session.data.timecoin, intervalNumber)
await ctx.answerCbQuery();
});
bot.hears('TIMER STOP', ctx => {
clearInterval(ctx.session.data.timerId)
ctx.reply('timer stopped! \n\n to see the rate of your cryptocurrency, just write!', getStartMenu());
});
//***************************************************************************************************
bot.hears('⏰ start crypto alarm', async ctx => {
await Promise.all([
ctx.reply('first write "/alarm"⏰ and write your "coin"🪙 and "Numbers" !', selectAlarmcoin()),
ctx.replyWithSticker("CAACAgIAAxkBAAEcc3Zj051QFBnH2JYGW5Z2uTE3csBHXAACJgMAApzW5wpVzm400GJTXi0E"),
])
return ctx.reply('example below: 👇')
});
bot.command('/alarm', async ctx => {
const message = ctx.message.text.replace('\/alarm', '');
const regex = message.match(/([a-zA-Z]+) (\d+)/);
if (regex) {
ctx.session.data.alarmcoin = regex[1].toUpperCase()
ctx.session.data.alarminterval = regex[2]
if (await getCoinURL(ctx.session.data.alarmcoin) !== undefined) {
ctx.reply(`launch ${ctx.session.data.alarminterval}$ ${ctx.session.data.alarmcoin} \n\n select: \n\n in up ↑ ( ${ctx.session.data.alarminterval}$) \n\n in down ↓( ${ctx.session.data.alarminterval}$)`, {
reply_markup: {
inline_keyboard: [[
{ text: 'in up ↑', callback_data: 'up' },
{ text: 'in down ↓', callback_data: 'down' },
]]
}
})
}
} else {
return ctx.reply('oops ! \n\n first write "/alarm"⏰ and write your "coin"🪙 and "Numbers" !', selectAlarmcoin());
}
});
bot.action(ALARM, async ctx => {
try {
let downUp = ctx.callbackQuery.data;
let interval = ctx.session.data.alarminterval
if (downUp !== undefined) {
ctx.replyWithSticker("CAACAgIAAxkBAAEcc5Zj06Dgl73TJhnSKLGX-HGVt1ZSbgAC4QADVp29ClvBlItA-NOgLQQ")
// ctx.reply('start alarm')
}
ctx.session.data.alarmId = setInterval(async () => {
let receiveCoin = await getCoinURL(ctx.session.data.alarmcoin)
let choice;
console.log('start alarm')
if (downUp === 'up') {
choice = receiveCoin >= interval
} else if (downUp === 'down') {
choice = receiveCoin <= interval
}
if (choice) {
ctx.reply(` USD ${receiveCoin} $`, getStopAlarm());
}
}, 60000 * 5)
await ctx.answerCbQuery();
}
catch { (error) => console.log(error) };
});
bot.hears('ALARM STOP', ctx => {
clearInterval(ctx.session.data.alarmId)
ctx.reply('alarm stopped! \n\n to see the rate of your cryptocurrency, just write!', getStartMenu());
});
bot.on('text', async ctx => {
let text = ctx.message.text
text = text.toUpperCase()
let coin = await getCoinURL(text);
if (coin === undefined) {
ctx.reply(`I do not understand you ${text} \n\n to see the rate of your cryptocurrency, just write!`, getStartMenu())
}
if (coin !== undefined) {
return ctx.reply(`${text}| USD ${coin} $`)
}
});
bot.on('sticker', async ctx => {
return ctx.replyWithSticker("CAACAgEAAxkBAAEcc1pj05vXCMZsS-g1bI3tVOZBtckkbAACCAADmAABGU_EmbBI082LWy0E")
})
bot.command('/help', async ctx => {
ctx.reply('to see the rate of your cryptocurrency, just write!', getStartMenu())
})
bot.launch()
.then((res) => console.log('bot start !'))
.catch((error) => console.log(error));
console.log('bot start !')