-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSms.js
103 lines (102 loc) · 2.68 KB
/
Sms.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
const axios = require("axios");
const fs = require("fs");
const path = require("path");
class Sms {
#login;
#password;
#webhookurl;
constructor(config) {
this.#login = config.login;
this.#password = config.password;
this.#webhookurl = config.webhookurl;
}
async auth() {
try {
const response = await axios.post(
"https://notify.eskiz.uz/api/auth/login",
{
email: this.#login,
password: this.#password,
}
);
const data = await response.data;
fs.writeFileSync(
path.join(__dirname, "token.json"),
JSON.stringify(data, null, 2)
);
} catch (error) {
console.log(error.message);
}
}
async send({ phone, message }) {
try {
const data = JSON.parse(
fs.readFileSync(path.join(__dirname, "token.json"))
);
const config = {
method: "post",
maxBodyLength: Infinity,
url: "https://notify.eskiz.uz/api/message/sms/send",
headers: {
Authorization: `Bearer ${data.data.token}`,
},
data: {
mobile_phone: phone,
message: message,
from: 4546,
callback_url: this.#webhookurl,
},
};
const response = await axios(config);
return await response.data;
// throw new Error("hello");
} catch (error) {
// console.log(error.message);
if (error.response.status === 401 || error.response.status === 403) {
await this.auth();
await this.send({ phone, message });
return;
}
return {
error: true,
message: error.response.data.message,
status: error.response.status,
};
}
}
async sendMultiple(arr, dispatch_id) {
try {
const data = JSON.parse(
fs.readFileSync(path.join(__dirname, "token.json"))
);
const config = {
method: "post",
maxBodyLength: Infinity,
url: "https://notify.eskiz.uz/api/message/sms/send-batch",
headers: {
Authorization: `Bearer ${data.data.token}`,
"content-type": "application/json; charset=utf-8",
},
data: JSON.stringify({
messages: arr,
from: 4546,
dispatch_id: dispatch_id,
}),
};
const response = await axios(config);
return response.data;
} catch (error) {
if (error.response.status === 401 || error.response.status === 403) {
await this.auth();
await this.sendMultiple(arr, dispatch_id);
return;
}
return {
error: true,
message: error.response.data.message,
status: error.response.status,
};
}
}
}
module.exports = Sms;