-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (60 loc) · 2.13 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
require('./controllers/notificationsController');
const Telegraf = require('telegraf');
const racoAuth = require('./controllers/AuthController');
const api = require('./controllers/ApiController');
const notificationsController = require('./controllers/notificationsController');
const utilsScenes = require('./utils/scenes');
const bot = new Telegraf(process.env.BOT_TOKEN);
bot.context.token = null;
utilsScenes(bot); //load scenes del bot (s'utilitza per placeslliures)
bot.use((ctx, next) => {
const start = new Date();
console.log("persona: " + ctx.message.from.id);
//check has valid token
racoAuth.private_token(ctx.message.from.id).then(function(token){
ctx.token = token;
return next(ctx).then(() => {
const ms = new Date() - start;
console.log('Response time %sms', ms);
})
}, function(){
console.log("no tengo el token, asking...");
ask_token(ctx);
return null;
});
});
bot.start(ctx => {
ctx.reply("Todo esta listo! A partir de ahora te tendré al tanto de los nuevos avisos en el racó ;)");
});
bot.command('data', (ctx) => {
console.log(ctx.token);
api.getData(ctx.token).then(function(res){
console.log(res);
ctx.reply("Nom: " + res.nom + " " + res.cognoms + "\nEmail: " + res.email);
}, function(err){ //error al pillar el token
ask_token(ctx);
});
});
bot.command('foto', (ctx) => {
api.getFoto(ctx.token).then(function(res){
ctx.replyWithPhoto({
source: Buffer.from(res)
});
});
});
bot.command('placeslliures', (ctx) =>{
console.log('places');
ctx.scene.enter('assigs');
});
bot.command('refresh_notis', (ctx) => { //command only for admin
if(ctx.message.from.id == process.env.ADMIN_ID){
timeoutInterval.refresh();
ctx.reply("Done! interval reset"); //hacemos reset, pork lo acabamos de ejecutar....
notificationsController.check_new_notifications();
}
});
function ask_token(ctx){
ctx.reply("Autoriza: "+process.env.URL+"/auth?id="+ctx.message.from.id);
}
let timeoutInterval = notificationsController.run(bot);
bot.startPolling();