-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
108 lines (83 loc) · 2.59 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
import { Telegraf } from 'telegraf'
import { exchangeKeyboard, mainKeyboard } from './keyboard-main.js'
const bot = new Telegraf(process.env.TELEGRAM_BOT_TOKEN)
const rate = 26.9514
bot.start( ctx => {
db[ctx.chat.id] = new Set()
db[ctx.chat.id].add('USD')
chats.add(ctx.chat.id)
ctx.replyWithMarkdown(
`*Національний Банк України.*\nПублічна інформація у формі відкритих даних.`, {
reply_markup: {
keyboard: mainKeyboard,
resize_keyboard: true,
one_time_keyboard: true,
}
}
)
}
)
bot.hears('Офіційний курс гривні до іноземних валют та банківських металів', async (ctx) => {
ctx.replyWithMarkdown('*Офіційний курс гривні до іноземних валют та банківських металів*',
{
reply_markup: {
keyboard: exchangeKeyboard,
resize_keyboard: true,
one_time_keyboard: true,
}
})
})
bot.hears('Назад', async (ctx) => {
ctx.replyWithMarkdown('*Назад*', {
reply_markup: {
keyboard: mainKeyboard,
resize_keyboard: true,
one_time_keyboard: true,
}
})
})
bot.hears('Отримати курс гривні', async (ctx) => {
sendlist(ctx.chat.id)
})
bot.launch()
import * as https from 'https'
let db = {}
let chats = new Set()
import cron from 'node-cron'
cron.schedule('0 16 * * *', () => {
chats.forEach(x => sendlist(x))
});
function sendlist(chatId) {
https.get('https://bank.gov.ua/NBU_Exchange/exchange?json', (res) => {
const selected = db[chatId]
let body = "";
res.on("data", (chunk) => {
body += chunk;
});
res.on("end", () => {
try {
const result = JSON.parse(body)
let message = "Ось актуальний курс на *" + result[0].StartDate + "*:\n\n"
result.forEach(row => {
if (selected.size > 0) {
if (!selected.has(row.CurrencyCodeL)) {
return
}
}
const element = row.CurrencyCodeL + " - " + row.Amount + ", coef - " + (row.Amount/rate).toFixed(4) + "\n"
message += element
});
bot.telegram.sendMessage(chatId, message, {
reply_markup: {
keyboard: mainKeyboard,
resize_keyboard: true,
one_time_keyboard: true,
},
parse_mode: "Markdown"
});
} catch (error) {
console.error(error.message);
};
});
})
}