This repository has been archived by the owner on Nov 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
142 lines (131 loc) · 4.69 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const TeleBot = require('telebot');
const fs = require('fs');
const log4js = require('log4js');
const config = require('./config');
const db = require('./db');
const apiEAV = require('./apiEAV');
const utils = require('./utils');
const bot = new TeleBot({
token: '',
usePlugins: ['askUser']
});
fs.stat('trains.sqlite', (err, stats) => {
if (err) {
db.initDb();
} else {
return null;
}
});
bot.on(['/start'], msg => {
const id = msg.from.id;
db.addUser(id);
db.addCounter(id);
return bot.sendMessage(id, 'Da dove vuoi partire?', {ask: 'stationDeparture'});
});
bot.on(['/lista'], msg => {
const id = msg.from.id;
db.getListStations()
.then((list) => list.map((list) => bot.sendMessage(id, list.nome_staz)));
});
bot.on(['/sendMessage'], msg => {
const id = msg.from.id;
const { adminId } = config;
if (id === adminId) {
return bot.sendMessage(id, 'Inserisci messaggio da inviare', {ask: 'sendMessage'});
} else {
return null;
}
});
bot.on('ask.sendMessage', msg => {
const id = msg.from.id;
const msgToSend = msg.text;
db.getAllUsers().then((res) => {
res.forEach((user) => {
bot.sendMessage(user.user_id, msgToSend)
.catch((err) => logger.error('fetch getStationsUser', err));
});
});
return null;
});
bot.on('ask.stationDeparture', msg => {
const id = msg.from.id;
const stationDeparture = msg.text;
const station = apiEAV.getInfoStation(stationDeparture);
station.then((station) => {
if (station.length === 0) {
return bot.sendMessage(id, `Non c'è nessuna fermata con questo nome`, {ask: 'stationDeparture'});
} else if (station.length === 1) {
const stationCode = station[0].Codice;
db.addDeparture(id, stationCode);
return bot.sendMessage(id, `Adesso, dove vuoi arrivare?`, {ask: 'stationArrival'});
} else if (station.length > 1) {
let stations = [];
station.forEach((station) => {
stations.push(station.Descrizione);
});
const replyMarkup = bot.keyboard(([stations]), {resize: true, once: true});
return bot.sendMessage(id, `Devi essere più specifico`, {ask: 'stationDeparture', replyMarkup});
}
});
});
bot.on('ask.stationArrival', msg => {
const id = msg.from.id;
const stationArrival = msg.text;
const station = apiEAV.getInfoStation(stationArrival);
station.then((station) => {
if (station.length === 0) {
return bot.sendMessage(id, `Non c'è nessuna fermata con questo nome`, {ask: 'stationArrival'});
} else if (station.length === 1) {
const stationCode = station[0].Codice;
db.addArrive(id, stationCode);
return bot.sendMessage(id, `A che ora vuoi partire?`, {ask: 'stationTime'});
} else if (station.length > 1) {
let stations = [];
station.forEach((station) => {
stations.push(station.Descrizione);
});
const replyMarkup = bot.keyboard(([stations]), {resize: true, once: true});
return bot.sendMessage(id, `Devi essere più specifico`, {ask: 'stationArrival', replyMarkup});
}
});
});
bot.on('ask.stationTime', msg => {
const id = msg.from.id;
const stationTime = msg.text.slice(0, 2).replace('.', '');
if (!isNaN(stationTime) && stationTime <= 24) {
db.addTime(id, stationTime);
} else {
msg.reply.text('Sembra che non hai inserito un orario corretto');
return bot.sendMessage(id, `A che ora vuoi partire?`, {ask: 'stationTime'});
}
db.getStationsUser(id)
.then(userTripInformation => {
const date = utils.getDate();
const {departure, arrive, time} = userTripInformation;
apiEAV.getTrip(date, arrive, time, departure)
.then((res) => res.json())
.then((tripsInfo) => {
let promise = Promise.resolve();
tripsInfo.forEach((trip) => {
const destination = trip.Descrizione_destinazione;
const departure = trip.Descrizione_origine;
const timeDeparture = new Date(parseInt(trip.partenza.substr(6)));
const timeDestination = new Date(parseInt(trip.arrivo.substr(6)));
const timeDepartureToString = utils.getTime(timeDeparture);
const timeDestinationToString = utils.getTime(timeDestination);
promise = promise.then(() => bot.sendMessage(id, `🚆 PARTENZA DA: ${departure} ⌛ ${timeDepartureToString} \n \n` +
`🚆 ARRIVO A: ${destination} ⌛ ${timeDestinationToString} \n \n`
));
});
})
.catch((e) => {
const logger = log4js.getLogger('error');
logger.error('fetch getStationsUser', e);
});
});
});
bot.start();
process.on('uncaughtException', function (exception) {
const logger = log4js.getLogger('error');
logger.error('crash bot', exception);
});