-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
35 lines (31 loc) · 1.46 KB
/
Copy pathutils.js
File metadata and controls
35 lines (31 loc) · 1.46 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
import * as dotenv from 'dotenv'
dotenv.config()
async function getAvailable(origin, destination, date) {
return fetch(`https://ws.alibaba.ir/api/v2/bus/available?orginCityCode=${origin}&destinationCityCode=${destination}&requestDate=${date}&passengerCount=1`).then(r => r.json());
}
export async function checkHasAvailable(origin, destination, date, hours) {
const {result: {availableList: items}} = await getAvailable(origin, destination, date);
return items
.filter(item => item.availableSeats > 0)
.filter(item => new Date(item.departureDateTime).getHours() >= hours[0] && new Date(item.departureDateTime).getHours() <= hours[1] )
}
export function sendNotification(message, phone) {
return fetch(
'http://ippanel.com/api/select',
{
method: 'POST',
body: JSON.stringify({
"op": "send",
"uname": process.env.SMS_USERNAME,
"pass": process.env.SMS_PASSWORD,
"message": message,
"from": process.env.SMS_NUMBER,
"to": phone,
}),
})
.then(r => r.json());
}
export async function sendAvailableNotification(items) {
const message = items.map(item => `${item.companyName}\n${item.orginCityName} => ${item.destinationCityName}\nساعت: ${item.departureTime}\nتعداد صندلی: ${item.availableSeats}`).join('\n\n')
return sendNotification(message, [process.env.NOTIFICATION_NUMBER])
}