-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from ArtiStyle/master
merge: add sms aero and playmobile transporters
- Loading branch information
Showing
4 changed files
with
242 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}, | ||
}, | ||
], | ||
}; | ||
} | ||
} |