@isor/sink
Version:
Library for working with sms-activate.ru
94 lines (93 loc) • 3.67 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ServiceOverseer = void 0;
const axios_1 = __importDefault(require("axios"));
const equal_1 = require("./equal");
const BASE_URL = "https://api.sms-activate.org/stubs/handler_api.php";
const WAIT_TIMEOUT = 3000;
const MAX_TIMEOUT = 240000;
class ServiceOverseer {
constructor(accessToken, id, phone) {
this.accessToken = accessToken;
this.id = id;
this.phone = phone;
}
get phoneNumber() {
return this.phone;
}
cancel() {
return __awaiter(this, void 0, void 0, function* () {
const response = yield axios_1.default.get(BASE_URL, {
params: {
api_key: this.accessToken,
action: "setStatus",
status: 8,
id: this.id,
},
});
(0, equal_1.Equal)(response.data);
});
}
message(regular, replaceValue = "") {
return __awaiter(this, void 0, void 0, function* () {
const defender = setTimeout(() => __awaiter(this, void 0, void 0, function* () {
yield this.cancel();
throw new Error("Waiting time out");
}), MAX_TIMEOUT);
yield axios_1.default.get(BASE_URL, {
params: {
api_key: this.accessToken,
action: "setStatus",
status: 1,
id: this.id,
},
});
while (true) {
yield new Promise((resolve) => {
setTimeout(() => {
return resolve();
}, WAIT_TIMEOUT);
});
const response = yield axios_1.default.get(BASE_URL, {
params: {
api_key: this.accessToken,
action: "getStatus",
id: this.id,
},
});
const [targetId] = response.data.split(":");
(0, equal_1.Equal)(targetId);
if (targetId === "STATUS_OK") {
clearTimeout(defender);
const message = response.data.substring(response.data.indexOf(":") + 1);
return message.replace(regular, replaceValue);
}
}
});
}
done() {
return __awaiter(this, void 0, void 0, function* () {
yield axios_1.default.get(BASE_URL, {
params: {
api_key: this.accessToken,
action: "setStatus",
status: 6,
id: this.id,
},
});
});
}
}
exports.ServiceOverseer = ServiceOverseer;