welsonjs/lib/api.websms.js

104 lines
2.8 KiB
JavaScript
Raw Normal View History

2022-02-10 02:24:43 +00:00
var RAND = require("lib/rand");
2022-01-05 08:43:31 +00:00
var HTTP = require("lib/http");
var WebSMSObject = function() {
2022-01-05 08:59:42 +00:00
this.host = "localhost";
2022-01-05 08:43:31 +00:00
this.token = "";
this.country = "any";
this.operator = "any";
this.product = "";
2022-02-10 02:24:43 +00:00
this.profiles = [];
2022-01-05 08:43:31 +00:00
this.setHost = function(host) {
this.host = host;
return this;
};
this.setToken = function(token) {
this.token = token;
return this;
};
this.setCountry = function(country) {
this.country = country;
return this;
};
this.setOperator = function(operator) {
this.operator = operator;
return this;
};
this.setProduct = function(product) {
this.product = product;
return this;
};
2022-02-10 02:24:43 +00:00
this.addProfile = function(product, country, operator) {
this.profiles.push({
"product": product,
"country": country,
"operator": operator
});
return this;
};
2022-01-05 08:43:31 +00:00
this.buy = function() {
try {
2022-02-10 02:24:43 +00:00
if (this.profiles.length > 0) {
var pf = RAND.one(this.profiles);
this.setProduct(pf.product);
this.setOperator(pf.operator);
this.setCountry(pf.country);
}
2022-01-05 08:43:31 +00:00
var response = HTTP.create()
.setBearerAuth(this.token)
.setUseCache(false)
.setHeader("Accept", "application/json")
.setParameter("country", this.country)
.setParameter("operator", this.operator)
.setParameter("product", this.product)
.open("GET", "https://" + this.host + "/v1/user/buy/activation/:country/:operator/:product")
.send(function(res) {
console.log("Got the number: " + res.phone);
}).responseBody
;
return {
"id": response.id,
2022-01-05 10:59:55 +00:00
"number": response.phone
2022-01-05 08:43:31 +00:00
};
} catch(e) {
console.error(e.message);
}
};
this.get = function(id) {
try {
var response = HTTP.create()
.setBearerAuth(this.token)
.setUseCache(false)
.setHeader("Accept", "application/json")
.setParameter("id", id)
.open("GET", "https://" + this.host + "/v1/user/check/:id")
.send(function(res) {
var messages = res.sms;
messages.forEach(function(x) {
console.log("Got the code: " + x.code);
});
}).responseBody
;
2022-01-05 10:59:55 +00:00
return response.sms.reduce(function(a, x) {
a = x.code;
return a;
2022-01-05 08:43:31 +00:00
}, null);
} catch (e) {
console.error(e.message);
}
};
};
2022-01-05 08:45:07 +00:00
exports.create = function() {
return new WebSMSObject();
};