Skip to content

Commit

Permalink
Merge pull request #27 from ArtiStyle/master
Browse files Browse the repository at this point in the history
merge: add sms aero and playmobile transporters
  • Loading branch information
borodayev authored Oct 24, 2018
2 parents de8e0f2 + 324ba1c commit 169b8d4
Show file tree
Hide file tree
Showing 4 changed files with 242 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"homepage": "https://github.com/FrankAst/sms-sender",
"license": "MIT",
"dependencies": {
"axios": "^0.18.0",
"isomorphic-fetch": "^2.2.1",
"mongoose": "^5.0.4",
"babel-runtime": "^6.26.0"
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
export { default as Smsc } from './transporters/Smsc';
export { default as Sns } from './transporters/Sns';
export { default as Mobizon } from './transporters/Mobizon';
export { default as PlayMobile } from './transporters/PlayMobile';
export { default as Aero } from './transporters/Aero';
105 changes: 105 additions & 0 deletions src/transporters/Aero.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// @flow
import 'isomorphic-fetch';
import axios from 'axios';
import type {
SendSmsResponseT,
GetStatusResponseT,
SmsStatusT,
ProviderI,
GetCostResponseT,
GetBalanceResponseT,
} from '../definitions';

type CredentialsT = {|
email: string,
apiKey: string,
|};

export default class PlayMobile implements ProviderI {
credentials: CredentialsT;

constructor(credentials: CredentialsT) {
this.credentials = credentials;
}

// make request to Aero API
async _send(cmd: 'sms/send' | 'sms/status', params: Object): Promise<Object> {
const { email, apiKey } = this.credentials || {};
const extendedParams = {
...params,
};
const url = `https://${email}:${apiKey}@gate.smsaero.ru/v2/${cmd}`;
try {
const res = await axios.get(url, {
params: extendedParams,
});
return res.data;
} catch (e) {
return { error: e.response.status, errMsg: e.response.statusText };
}
}
// eslint-disable-next-line class-methods-use-this
_prepareStatus(status: number): SmsStatusT {
switch (status) {
case 2:
return 'error';
case 6:
return 'error';
case 0:
return 'pending'; //
case 4:
return 'pending'; // - Waiting for sending
case 8:
return 'pending'; // - Given to operator
case 1:
return 'ok'; // - Delivered
case 3:
return 'ok'; // - Delivered
default:
return 'error';
}
}

// send message
async sendSms(phone: string, message: string): Promise<SendSmsResponseT> {
const params = {
number: phone,
text: message,
sign: 'SMS Aero',
channel: 'DIRECT',
};
const rawResponse = await this._send('sms/send', params);
const res = {
messageId: `${rawResponse.id}-${phone}`,
rawResponse,
};
return res;
}

// get status of message (messageId format: id-phoneNumber)
async getStatus(messageId: string): Promise<GetStatusResponseT> {
const params = {
id: messageId.trim().split('-')[0],
};
const rawResponse = await this._send('sms/status', params);
const res = {
status: this._prepareStatus(rawResponse.status),
rawResponse,
};
return res;
}

// get current provider name
// eslint-disable-next-line class-methods-use-this
getProviderName(): string {
return 'Aero';
}
// eslint-disable-next-line
async getBalance(): Promise<GetBalanceResponseT> {
throw new Error(`PlayMobile does not support getting balance`);
}
// eslint-disable-next-line
async getCost(phone: string, message: string): Promise<GetCostResponseT> {
throw new Error(`PlayMobile does not support getting cost`);
}
}
134 changes: 134 additions & 0 deletions src/transporters/PlayMobile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// @flow
import 'isomorphic-fetch';
import axios from 'axios';

import type {
SendSmsResponseT,
GetStatusResponseT,
SmsStatusT,
ProviderI,
GetCostResponseT,
GetBalanceResponseT,
} from '../definitions';

type CredentialsT = {|
url: string,
login: string,
password: string,
|};

type OptionsT = { mesId: number, phone: string, message: string };

export default class PlayMobile implements ProviderI {
credentials: CredentialsT;

constructor(credentials: CredentialsT) {
this.credentials = credentials;
}

// make request to PlayMobile API
async _send(cmd: 'send' | 'get-status', params: Object): Promise<Object> {
const { login, password, url } = this.credentials || {};
const stringToEncode = `${login}:${password}`;
try {
const response = await axios({
method: 'post',
url: `${url}/${cmd}`,
data: params,
headers: {
'Content-Type': 'application/json',
Authorization: `Basic ${Buffer.from(stringToEncode).toString('base64')}`,
},
});
return cmd === 'get-status'
? { body: response.data }
: { statusCode: response.status, body: response.data };
} catch (e) {
return {
statusCode: e.response.status,
errMsg: (e.response && e.response.statusText) || ' ',
};
}
}

// get more detailed description on https://smsc.kz/api/http/status_messages/statuses/#menu
// eslint-disable-next-line class-methods-use-this
_prepareStatus(status: string): SmsStatusT {
switch (status) {
case 'failed':
return 'error'; // - Not delivered
case 'notDelivered':
return 'pending'; // - Waiting for sending
case 'deferred':
return 'pending'; // - Given to operator
case 'transmitted':
return 'ok'; // - Delivered
case 'delivered':
return 'ok'; // - Delivered
default:
return 'error';
}
}

// send message
async sendSms(phone: string, message: string): Promise<SendSmsResponseT> {
const mesId = Math.floor(100000 + Math.random() * 900000);
const params = this.createParams({
mesId,
phone,
message,
});
const rawResponse = await this._send('send', params);
const res = {
messageId: `${mesId}-${phone}`,
rawResponse,
};
return res;
}

// get status of message (messageId format: id-phoneNumber)
async getStatus(messageId: string): Promise<GetStatusResponseT> {
const params = {
'message-id': [messageId.trim().split('-')[0]],
};
const rawResponse = await this._send('get-status', params);
const res = {
status: this._prepareStatus(rawResponse.status),
rawResponse,
};
return res;
}

// get current provider name
// eslint-disable-next-line class-methods-use-this
getProviderName(): string {
return 'PlayMobile';
}
// eslint-disable-next-line
async getBalance(): Promise<GetBalanceResponseT> {
throw new Error(`PlayMobile does not support getting balance`);
}
// eslint-disable-next-line
async getCost(phone: string, message: string): Promise<GetCostResponseT> {
throw new Error(`PlayMobile does not support getting cost`);
}
// eslint-disable-next-line class-methods-use-this
createParams(options: OptionsT): Object {
const { mesId, phone, message } = options || {};
return {
messages: [
{
'message-id': mesId,
recipient: phone,
sms: {
ttl: '300',
originator: 'smsSender',
content: {
text: message,
},
},
},
],
};
}
}

0 comments on commit 169b8d4

Please sign in to comment.