-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
70 lines (61 loc) · 1.8 KB
/
Copy pathserver.js
File metadata and controls
70 lines (61 loc) · 1.8 KB
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
//Consts
const CONSTS = require('./utils/consts.js');
const db = require('./utils/localstorage/db.js');
const request = require('./utils/network/request.js');
//Telegraf
const Telegraf = require('telegraf');
const bot = new Telegraf(CONSTS.BOT_API);
//Express
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
//START
bot.start(async (ctx) => {
console.log('started:', ctx.from.id);
return ctx.replyWithMarkdown(CONSTS.WELCOME_T);
})
bot.on('new_chat_members', async (ctx) => {
ctx.replyWithMarkdown(CONSTS.WELCOME_T);
})
//MAIN
bot.hears(CONSTS.MAIN_RE, async (ctx) => {
let m = ctx.message.text;
let arr = m.match(CONSTS.MAIN_RE), i=0, reply='';
const inf = await db.getItems(CONSTS.KEYS);
for(;i<arr.length; i++) {
reply += 'The *';
let aed = arr[i].match(CONSTS.NUM_RE)[0];
console.log(aed);
reply += aed + 'AED* is *';
let usd = aed*inf.rate;
reply += usd.toFixed(2) + 'USD*\n';
}
let last = new Date();
last.setTime(inf.date);
console.log(last);
reply += '\`Last update: ';
reply += last + '\`';
console.log(reply);
ctx.replyWithMarkdown(reply);
})
//WebHooks
app.get('/', (req,res) => res.send('Hello World!'));
app.use(bot.webhookCallback('/webHook'));
bot.telegram.setWebhook(CONSTS.WEBHOOK_URL);
const PORT = process.env.port || 3001;
app.listen(PORT, ()=> {
console.log(`We are on ${PORT}`);
});
//CRON
const cron = require('node-cron');
cron.schedule('10 * * * *', () => update(), true);
async function update() {
const rates = await request.get(CONSTS.RATE_URL);
const forSave = {
rate: 1/rates.rates.AED,
date: Date.now()
}
console.log(forSave.date);
await db.saveItems(forSave);
console.log(`Saved: ${forSave}`);
}