[Utils] add sms

This commit is contained in:
vilP1L
2019-05-25 19:05:22 -04:00
parent 17b984fec4
commit c10fc5208a

44
src/utils/sms.js Normal file
View File

@@ -0,0 +1,44 @@
/* eslint-disable consistent-return */
import request from 'request';
import { promisify } from 'util';
import { sms } from '../../config';
request.promise = promisify(request);
export const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
export const getPhone = async () => {
const { body } = await request.promise(`https://onlinesim.ru/api/getNum.php?apikey=${sms.apiKey}&service=${sms.service}&country=${sms.country}`, {
headers: {
accept: 'application/json',
},
});
const { tzid } = JSON.parse(body);
let result = null;
while (!result) {
const { body } = await request.promise(`https://onlinesim.ru/api/getState.php?apikey=${sms.apiKey}&tzid=${tzid}`, {
headers: {
accept: 'application/json',
},
});
result = JSON.parse(body).shift().number;
if (result) return { result, tzid };
await sleep(2000);
}
};
export const getCode = async (tzid) => {
let result = null;
while (!result) {
const { body } = await request.promise(`https://onlinesim.ru/api/getState.php?apikey=${sms.apiKey}&tzid=${tzid}&message_to_code=1`, {
headers: {
accept: 'application/json',
},
});
result = JSON.parse(body).shift().msg;
const { time } = JSON.parse(body).shift();
if (time < 850) return false;
if (result) return result;
await sleep(2000);
}
};