Skip to content

Commit

Permalink
Migrated to Axios
Browse files Browse the repository at this point in the history
  • Loading branch information
raxraj committed May 14, 2020
1 parent 543e680 commit c3e0625
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 433 deletions.
17 changes: 6 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
<a href="https://www.npmjs.com/package/fast-two-sms" target="_blank">
<img alt="Version" src="https://img.shields.io/npm/v/fast-two-sms.svg">
</a>
<a href="https://github.com/raxraj/fast-two-sms#readme" target="_blank">
<a href="https://github.com/raxraj/fast2sms#readme" target="_blank">
<img alt="Documentation" src="https://img.shields.io/badge/documentation-yes-brightgreen.svg" />
</a>
<a href="https://github.com/raxraj/fast-two-sms/graphs/commit-activity" target="_blank">
<a href="https://github.com/raxraj/fast2sms/graphs/commit-activity" target="_blank">
<img alt="Maintenance" src="https://img.shields.io/badge/Maintained%3F-yes-green.svg" />
</a>
<a href="https://github.com/raxraj/fast-two-sms/blob/master/LICENSE" target="_blank">
<a href="https://github.com/raxraj/fast2sms/blob/master/LICENSE" target="_blank">
<img alt="License: MIT" src="https://img.shields.io/github/license/raxraj/fast-two-sms" />
</a>
<a href="https://twitter.com/raxrajtwit" target="_blank">
<img alt="Twitter: raxrajtwit" src="https://img.shields.io/twitter/follow/raxrajtwit.svg?style=social" />
</a>
</p>

> The Module to send Message using FAST2SMS.com
The Module to send Message using FAST2SMS.com

### 🏠 [Homepage](https://github.com/raxraj/fast2sms#readme)

Expand All @@ -35,15 +35,10 @@ This package is provided in these module formats:

## Usage

```sh
```js
const fast2sms = require('fast-two-sms')

var options =
{
authorization : YOUR_API_KEY,
message : 'YOUR_MESSAGE_HERE',
numbers : ['9999999999','8888888888']
}
var options = {authorization : YOUR_API_KEY , message : 'YOUR_MESSAGE_HERE' , numbers : ['9999999999','8888888888']}
fast2sms.sendMessage(options)

```
Expand Down
129 changes: 71 additions & 58 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,76 @@
var unirest = require('unirest')
let chalk = require('chalk');
const axios = require('axios');

const sendMessage = (props)=>{
if(props.method === undefined){
props.method='POST'
}
if(props.sender_id===undefined){
props.sender_id='FSTSMS'
}
if(props.language===undefined){
props.language = 'english'
}
if(props.route===undefined){
props.route = 'p'
}
if(props.flash === undefined){
props.flash = 0;
}
const sendMessage = async (props) => {
if (props.method === undefined) {
props.method = 'POST';
}
if (props.sender_id === undefined) {
props.sender_id = 'FSTSMS';
}
if (props.language === undefined) {
props.language = 'english';
}
if (props.route === undefined) {
props.route = 'p';
}
if (props.flash === undefined) {
props.flash = 0;
}
if (props.showLogs === undefined) {
props.showLogs = true;
}
//THE REASON TO GO WITH (if) and not (props.method = props.method || 'POST') is for empty String and false values.

var req = unirest(props.method , "https://www.fast2sms.com/dev/bulk")
let url = 'https://www.fast2sms.com/dev/bulk';
let nums = props.numbers.join(',');
if (props.method === 'GET') {
//NO-CACHE ONLY IF GET
data = {
authorization: props.authorization,
sender_id: props.sender_id,
message: props.message,
language: props.language,
route: props.route,
numbers: nums,
flash: props.flash
};
headers = {
'cache-control': 'no-cache'
};
} else {
headers = {
authorization: props.authorization
};

var nums = props.numbers.join(',')
console.log(nums);

if(props.method==='GET'){ //NO-CACHE ONLY IF GET
req.query({
"authorization": props.authorization,
"sender_id": props.sender_id,
"message": props.message,
"language": props.language,
"route": props.route,
"numbers": nums,
"flash" : props.flash
});
req.headers({
"cache-control": "no-cache"
});
}
else{
req.headers({
"authorization": props.authorization
});

req.form({
"sender_id": props.sender_id,
"message": props.message,
"language": props.language,
"route": props.route,
"numbers": nums,
});
}

req.end(function (res) {
if (res.error) console.log(res);

console.log(res.body);

});
}
data = {
sender_id: props.sender_id,
message: props.message,
language: props.language,
route: props.route,
numbers: nums
};
}
try {
const response = await axios({
method: props.method,
url,
data,
headers
});
if (props.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;
}
};

module.exports = {
sendMessage : sendMessage
}
sendMessage: sendMessage
};
Loading

0 comments on commit c3e0625

Please sign in to comment.