|
| 1 | +// @flow |
| 2 | +import 'isomorphic-fetch'; |
| 3 | +import axios from 'axios'; |
| 4 | + |
| 5 | +import type { |
| 6 | + SendSmsResponseT, |
| 7 | + GetStatusResponseT, |
| 8 | + SmsStatusT, |
| 9 | + ProviderI, |
| 10 | + GetCostResponseT, |
| 11 | + GetBalanceResponseT, |
| 12 | +} from '../definitions'; |
| 13 | + |
| 14 | +type CredentialsT = {| |
| 15 | + url: string, |
| 16 | + login: string, |
| 17 | + password: string, |
| 18 | +|}; |
| 19 | + |
| 20 | +type OptionsT = { mesId: number, phone: string, message: string }; |
| 21 | + |
| 22 | +export default class PlayMobile implements ProviderI { |
| 23 | + credentials: CredentialsT; |
| 24 | + |
| 25 | + constructor(credentials: CredentialsT) { |
| 26 | + this.credentials = credentials; |
| 27 | + } |
| 28 | + |
| 29 | + // make request to PlayMobile API |
| 30 | + async _send(cmd: 'send' | 'get-status', params: Object): Promise<Object> { |
| 31 | + const { login, password, url } = this.credentials || {}; |
| 32 | + const stringToEncode = `${login}:${password}`; |
| 33 | + try { |
| 34 | + const response = await axios({ |
| 35 | + method: 'post', |
| 36 | + url: `${url}/${cmd}`, |
| 37 | + data: params, |
| 38 | + headers: { |
| 39 | + 'Content-Type': 'application/json', |
| 40 | + Authorization: `Basic ${Buffer.from(stringToEncode).toString('base64')}`, |
| 41 | + }, |
| 42 | + }); |
| 43 | + return cmd === 'get-status' |
| 44 | + ? { body: response.data } |
| 45 | + : { statusCode: response.status, body: response.data }; |
| 46 | + } catch (e) { |
| 47 | + return { |
| 48 | + statusCode: e.response.status, |
| 49 | + errMsg: (e.response && e.response.statusText) || ' ', |
| 50 | + }; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // get more detailed description on https://smsc.kz/api/http/status_messages/statuses/#menu |
| 55 | + // eslint-disable-next-line class-methods-use-this |
| 56 | + _prepareStatus(status: string): SmsStatusT { |
| 57 | + switch (status) { |
| 58 | + case 'failed': |
| 59 | + return 'error'; // - Not delivered |
| 60 | + case 'notDelivered': |
| 61 | + return 'pending'; // - Waiting for sending |
| 62 | + case 'deferred': |
| 63 | + return 'pending'; // - Given to operator |
| 64 | + case 'transmitted': |
| 65 | + return 'ok'; // - Delivered |
| 66 | + case 'delivered': |
| 67 | + return 'ok'; // - Delivered |
| 68 | + default: |
| 69 | + return 'error'; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // send message |
| 74 | + async sendSms(phone: string, message: string): Promise<SendSmsResponseT> { |
| 75 | + const mesId = Math.floor(100000 + Math.random() * 900000); |
| 76 | + const params = this.createParams({ |
| 77 | + mesId, |
| 78 | + phone, |
| 79 | + message, |
| 80 | + }); |
| 81 | + const rawResponse = await this._send('send', params); |
| 82 | + const res = { |
| 83 | + messageId: `${mesId}-${phone}`, |
| 84 | + rawResponse, |
| 85 | + }; |
| 86 | + return res; |
| 87 | + } |
| 88 | + |
| 89 | + // get status of message (messageId format: id-phoneNumber) |
| 90 | + async getStatus(messageId: string): Promise<GetStatusResponseT> { |
| 91 | + const params = { |
| 92 | + 'message-id': [messageId.trim().split('-')[0]], |
| 93 | + }; |
| 94 | + const rawResponse = await this._send('get-status', params); |
| 95 | + const res = { |
| 96 | + status: this._prepareStatus(rawResponse.status), |
| 97 | + rawResponse, |
| 98 | + }; |
| 99 | + return res; |
| 100 | + } |
| 101 | + |
| 102 | + // get current provider name |
| 103 | + // eslint-disable-next-line class-methods-use-this |
| 104 | + getProviderName(): string { |
| 105 | + return 'PlayMobile'; |
| 106 | + } |
| 107 | + // eslint-disable-next-line |
| 108 | + async getBalance(): Promise<GetBalanceResponseT> { |
| 109 | + throw new Error(`PlayMobile does not support getting balance`); |
| 110 | + } |
| 111 | + // eslint-disable-next-line |
| 112 | + async getCost(phone: string, message: string): Promise<GetCostResponseT> { |
| 113 | + throw new Error(`PlayMobile does not support getting cost`); |
| 114 | + } |
| 115 | + // eslint-disable-next-line class-methods-use-this |
| 116 | + createParams(options: OptionsT): Object { |
| 117 | + const { mesId, phone, message } = options || {}; |
| 118 | + return { |
| 119 | + messages: [ |
| 120 | + { |
| 121 | + 'message-id': mesId, |
| 122 | + recipient: phone, |
| 123 | + sms: { |
| 124 | + ttl: '300', |
| 125 | + originator: 'smsSender', |
| 126 | + content: { |
| 127 | + text: message, |
| 128 | + }, |
| 129 | + }, |
| 130 | + }, |
| 131 | + ], |
| 132 | + }; |
| 133 | + } |
| 134 | +} |
0 commit comments