-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
109 lines (101 loc) · 2.54 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
"use strict";
const chalk = require("chalk");
const axios = require("axios");
const updateNotifier = require("update-notifier");
//Update Notifications
const pkg = require("./package.json");
const notifier = updateNotifier({ pkg });
notifier.notify();
let getWalletBalance = async function (authorization) {
try {
const response = await axios({
url: "https://www.fast2sms.com/dev/wallet",
method: 'GET',
params: {
authorization,
},
});
return response.data;
} catch (error) {
return error.response.data;
}
};
const sendMessage = async (props) => {
const url = "https://www.fast2sms.com/dev/bulk";
const { data, headers } = generateDataAndHeadersForApi(props);
const { method, showLogs } = props;
try {
const response =
method === "GET"
? await axios({url,data,headers,method})
: await axios.post(url, data, { headers: headers });
if (showLogs) console.log(chalk.greenBright("Message sent successfully."));
return response.data;
} catch (error) {
if (error.response.data.status_code === 412)
if (props.showLogs)
console.log(
chalk.red("Can't send message. Authorization key missing or invalid.")
);
if (error.response.data.status_code === 402)
if (props.showLogs)
console.log(chalk.red("Can't send message. Message text is required."));
if (error.response.data.status_code === 405)
if (props.showLogs)
console.log(
chalk.red("Can't send message.Atleast one Number is required.")
);
return error.response.data;
}
};
let generateDataAndHeadersForApi = function (props) {
const {
authorization,
method = "POST",
sender_id = "FSTSMS",
language = "english",
route = "p",
flash = 0,
numbers,
message,
variables,
variables_values,
} = props;
let nums = numbers.join(",");
let data = {},
headers = {};
if (method === "GET") {
data = {
authorization: authorization,
sender_id: sender_id,
message: message,
language: language,
route: route,
numbers: nums,
flash: flash,
variables,
variables_values,
};
headers = {
"cache-control": "no-cache",
};
} else {
data = {
sender_id: sender_id,
message: message,
language: language,
route: route,
numbers: nums,
variables,
variables_values,
};
headers = {
authorization: authorization,
};
}
return { data, headers };
};
module.exports = {
sendMessage,
getWalletBalance,
};