diff --git a/package-lock.json b/package-lock.json
index 3060a49..d1ac651 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -9,7 +9,7 @@
"version": "2.0.1",
"license": "MIT",
"dependencies": {
- "undici": "^5.4.0"
+ "undici": "^5.28.4"
},
"devDependencies": {
"clean-jsdoc-theme": "^4.1.6",
@@ -177,6 +177,14 @@
"node": "^12.22.0 || ^14.17.0 || >=16.0.0"
}
},
+ "node_modules/@fastify/busboy": {
+ "version": "2.1.1",
+ "resolved": "https://registry.npmjs.org/@fastify/busboy/-/busboy-2.1.1.tgz",
+ "integrity": "sha512-vBZP4NlzfOlerQTnba4aqZoMhE/a9HY7HRqoOPaETQcSQuWEIyZMHGfVu6w9wGtGK5fED5qRs2DteVCjOH60sA==",
+ "engines": {
+ "node": ">=14"
+ }
+ },
"node_modules/@humanwhocodes/config-array": {
"version": "0.9.5",
"resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.9.5.tgz",
@@ -4292,11 +4300,14 @@
"dev": true
},
"node_modules/undici": {
- "version": "5.4.0",
- "resolved": "https://registry.npmjs.org/undici/-/undici-5.4.0.tgz",
- "integrity": "sha512-A1SRXysDg7J+mVP46jF+9cKANw0kptqSFZ8tGyL+HBiv0K1spjxPX8Z4EGu+Eu6pjClJUBdnUPlxrOafR668/g==",
+ "version": "5.28.4",
+ "resolved": "https://registry.npmjs.org/undici/-/undici-5.28.4.tgz",
+ "integrity": "sha512-72RFADWFqKmUb2hmmvNODKL3p9hcB6Gt2DOQMis1SEBaV6a4MH8soBvzg+95CYhCKPFedut2JY9bMfrDl9D23g==",
+ "dependencies": {
+ "@fastify/busboy": "^2.0.0"
+ },
"engines": {
- "node": ">=12.18"
+ "node": ">=14.0"
}
},
"node_modules/unified": {
@@ -4879,6 +4890,11 @@
"strip-json-comments": "^3.1.1"
}
},
+ "@fastify/busboy": {
+ "version": "2.1.1",
+ "resolved": "https://registry.npmjs.org/@fastify/busboy/-/busboy-2.1.1.tgz",
+ "integrity": "sha512-vBZP4NlzfOlerQTnba4aqZoMhE/a9HY7HRqoOPaETQcSQuWEIyZMHGfVu6w9wGtGK5fED5qRs2DteVCjOH60sA=="
+ },
"@humanwhocodes/config-array": {
"version": "0.9.5",
"resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.9.5.tgz",
@@ -7862,9 +7878,12 @@
"dev": true
},
"undici": {
- "version": "5.4.0",
- "resolved": "https://registry.npmjs.org/undici/-/undici-5.4.0.tgz",
- "integrity": "sha512-A1SRXysDg7J+mVP46jF+9cKANw0kptqSFZ8tGyL+HBiv0K1spjxPX8Z4EGu+Eu6pjClJUBdnUPlxrOafR668/g=="
+ "version": "5.28.4",
+ "resolved": "https://registry.npmjs.org/undici/-/undici-5.28.4.tgz",
+ "integrity": "sha512-72RFADWFqKmUb2hmmvNODKL3p9hcB6Gt2DOQMis1SEBaV6a4MH8soBvzg+95CYhCKPFedut2JY9bMfrDl9D23g==",
+ "requires": {
+ "@fastify/busboy": "^2.0.0"
+ }
},
"unified": {
"version": "10.1.2",
diff --git a/package.json b/package.json
index 0fc75ab..aa7f81a 100644
--- a/package.json
+++ b/package.json
@@ -10,17 +10,17 @@
"remark": "npx remark README.md --use remark-preset-lint-consistent --use remark-preset-lint-recommended"
},
"dependencies": {
- "undici": "^5.4.0"
+ "undici": "^5.28.4"
},
"devDependencies": {
+ "clean-jsdoc-theme": "^4.1.6",
"eslint": "^8.17.0",
"eslint-config-standard": "^17.0.0",
"eslint-plugin-import": "^2.26.0",
+ "eslint-plugin-jsdoc": "^37.7.0",
"eslint-plugin-n": "^15.2.1",
"eslint-plugin-promise": "^6.0.0",
"jsdoc": "^3.6.10",
- "eslint-plugin-jsdoc": "^37.7.0",
- "clean-jsdoc-theme": "^4.1.6",
"remark-cli": "^10.0.1",
"remark-preset-lint-consistent": "^5.1.1",
"remark-preset-lint-recommended": "^6.1.2"
diff --git a/src/countries.json b/src/countries.json
new file mode 100644
index 0000000..8d15cfa
--- /dev/null
+++ b/src/countries.json
@@ -0,0 +1,660 @@
+[
+ {
+ "code": "RU",
+ "country": "Russia",
+ "phoneCode": 7,
+ "smsHubId": 0,
+ "smsBowerId": 0
+ },
+ {
+ "code": "UA",
+ "country": "Ukraine",
+ "phoneCode": 380,
+ "smsHubId": 1,
+ "smsBowerId": 1
+ },
+ {
+ "code": "KZ",
+ "country": "Kazakhstan",
+ "phoneCode": 7,
+ "smsHubId": 2,
+ "smsBowerId": 2
+ },
+ {
+ "code": "CN",
+ "country": "China",
+ "phoneCode": 86,
+ "smsHubId": 3,
+ "smsBowerId": 3
+ },
+ {
+ "code": "PH",
+ "country": "Philippines",
+ "phoneCode": 63,
+ "smsHubId": 4,
+ "smsBowerId": 4
+ },
+ {
+ "code": "MM",
+ "country": "Myanmar",
+ "phoneCode": 95,
+ "smsHubId": 5,
+ "smsBowerId": 5
+ },
+ {
+ "code": "ID",
+ "country": "Indonesia",
+ "phoneCode": 62,
+ "smsHubId": 6,
+ "smsBowerId": 6
+ },
+ {
+ "code": "MY",
+ "country": "Malaysia",
+ "phoneCode": 60,
+ "smsHubId": 7,
+ "smsBowerId": 7
+ },
+ {
+ "code": "KE",
+ "country": "Kenya",
+ "phoneCode": 254,
+ "smsHubId": 8,
+ "smsBowerId": 8
+ },
+ {
+ "code": "TZ",
+ "country": "Tanzania",
+ "phoneCode": 255,
+ "smsHubId": 9,
+ "smsBowerId": 9
+ },
+ {
+ "code": "VN",
+ "country": "Vietnam",
+ "phoneCode": 84,
+ "smsHubId": 10,
+ "smsBowerId": 10
+ },
+ {
+ "code": "KG",
+ "country": "Kyrgyzstan",
+ "phoneCode": 996,
+ "smsHubId": 11,
+ "smsBowerId": 11
+ },
+ {
+ "code": "US",
+ "country": "USA (Virtual)",
+ "phoneCode": 1,
+ "smsHubId": 12,
+ "smsBowerId": 12
+ },
+ {
+ "code": "US_nonvirt",
+ "country": "USA",
+ "phoneCode": 1,
+ "smsHubId": 187,
+ "smsBowerId": 187
+ },
+ {
+ "code": "IL",
+ "country": "Israel",
+ "phoneCode": 972,
+ "smsHubId": 13,
+ "smsBowerId": 13
+ },
+ {
+ "code": "HK",
+ "country": "Hong Kong",
+ "phoneCode": 852,
+ "smsHubId": 14,
+ "smsBowerId": 14
+ },
+ {
+ "code": "PL",
+ "country": "Poland",
+ "phoneCode": 48,
+ "smsHubId": 15,
+ "smsBowerId": 15
+ },
+ {
+ "code": "GB",
+ "country": "United Kingdom",
+ "phoneCode": 44,
+ "smsHubId": 16,
+ "smsBowerId": 16
+ },
+ {
+ "code": "MG",
+ "country": "Madagascar",
+ "phoneCode": 261,
+ "smsHubId": 17,
+ "smsBowerId": 17
+ },
+ {
+ "code": "CD",
+ "country": "Democratic Republic of the Congo",
+ "phoneCode": 243,
+ "smsHubId": 18,
+ "smsBowerId": 18
+ },
+ {
+ "code": "NG",
+ "country": "Nigeria",
+ "phoneCode": 234,
+ "smsHubId": 19,
+ "smsBowerId": 19
+ },
+ {
+ "code": "MO",
+ "country": "Macao",
+ "phoneCode": 853,
+ "smsHubId": 20,
+ "smsBowerId": 20
+ },
+ {
+ "code": "EG",
+ "country": "Egypt",
+ "phoneCode": 20,
+ "smsHubId": 21,
+ "smsBowerId": 21
+ },
+ {
+ "code": "IN",
+ "country": "India",
+ "phoneCode": 91,
+ "smsHubId": 22,
+ "smsBowerId": 22
+ },
+ {
+ "code": "IE",
+ "country": "Ireland",
+ "phoneCode": 353,
+ "smsHubId": 23,
+ "smsBowerId": 23
+ },
+ {
+ "code": "KH",
+ "country": "Cambodia",
+ "phoneCode": 855,
+ "smsHubId": 24,
+ "smsBowerId": 24
+ },
+ {
+ "code": "LA",
+ "country": "Laos",
+ "phoneCode": 856,
+ "smsHubId": 25,
+ "smsBowerId": 25
+ },
+ {
+ "code": "HT",
+ "country": "Haiti",
+ "phoneCode": 509,
+ "smsHubId": 26,
+ "smsBowerId": 26
+ },
+ {
+ "code": "CI",
+ "country": "Côte d'Ivoire",
+ "phoneCode": 225,
+ "smsHubId": 27,
+ "smsBowerId": 27
+ },
+ {
+ "code": "GM",
+ "country": "Gambia",
+ "phoneCode": 220,
+ "smsHubId": 28,
+ "smsBowerId": 28
+ },
+ {
+ "code": "RS",
+ "country": "Serbia",
+ "phoneCode": 381,
+ "smsHubId": 29,
+ "smsBowerId": 29
+ },
+ {
+ "code": "YE",
+ "country": "Yemen",
+ "phoneCode": 967,
+ "smsHubId": 30,
+ "smsBowerId": 30
+ },
+ {
+ "code": "ZA",
+ "country": "South Africa",
+ "phoneCode": 27,
+ "smsHubId": 31,
+ "smsBowerId": 31
+ },
+ {
+ "code": "RO",
+ "country": "Romania",
+ "phoneCode": 40,
+ "smsHubId": 32,
+ "smsBowerId": 32
+ },
+ {
+ "code": "CO",
+ "country": "Colombia",
+ "phoneCode": 57,
+ "smsHubId": 33,
+ "smsBowerId": 33
+ },
+ {
+ "code": "EE",
+ "country": "Estonia",
+ "phoneCode": 372,
+ "smsHubId": 34,
+ "smsBowerId": 34
+ },
+ {
+ "code": "AZ",
+ "country": "Azerbaijan",
+ "phoneCode": 994,
+ "smsHubId": 35,
+ "smsBowerId": 35
+ },
+ {
+ "code": "CA",
+ "country": "Canada",
+ "phoneCode": 1,
+ "smsHubId": 36,
+ "smsBowerId": 36
+ },
+ {
+ "code": "MA",
+ "country": "Morocco",
+ "phoneCode": 212,
+ "smsHubId": 37,
+ "smsBowerId": 37
+ },
+ {
+ "code": "GH",
+ "country": "Ghana",
+ "phoneCode": 233,
+ "smsHubId": 38,
+ "smsBowerId": 38
+ },
+ {
+ "code": "AR",
+ "country": "Argentina",
+ "phoneCode": 54,
+ "smsHubId": 39,
+ "smsBowerId": 39
+ },
+ {
+ "code": "UZ",
+ "country": "Uzbekistan",
+ "phoneCode": 998,
+ "smsHubId": 40,
+ "smsBowerId": 40
+ },
+ {
+ "code": "CM",
+ "country": "Cameroon",
+ "phoneCode": 237,
+ "smsHubId": 41,
+ "smsBowerId": 41
+ },
+ {
+ "code": "TD",
+ "country": "Chad",
+ "phoneCode": 235,
+ "smsHubId": 42,
+ "smsBowerId": 42
+ },
+ {
+ "code": "DE",
+ "country": "Germany",
+ "phoneCode": 49,
+ "smsHubId": 43,
+ "smsBowerId": 43
+ },
+ {
+ "code": "LT",
+ "country": "Lithuania",
+ "phoneCode": 370,
+ "smsHubId": 44,
+ "smsBowerId": 44
+ },
+ {
+ "code": "HR",
+ "country": "Croatia",
+ "phoneCode": 385,
+ "smsHubId": 45,
+ "smsBowerId": 45
+ },
+ {
+ "code": "SE",
+ "country": "Sweden",
+ "phoneCode": 46,
+ "smsHubId": 46,
+ "smsBowerId": 174
+ },
+ {
+ "code": "IQ",
+ "country": "Iraq",
+ "phoneCode": 964,
+ "smsHubId": 47,
+ "smsBowerId": 47
+ },
+ {
+ "code": "NL",
+ "country": "Netherlands",
+ "phoneCode": 31,
+ "smsHubId": 48,
+ "smsBowerId": 48
+ },
+ {
+ "code": "LV",
+ "country": "Latvia",
+ "phoneCode": 371,
+ "smsHubId": 49,
+ "smsBowerId": 49
+ },
+ {
+ "code": "AT",
+ "country": "Austria",
+ "phoneCode": 43,
+ "smsHubId": 50,
+ "smsBowerId": 50
+ },
+ {
+ "code": "BY",
+ "country": "Belarus",
+ "phoneCode": 375,
+ "smsHubId": 51,
+ "smsBowerId": 51
+ },
+ {
+ "code": "TH",
+ "country": "Thailand",
+ "phoneCode": 66,
+ "smsHubId": 52,
+ "smsBowerId": 52
+ },
+ {
+ "code": "SA",
+ "country": "Saudi Arabia",
+ "phoneCode": 966,
+ "smsHubId": 53,
+ "smsBowerId": 53
+ },
+ {
+ "code": "MX",
+ "country": "Mexico",
+ "phoneCode": 52,
+ "smsHubId": 54,
+ "smsBowerId": 54
+ },
+ {
+ "code": "TW",
+ "country": "Taiwan",
+ "phoneCode": 886,
+ "smsHubId": 55,
+ "smsBowerId": 55
+ },
+ {
+ "code": "ES",
+ "country": "Spain",
+ "phoneCode": 34,
+ "smsHubId": 56,
+ "smsBowerId": 56
+ },
+ {
+ "code": "IR",
+ "country": "Iran",
+ "phoneCode": 98,
+ "smsHubId": 57,
+ "smsBowerId": 57
+ },
+ {
+ "code": "DZ",
+ "country": "Algeria",
+ "phoneCode": 213,
+ "smsHubId": 58,
+ "smsBowerId": 58
+ },
+ {
+ "code": "SI",
+ "country": "Slovenia",
+ "phoneCode": 386,
+ "smsHubId": 59,
+ "smsBowerId": 59
+ },
+ {
+ "code": "BD",
+ "country": "Bangladesh",
+ "phoneCode": 880,
+ "smsHubId": 60,
+ "smsBowerId": 60
+ },
+ {
+ "code": "SN",
+ "country": "Senegal",
+ "phoneCode": 221,
+ "smsHubId": 61,
+ "smsBowerId": 61
+ },
+ {
+ "code": "TR",
+ "country": "Turkey",
+ "phoneCode": 90,
+ "smsHubId": 62,
+ "smsBowerId": 62
+ },
+ {
+ "code": "CZ",
+ "country": "Czech Republic",
+ "phoneCode": 420,
+ "smsHubId": 63,
+ "smsBowerId": 63
+ },
+ {
+ "code": "LK",
+ "country": "Sri Lanka",
+ "phoneCode": 94,
+ "smsHubId": 64,
+ "smsBowerId": 64
+ },
+ {
+ "code": "PE",
+ "country": "Peru",
+ "phoneCode": 51,
+ "smsHubId": 65,
+ "smsBowerId": 65
+ },
+ {
+ "code": "PK",
+ "country": "Pakistan",
+ "phoneCode": 92,
+ "smsHubId": 66,
+ "smsBowerId": 66
+ },
+ {
+ "code": "NZ",
+ "country": "New Zealand",
+ "phoneCode": 64,
+ "smsHubId": 67,
+ "smsBowerId": 175
+ },
+ {
+ "code": "GN",
+ "country": "Guinea",
+ "phoneCode": 224,
+ "smsHubId": 68,
+ "smsBowerId": 68
+ },
+ {
+ "code": "ML",
+ "country": "Mali",
+ "phoneCode": 223,
+ "smsHubId": 69,
+ "smsBowerId": 69
+ },
+ {
+ "code": "VE",
+ "country": "Venezuela",
+ "phoneCode": 58,
+ "smsHubId": 70,
+ "smsBowerId": 70
+ },
+ {
+ "code": "ET",
+ "country": "Ethiopia",
+ "phoneCode": 251,
+ "smsHubId": 71,
+ "smsBowerId": 71
+ },
+ {
+ "code": "MN",
+ "country": "Mongolia",
+ "phoneCode": 976,
+ "smsHubId": 72,
+ "smsBowerId": 72
+ },
+ {
+ "code": "BR",
+ "country": "Brazil",
+ "phoneCode": 55,
+ "smsHubId": 73,
+ "smsBowerId": 73
+ },
+ {
+ "code": "AF",
+ "country": "Afghanistan",
+ "phoneCode": 93,
+ "smsHubId": 74,
+ "smsBowerId": 74
+ },
+ {
+ "code": "UG",
+ "country": "Uganda",
+ "phoneCode": 256,
+ "smsHubId": 75,
+ "smsBowerId": 75
+ },
+ {
+ "code": "AO",
+ "country": "Angola",
+ "phoneCode": 244,
+ "smsHubId": 76,
+ "smsBowerId": 76
+ },
+ {
+ "code": "CY",
+ "country": "Cyprus",
+ "phoneCode": 357,
+ "smsHubId": 77,
+ "smsBowerId": 77
+ },
+ {
+ "code": "FR",
+ "country": "France",
+ "phoneCode": 33,
+ "smsHubId": 78,
+ "smsBowerId": 78
+ },
+ {
+ "code": "PG",
+ "country": "Papua New Guinea",
+ "phoneCode": 675,
+ "smsHubId": 79,
+ "smsBowerId": 79
+ },
+ {
+ "code": "MZ",
+ "country": "Mozambique",
+ "phoneCode": 258,
+ "smsHubId": 80,
+ "smsBowerId": 80
+ },
+ {
+ "code": "NP",
+ "country": "Nepal",
+ "phoneCode": 977,
+ "smsHubId": 81,
+ "smsBowerId": 81
+ },
+ {
+ "code": "BE",
+ "country": "Belgium",
+ "phoneCode": 32,
+ "smsHubId": 82,
+ "smsBowerId": 82
+ },
+ {
+ "code": "BG",
+ "country": "Bulgaria",
+ "phoneCode": 359,
+ "smsHubId": 83,
+ "smsBowerId": 83
+ },
+ {
+ "code": "HU",
+ "country": "Hungary",
+ "phoneCode": 36,
+ "smsHubId": 84,
+ "smsBowerId": 84
+ },
+ {
+ "code": "DK",
+ "country": "Denmark",
+ "phoneCode": 45,
+ "smsHubId": 85,
+ "smsBowerId": 172
+ },
+ {
+ "code": "FI",
+ "country": "Finland",
+ "phoneCode": 358,
+ "smsHubId": 86,
+ "smsBowerId": 163
+ },
+ {
+ "code": "NO",
+ "country": "Norway",
+ "phoneCode": 47,
+ "smsHubId": 87,
+ "smsBowerId": 174
+ },
+ {
+ "code": "IS",
+ "country": "Iceland",
+ "phoneCode": 354,
+ "smsHubId": 88,
+ "smsBowerId": 132
+ },
+ {
+ "code": "ZW",
+ "country": "Zimbabwe",
+ "phoneCode": 263,
+ "smsHubId": 89,
+ "smsBowerId": 89
+ },
+ {
+ "code": "PY",
+ "country": "Paraguay",
+ "phoneCode": 595,
+ "smsHubId": 90,
+ "smsBowerId": 87
+ },
+ {
+ "code": "SY",
+ "country": "Syria",
+ "phoneCode": 963,
+ "smsHubId": 91,
+ "smsBowerId": 91
+ },
+ {
+ "code": "UG",
+ "country": "Uganda",
+ "phoneCode": 256,
+ "smsHubId": 92,
+ "smsBowerId": 75
+ }
+]
\ No newline at end of file
diff --git a/src/index.js b/src/index.js
index d8a2b7f..d030265 100644
--- a/src/index.js
+++ b/src/index.js
@@ -1,4 +1,5 @@
-const { request } = require('undici')
+const { request, ProxyAgent } = require('undici')
+const countries = require('./countries.json');
const errorsList = {
BANNED: 'Account banned',
@@ -31,6 +32,9 @@ const supportedMethods = {
smsactivate: [
'getNumbersStatus', 'getBalance', 'getNumber', 'setStatus', 'getStatus', 'getPrices', 'getFullSms',
'getAdditionalService', 'getCountries', 'getQiwiRequisites', 'getCode'
+ ],
+ smsbower: [
+ 'getBalance', 'getNumber', 'setStatus', 'getStatus', 'getPrices', 'getCode', 'setMaxPrice'
]
}
@@ -90,6 +94,7 @@ class GetSMS {
* @property {(en|ru)} [lang=ru] - Lang code, smshub can returns results with russian or english (Only smshub)
* @property {(smshub|smsactivate)} service - Service name
* @property {interval} [interval=2000] - Polling interval of getCode method
+ * @property {boolean} [withoutCountryCode=false] - Return numbers without country code
*/
/**
@@ -99,11 +104,11 @@ class GetSMS {
* @param {InitProperties} - Options object
* @throws Error
*/
- constructor ({ key, url, secondUrl = 'https://smshub.org/api.php', service, lang = 'ru', interval = 2000 }) {
+ constructor ({ key, url, secondUrl = 'https://smshub.org/api.php', service, lang = 'ru', interval = 2000, withoutCountryCode = false, proxyUrl = null }) {
if (!key || !url || !service) {
throw new Error('Missing argument(s)')
}
- if (!['smshub', 'smsactivate'].includes(service)) {
+ if (!['smshub', 'smsactivate', 'smsbower'].includes(service)) {
throw new Error('Invalid service name')
}
this._key = key
@@ -112,6 +117,9 @@ class GetSMS {
this._secondUrl = secondUrl
this._service = service
this._interval = interval
+ this._withoutCountryCode = withoutCountryCode
+ this._proxyUrl = proxyUrl
+ this._maxPrice = null;
return new Proxy(this, {
get (target, prop) {
@@ -150,12 +158,39 @@ class GetSMS {
Object.assign(qs, { api_key: this._key })
).toString()
+ let dispatcher = {};
+ if (this._proxyUrl) {
+ const uri = new URL(this._proxyUrl);
+
+ const token =
+ "Basic " +
+ Buffer.from(`${uri.username}:${uri.password}`).toString("base64");
+
+ dispatcher = {
+ dispatcher : new ProxyAgent({
+ uri: uri,
+ //auth: Buffer.from(`${uri.username}:${uri.password}`).toString('base64'),
+ token: token,
+ }),
+ }
+ }
+
+ console.log(JSON.stringify({
+ method,
+ form: form ? new URLSearchParams(form).toString() : undefined,
+ headers: {
+ Cookie: 'lang=' + this._lang
+ },
+ ...dispatcher
+ }));
+
return request(url, {
method,
form: form ? new URLSearchParams(form).toString() : undefined,
headers: {
Cookie: 'lang=' + this._lang
- }
+ },
+ ...dispatcher
})
.then(data => data.body.text())
.then(data => {
@@ -237,6 +272,7 @@ class GetSMS {
* .getMultiServiceNumber(['ok','vk','vi','av'], 'mts', 0, [0, 1, 0, 0])
*/
getMultiServiceNumber (service, operator, country, forward, ref) {
+ country = this._getCountryId(country)
if (Array.isArray(service)) service = service.toString()
if (Array.isArray(forward)) forward = forward.toString()
if (Array.isArray(operator)) operator = operator.toString()
@@ -376,15 +412,22 @@ class GetSMS {
* @method
* @public
* @async
- * @param {string} service - Service code name
- * @param {string|number} maxPrice - Max buy price
+ * @param {string|number} maxPrice - Max buy price (for SmsHub in USD, no need to add country)
* @param {boolean} random - Enable random number
- * @param {string|number} country - Country ID
+ * @param {string|number|null} country - Country ID
* @returns {Promise}
* @throws Error
* @throws ServiceApiError
*/
- setMaxPrice (service, maxPrice, random = true, country) {
+ setMaxPrice (maxPrice, random = true, country = null) {
+
+ // For smshub it will be used during getNumber request
+ if ('smshub' === this._service || 'smsbower' === this._service) {
+ this._maxPrice = maxPrice;
+ return;
+ }
+ country = this._getCountryId(country)
+
return this._request({ action: 'setMaxPrice', service, maxPrice, country, random })
}
@@ -443,7 +486,7 @@ class GetSMS {
* @public
* @async
* @param {string} service - Service code name
- * @param {string} [operator] - Mobile operator code name
+ * @param {string|array} [operators] - Mobile operator code name or multiple operators in array (will be choosen random + will be changed if no numbers)
* @param {string|number} [country] - Country ID
* @param {(string|number)} [forward] - Number forward, must be 1
or 0
*
* @param {string} [phoneException] - Prefixes for excepting mobile numbers separated by comma *
@@ -452,15 +495,36 @@ class GetSMS {
* @throws Error
* @throws ServiceApiError
*/
- getNumber (service, operator, country, forward, phoneException, ref) {
- return this._request({ action: 'getNumber', service, operator, country, forward, phoneException, ref })
- .then((response) => {
- const [, id, number] = response.split(':')
- return {
- id,
- number
- }
- })
+ getNumber (service, operators, country, forward, phoneException = '', ref = '') {
+ country = this._getCountryId(country);
+ const operator = Array.isArray(operators) ? operators[Math.floor(Math.random() * operators.length)] : operators;
+
+ return this._request({ action: 'getNumber', service, operator, country, forward, phoneException, ref, ...(('smshub' === this._service || 'smsbower' === this._service) && this._maxPrice !== null && { maxPrice: this._maxPrice, currency: 840 }) })
+ .then((response) => {
+ let [, id, number] = response.split(':')
+
+ // Removing country code if needed
+ if (this._withoutCountryCode) {
+ const countryObj = countries.find((el) => {
+ return el.smsHubId === country
+ })
+ // removing country number, but only if it is in the beginning of the string
+ number = number.replace(new RegExp('^' + countryObj.phoneCode), '')
+ }
+
+ return {
+ id,
+ number
+ }
+ })
+ .catch((e) => {
+ // If operators are array, trying again but excluding failed operator
+ if ('NO_NUMBERS' === e.code && 'string' !== typeof operators && 0 !== operators.length) {
+ return this.getNumber(service, operators.filter(value => value !== operator), country, forward, phoneException, ref);
+ } else {
+ throw e;
+ }
+ })
}
/**
@@ -579,10 +643,37 @@ class GetSMS {
* @throws ServiceApiError
*/
getPrices (service, country) {
+ country = this._getCountryId(country)
return this._request({ action: 'getPrices', country, service })
}
+
+ /**
+ * Method for getting ID of the country by 2 letter code
+ * @method
+ * @private
+ * @param {string|number} [country] - Country ID or 2 letter symbol code
+ * @returns number
+ * @throws Error
+ */
+ _getCountryId(country) {
+ if (!/^-?\d+$/.test(country) && 'number' !== typeof country && (2 === country.length || 'US_nonvirt' === country)) {
+ const res = countries.find((el) => el.code === country)
+ if (res) {
+ console.log(`res`, JSON.stringify(res), this._service);
+ if ('smshub' === this._service) {
+ return res.smsHubId;
+ } else if ('smsbower' === this._service) {
+ return res.smsBowerId;
+ }
+ } else {
+ throw new Error('Country ID is not found by 2 letter symbol code')
+ }
+ }
+ return country;
+ }
}
+
module.exports = {
GetSMS,
ServiceApiError,