1- var unirest = require ( 'unirest' )
1+ let chalk = require ( 'chalk' ) ;
2+ const axios = require ( 'axios' ) ;
23
3- const sendMessage = ( props ) => {
4- if ( props . method === undefined ) {
5- props . method = 'POST'
6- }
7- if ( props . sender_id === undefined ) {
8- props . sender_id = 'FSTSMS'
9- }
10- if ( props . language === undefined ) {
11- props . language = 'english'
12- }
13- if ( props . route === undefined ) {
14- props . route = 'p'
15- }
16- if ( props . flash === undefined ) {
17- props . flash = 0 ;
18- }
4+ const sendMessage = async ( props ) => {
5+ if ( props . method === undefined ) {
6+ props . method = 'POST' ;
7+ }
8+ if ( props . sender_id === undefined ) {
9+ props . sender_id = 'FSTSMS' ;
10+ }
11+ if ( props . language === undefined ) {
12+ props . language = 'english' ;
13+ }
14+ if ( props . route === undefined ) {
15+ props . route = 'p' ;
16+ }
17+ if ( props . flash === undefined ) {
18+ props . flash = 0 ;
19+ }
20+ if ( props . showLogs === undefined ) {
21+ props . showLogs = true ;
22+ }
23+ //THE REASON TO GO WITH (if) and not (props.method = props.method || 'POST') is for empty String and false values.
1924
20- var req = unirest ( props . method , "https://www.fast2sms.com/dev/bulk" )
25+ let url = 'https://www.fast2sms.com/dev/bulk' ;
26+ let nums = props . numbers . join ( ',' ) ;
27+ if ( props . method === 'GET' ) {
28+ //NO-CACHE ONLY IF GET
29+ data = {
30+ authorization : props . authorization ,
31+ sender_id : props . sender_id ,
32+ message : props . message ,
33+ language : props . language ,
34+ route : props . route ,
35+ numbers : nums ,
36+ flash : props . flash
37+ } ;
38+ headers = {
39+ 'cache-control' : 'no-cache'
40+ } ;
41+ } else {
42+ headers = {
43+ authorization : props . authorization
44+ } ;
2145
22- var nums = props . numbers . join ( ',' )
23- console . log ( nums ) ;
24-
25- if ( props . method === 'GET' ) { //NO-CACHE ONLY IF GET
26- req . query ( {
27- "authorization" : props . authorization ,
28- "sender_id" : props . sender_id ,
29- "message" : props . message ,
30- "language" : props . language ,
31- "route" : props . route ,
32- "numbers" : nums ,
33- "flash" : props . flash
34- } ) ;
35- req . headers ( {
36- "cache-control" : "no-cache"
37- } ) ;
38- }
39- else {
40- req . headers ( {
41- "authorization" : props . authorization
42- } ) ;
43-
44- req . form ( {
45- "sender_id" : props . sender_id ,
46- "message" : props . message ,
47- "language" : props . language ,
48- "route" : props . route ,
49- "numbers" : nums ,
50- } ) ;
51- }
52-
53- req . end ( function ( res ) {
54- if ( res . error ) console . log ( res ) ;
55-
56- console . log ( res . body ) ;
57-
58- } ) ;
59- }
46+ data = {
47+ sender_id : props . sender_id ,
48+ message : props . message ,
49+ language : props . language ,
50+ route : props . route ,
51+ numbers : nums
52+ } ;
53+ }
54+ try {
55+ const response = await axios ( {
56+ method : props . method ,
57+ url,
58+ data,
59+ headers
60+ } ) ;
61+ if ( props . showLogs ) console . log ( chalk . greenBright ( 'Message sent successfully.' ) ) ;
62+ return response . data ;
63+ } catch ( error ) {
64+ if ( error . response . data . status_code === 412 )
65+ if ( props . showLogs ) console . log ( chalk . red ( "Can't send message. Authorization key missing or invalid." ) ) ;
66+ if ( error . response . data . status_code === 402 )
67+ if ( props . showLogs ) console . log ( chalk . red ( "Can't send message. Message text is required." ) ) ;
68+ if ( error . response . data . status_code === 405 )
69+ if ( props . showLogs ) console . log ( chalk . red ( "Can't send message.Atleast one Number is required." ) ) ;
70+ return error . response . data ;
71+ }
72+ } ;
6073
6174module . exports = {
62- sendMessage : sendMessage
63- }
75+ sendMessage : sendMessage
76+ } ;
0 commit comments