-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
369 lines (336 loc) · 10.3 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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
const { Telegraf, Markup, Context, session, Stage, WizardScene, Scenes } = require("telegraf");
const { Keyboard, Key } = require('telegram-keyboard')
const { CallbackData } = require('telegraf-callback-data');
var express = require('express');
const mongoose = require('mongoose');
var app = express();
app.set('port', (process.env.PORT || 5000));
app.get('/', function(request, response) {
var result = 'App is running'
response.send(result);
}).listen(app.get('port'), function() {
console.log('App is running, server is listening on port ', app.get('port'));
});
require('dotenv').config();
mongoose.connect(process.env.MONGO);
const request = require('request');
var dateFormat = require('dateformat');
const savingData = new CallbackData(
'saving',
['user_id', 'number', 'type']
);
const bot = new Telegraf(process.env.TOKEN);
const Schema = mongoose.Schema;
userSchema = new Schema({
id: String,
first_name: String,
last_name: String,
});
trackingSchema = new Schema({
id: String,
number: String,
name: String,
creator: String,
notify: Boolean,
});
User = mongoose.model('User', userSchema)
Tracking = mongoose.model('Tracking', trackingSchema)
var currentID, callbackData, currentNotitfy = true, callbackDeleteData;
var setTrackingName = new Scenes.WizardScene(
"setTrackingName",
async (ctx) => {
await ctx.reply("Parfait ! Comment souhaitez vous nommer ce suivi ?").catch(error => showError(error, ctx));
return await ctx.wizard.next()
},
async (ctx) => {
if (!ctx.message){
return ;
}
const newTracking = new Tracking({
number: currentID,
name: ctx.message.text,
creator: ctx.chat.id,
notify: currentNotitfy,
});
await newTracking.save();
await ctx.reply("Le numéro de suivi a été enregistré. Vous serez notifié de son statut directement ici.").catch(error => showError(error, ctx))
return await ctx.scene.leave().catch(error => showError(error, ctx));
},
);
stage = new Scenes.Stage()
stage.register(setTrackingName);
bot.use(session());
bot.use(stage.middleware());
async function showError(err, ctx){
console.log(err)
await bot.telegram.sendMessage(
ctx.chat.id,
err
).catch(error => console.log(error))
}
setInterval(async function () {
await Tracking.find({}, async function(err, trackings){
if (err){
await showError(err, ctx)
} else{
await trackings.forEach( async (item, i) => {
if (item.notify){
var nuuuu = item.number;
let url='https://api.laposte.fr/suivi/v2/idships/' + nuuuu;
if (nuuuu[0] == '/'){
showingKeyboard = false
var nuu2 = nuuuu.substring(1, nuuuu.length)
url='https://api.laposte.fr/suivi/v2/idships/' + nuu2;
}
await request({
url: url,
method: 'GET',
headers: {
'Accept' : 'application/json',
'X-Okapi-Key': process.env.POSTE
}
}, async function (error, response, body) {
if (error) throw error;
if (JSON.parse(body).returnCode != 200){
console.log(JSON.parse(body).returnMessage)
}
else{
const data = JSON.parse(body)
const shipmentData = data.shipment
let message = "<b>" + shipmentData.product + "</b> (" + "<a href='" + shipmentData.url + "'>" +shipmentData.idShip + "</a>) ✅"
if (shipmentData.isFinal){
trackings[i].notify = false;
trackings[i].save();
await bot.telegram.sendMessage(
item.creator,
message,
{
parse_mode: 'HTML',
disable_web_page_preview: true,
},
).catch(error => showError(error, ctx))
}
}
});
}
});
}
})
}, 1 * 60 * 60 * 1000); // 1 hour
setInterval(async function () {
var nbUsers = 0, nbTrackings = 0;
await User.find({}, async function (err, docs) {
if (!err){
nbUsers = docs.length;
}
else{
functions.error(err, ctx)
}
});
await Tracking.find({}, async function (err, docs) {
if (!err){
nbTrackings = docs.length;
}
else{
functions.error(err, ctx)
}
});
await bot.telegram.sendMessage(
"492723430",
"Nombre d'utilisateurs : " + nbUsers.toString() + "\nNombre de suivis : " + nbTrackings.toString()
).catch(error => functions.error(error, ctx))
}, 1 * 60 * 60 * 1000 * 24);
bot.command('stats', async ctx => {
var nbUsers = 0, nbTrackings = 0;
await User.find({}, async function (err, docs) {
if (!err){
nbUsers = docs.length;
}
else{
functions.error(err, ctx)
}
});
await Tracking.find({}, async function (err, docs) {
if (!err){
nbTrackings = docs.length;
}
else{
functions.error(err, ctx)
}
});
await bot.telegram.sendMessage(
ctx.chat.id,
"Nombre d'utilisateurs : " + nbUsers.toString() + "\nNombre de suivis : " + nbTrackings.toString()
).catch(error => functions.error(error, ctx))
});
bot.command('start', async ctx => {
await User.find({ id: ctx.chat.id}, async function (err, docs) {
if (err){
await showError(err, ctx)
}
else {
if (docs.length == 0){
const newUser = new User({
id: ctx.chat.id,
first_name: ctx.chat.first_name,
last_name: ctx.chat.last_name,
});
await newUser.save();
}
await bot.telegram.sendMessage(
ctx.chat.id,
"Salut, envoie-moi le numéro de l'envoi pour être notifié de son statut.\n\n/suivis – Pour voir les numéros enregistrés"
).catch(error => showError(error, ctx))
}
});
});
bot.command('suivis', async ctx => {
await Tracking.find({creator: ctx.chat.id}, async function(err, trackings){
if (err){
await showError(err, ctx)
} else{
let trackingsMessage;
if (trackings.length == 0){
trackingsMessage = "Vous n'avez pas de numéro enregistré.";
}
else{
trackingsMessage = "Vos numéros enregistrés. Utilisez la commande à côté du nom pour les afficher :" + "\n"
}
await trackings.forEach((item, i) => {
trackingsMessage += '\n' + trackings[i].name + " – /" + trackings[i].number
});
await bot.telegram.sendMessage(
ctx.chat.id,
trackingsMessage,
).catch(error => showError(error, ctx))
}
})
});
bot.on('text', async (ctx) => {
var showingKeyboard = true
var nuuuu = await ctx.message.text
let url='https://api.laposte.fr/suivi/v2/idships/' + nuuuu;
if (nuuuu[0] == '/'){
showingKeyboard = false
var nuu2 = nuuuu.substring(1, nuuuu.length)
url='https://api.laposte.fr/suivi/v2/idships/' + nuu2;
}
await request({
url: url,
method: 'GET',
headers: {
'Accept' : 'application/json',
'X-Okapi-Key': process.env.POSTE
}
}, async function (error, response, body) {
if (error) throw error;
if (JSON.parse(body).returnCode != 200){
await bot.telegram.sendMessage(
ctx.chat.id,
JSON.parse(body).returnMessage,
).catch(er2 => showError(er2, ctx));
}
else{
const data = await JSON.parse(body)
const shipmentData = data.shipment
let message = "<b>" + shipmentData.product + "</b> (" + "<a href='" + shipmentData.url + "'>" +shipmentData.idShip + "</a>) ";
if (shipmentData.isFinal){
currentNotitfy = false;
message += "✅"
}
else{
currentNotitfy = true;
message += "🕐"
}
message += "\n\n";
const timeline = await shipmentData.timeline
await timeline.forEach((item, i) => {
if (item.status){
message += "<b>" + item.shortLabel + "</b>"
}
else{
message += item.shortLabel
}
if (item.date){
var day = dateFormat(item.date, "dd/mm");
message += "<b> le " + day + "</b>"
}
if (item.longLabel.length > 0){
message += " (" + item.longLabel + ")";
}
message += "\n";
});
if (showingKeyboard){
callbackData = savingData.create({
user_id: ctx.chat.id,
number: shipmentData.idShip,
type: 'save'
})
await bot.telegram.sendMessage(
ctx.chat.id,
message,
{
parse_mode: 'HTML',
disable_web_page_preview: true,
"reply_markup":
{
"inline_keyboard": [
[
{"text": "Enregistrer", "callback_data": callbackData, "hide": false},
],
]
},
},
).catch(err3 => showError(err3, ctx));
}
else{
callbackData = savingData.create({
user_id: ctx.chat.id,
number: shipmentData.idShip,
type: 'delete'
})
await bot.telegram.sendMessage(
ctx.chat.id,
message,
{
parse_mode: 'HTML',
disable_web_page_preview: true,
"reply_markup":
{
"inline_keyboard": [
[
{"text": "Supprimer", "callback_data": callbackData, "hide": false},
],
]
},
},
).catch(error => showError(error, ctx));
}
}
});
});
bot.action(
savingData.filter({
type: 'save',
}),
async (ctx) => {
currentID = await savingData.parse(callbackData).number
await ctx.scene.enter("setTrackingName");
}
);
bot.action(
savingData.filter({
type: 'delete',
}),
async (ctx) => {
let trackID = currentID = await savingData.parse(callbackData).number
await Tracking.deleteOne({number: trackID}, async function (err, dish) {
if (err){
await showError(err, ctx)
}
})
await ctx.deleteMessage().catch(error => showError(error, ctx));
await bot.telegram.sendMessage(ctx.chat.id, "Le suivi a été supprimé !").catch(error => showError(error, ctx))
}
);
bot.launch()