From ff10e4a6d477813ea88b621dcc12bd64ad9c8987 Mon Sep 17 00:00:00 2001 From: Eugene Bos Date: Wed, 21 Feb 2024 22:00:59 +0700 Subject: [PATCH 01/10] option to remove country code from numbers --- src/countries.json | 560 +++++++++++++++++++++++++++++++++++++++++++++ src/index.js | 17 +- 2 files changed, 575 insertions(+), 2 deletions(-) create mode 100644 src/countries.json diff --git a/src/countries.json b/src/countries.json new file mode 100644 index 0000000..6cd4d0b --- /dev/null +++ b/src/countries.json @@ -0,0 +1,560 @@ +[ + { + "code": "RU", + "country": "Russia", + "phoneCode": 7, + "smsHubId": 0 + }, + { + "code": "UA", + "country": "Ukraine", + "phoneCode": 380, + "smsHubId": 1 + }, + { + "code": "KZ", + "country": "Kazakhstan", + "phoneCode": 7, + "smsHubId": 2 + }, + { + "code": "CN", + "country": "China", + "phoneCode": 86, + "smsHubId": 3 + }, + { + "code": "PH", + "country": "Philippines", + "phoneCode": 63, + "smsHubId": 4 + }, + { + "code": "MM", + "country": "Myanmar", + "phoneCode": 95, + "smsHubId": 5 + }, + { + "code": "ID", + "country": "Indonesia", + "phoneCode": 62, + "smsHubId": 6 + }, + { + "code": "MY", + "country": "Malaysia", + "phoneCode": 60, + "smsHubId": 7 + }, + { + "code": "KE", + "country": "Kenya", + "phoneCode": 254, + "smsHubId": 8 + }, + { + "code": "TZ", + "country": "Tanzania", + "phoneCode": 255, + "smsHubId": 9 + }, + { + "code": "VN", + "country": "Vietnam", + "phoneCode": 84, + "smsHubId": 10 + }, + { + "code": "KG", + "country": "Kyrgyzstan", + "phoneCode": 996, + "smsHubId": 11 + }, + { + "code": "US", + "country": "USA (Virtual)", + "phoneCode": 1, + "smsHubId": 12 + }, + { + "code": "IL", + "country": "Israel", + "phoneCode": 972, + "smsHubId": 13 + }, + { + "code": "HK", + "country": "Hong Kong", + "phoneCode": 852, + "smsHubId": 14 + }, + { + "code": "PL", + "country": "Poland", + "phoneCode": 48, + "smsHubId": 15 + }, + { + "code": "GB", + "country": "United Kingdom", + "phoneCode": 44, + "smsHubId": 16 + }, + { + "code": "MG", + "country": "Madagascar", + "phoneCode": 261, + "smsHubId": 17 + }, + { + "code": "CD", + "country": "Democratic Republic of the Congo", + "phoneCode": 243, + "smsHubId": 18 + }, + { + "code": "NG", + "country": "Nigeria", + "phoneCode": 234, + "smsHubId": 19 + }, + { + "code": "MO", + "country": "Macao", + "phoneCode": 853, + "smsHubId": 20 + }, + { + "code": "EG", + "country": "Egypt", + "phoneCode": 20, + "smsHubId": 21 + }, + { + "code": "IN", + "country": "India", + "phoneCode": 91, + "smsHubId": 22 + }, + { + "code": "IE", + "country": "Ireland", + "phoneCode": 353, + "smsHubId": 23 + }, + { + "code": "KH", + "country": "Cambodia", + "phoneCode": 855, + "smsHubId": 24 + }, + { + "code": "LA", + "country": "Laos", + "phoneCode": 856, + "smsHubId": 25 + }, + { + "code": "HT", + "country": "Haiti", + "phoneCode": 509, + "smsHubId": 26 + }, + { + "code": "CI", + "country": "Côte d'Ivoire", + "phoneCode": 225, + "smsHubId": 27 + }, + { + "code": "GM", + "country": "Gambia", + "phoneCode": 220, + "smsHubId": 28 + }, + { + "code": "RS", + "country": "Serbia", + "phoneCode": 381, + "smsHubId": 29 + }, + { + "code": "YE", + "country": "Yemen", + "phoneCode": 967, + "smsHubId": 30 + }, + { + "code": "ZA", + "country": "South Africa", + "phoneCode": 27, + "smsHubId": 31 + }, + { + "code": "RO", + "country": "Romania", + "phoneCode": 40, + "smsHubId": 32 + }, + { + "code": "CO", + "country": "Colombia", + "phoneCode": 57, + "smsHubId": 33 + }, + { + "code": "EE", + "country": "Estonia", + "phoneCode": 372, + "smsHubId": 34 + }, + { + "code": "AZ", + "country": "Azerbaijan", + "phoneCode": 994, + "smsHubId": 35 + }, + { + "code": "CA", + "country": "Canada", + "phoneCode": 1, + "smsHubId": 36 + }, + { + "code": "MA", + "country": "Morocco", + "phoneCode": 212, + "smsHubId": 37 + }, + { + "code": "GH", + "country": "Ghana", + "phoneCode": 233, + "smsHubId": 38 + }, + { + "code": "AR", + "country": "Argentina", + "phoneCode": 54, + "smsHubId": 39 + }, + { + "code": "UZ", + "country": "Uzbekistan", + "phoneCode": 998, + "smsHubId": 40 + }, + { + "code": "CM", + "country": "Cameroon", + "phoneCode": 237, + "smsHubId": 41 + }, + { + "code": "TD", + "country": "Chad", + "phoneCode": 235, + "smsHubId": 42 + }, + { + "code": "DE", + "country": "Germany", + "phoneCode": 49, + "smsHubId": 43 + }, + { + "code": "LT", + "country": "Lithuania", + "phoneCode": 370, + "smsHubId": 44 + }, + { + "code": "HR", + "country": "Croatia", + "phoneCode": 385, + "smsHubId": 45 + }, + { + "code": "SE", + "country": "Sweden", + "phoneCode": 46, + "smsHubId": 46 + }, + { + "code": "IQ", + "country": "Iraq", + "phoneCode": 964, + "smsHubId": 47 + }, + { + "code": "NL", + "country": "Netherlands", + "phoneCode": 31, + "smsHubId": 48 + }, + { + "code": "LV", + "country": "Latvia", + "phoneCode": 371, + "smsHubId": 49 + }, + { + "code": "AT", + "country": "Austria", + "phoneCode": 43, + "smsHubId": 50 + }, + { + "code": "BY", + "country": "Belarus", + "phoneCode": 375, + "smsHubId": 51 + }, + { + "code": "TH", + "country": "Thailand", + "phoneCode": 66, + "smsHubId": 52 + }, + { + "code": "SA", + "country": "Saudi Arabia", + "phoneCode": 966, + "smsHubId": 53 + }, + { + "code": "MX", + "country": "Mexico", + "phoneCode": 52, + "smsHubId": 54 + }, + { + "code": "TW", + "country": "Taiwan", + "phoneCode": 886, + "smsHubId": 55 + }, + { + "code": "ES", + "country": "Spain", + "phoneCode": 34, + "smsHubId": 56 + }, + { + "code": "IR", + "country": "Iran", + "phoneCode": 98, + "smsHubId": 57 + }, + { + "code": "DZ", + "country": "Algeria", + "phoneCode": 213, + "smsHubId": 58 + }, + { + "code": "SI", + "country": "Slovenia", + "phoneCode": 386, + "smsHubId": 59 + }, + { + "code": "BD", + "country": "Bangladesh", + "phoneCode": 880, + "smsHubId": 60 + }, + { + "code": "SN", + "country": "Senegal", + "phoneCode": 221, + "smsHubId": 61 + }, + { + "code": "TR", + "country": "Turkey", + "phoneCode": 90, + "smsHubId": 62 + }, + { + "code": "CZ", + "country": "Czech Republic", + "phoneCode": 420, + "smsHubId": 63 + }, + { + "code": "LK", + "country": "Sri Lanka", + "phoneCode": 94, + "smsHubId": 64 + }, + { + "code": "PE", + "country": "Peru", + "phoneCode": 51, + "smsHubId": 65 + }, + { + "code": "PK", + "country": "Pakistan", + "phoneCode": 92, + "smsHubId": 66 + }, + { + "code": "NZ", + "country": "New Zealand", + "phoneCode": 64, + "smsHubId": 67 + }, + { + "code": "GN", + "country": "Guinea", + "phoneCode": 224, + "smsHubId": 68 + }, + { + "code": "ML", + "country": "Mali", + "phoneCode": 223, + "smsHubId": 69 + }, + { + "code": "VE", + "country": "Venezuela", + "phoneCode": 58, + "smsHubId": 70 + }, + { + "code": "ET", + "country": "Ethiopia", + "phoneCode": 251, + "smsHubId": 71 + }, + { + "code": "MN", + "country": "Mongolia", + "phoneCode": 976, + "smsHubId": 72 + }, + { + "code": "BR", + "country": "Brazil", + "phoneCode": 55, + "smsHubId": 73 + }, + { + "code": "AF", + "country": "Afghanistan", + "phoneCode": 93, + "smsHubId": 74 + }, + { + "code": "UG", + "country": "Uganda", + "phoneCode": 256, + "smsHubId": 75 + }, + { + "code": "AO", + "country": "Angola", + "phoneCode": 244, + "smsHubId": 76 + }, + { + "code": "CY", + "country": "Cyprus", + "phoneCode": 357, + "smsHubId": 77 + }, + { + "code": "FR", + "country": "France", + "phoneCode": 33, + "smsHubId": 78 + }, + { + "code": "PG", + "country": "Papua New Guinea", + "phoneCode": 675, + "smsHubId": 79 + }, + { + "code": "MZ", + "country": "Mozambique", + "phoneCode": 258, + "smsHubId": 80 + }, + { + "code": "NP", + "country": "Nepal", + "phoneCode": 977, + "smsHubId": 81 + }, + { + "code": "BE", + "country": "Belgium", + "phoneCode": 32, + "smsHubId": 82 + }, + { + "code": "BG", + "country": "Bulgaria", + "phoneCode": 359, + "smsHubId": 83 + }, + { + "code": "HU", + "country": "Hungary", + "phoneCode": 36, + "smsHubId": 84 + }, + { + "code": "DK", + "country": "Denmark", + "phoneCode": 45, + "smsHubId": 85 + }, + { + "code": "FI", + "country": "Finland", + "phoneCode": 358, + "smsHubId": 86 + }, + { + "code": "NO", + "country": "Norway", + "phoneCode": 47, + "smsHubId": 87 + }, + { + "code": "IS", + "country": "Iceland", + "phoneCode": 354, + "smsHubId": 88 + }, + { + "code": "ZW", + "country": "Zimbabwe", + "phoneCode": 263, + "smsHubId": 89 + }, + { + "code": "PY", + "country": "Paraguay", + "phoneCode": 595, + "smsHubId": 90 + }, + { + "code": "SY", + "country": "Syria", + "phoneCode": 963, + "smsHubId": 91 + }, + { + "code": "UG", + "country": "Uganda", + "phoneCode": 256, + "smsHubId": 92 + } +] diff --git a/src/index.js b/src/index.js index bf01864..63b4705 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,5 @@ const { request } = require('undici') +const countries = require('./countries.json'); const errorsList = { BANNED: 'Account banned', @@ -89,6 +90,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 */ /** @@ -98,7 +100,7 @@ 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 }) { if (!key || !url || !service) { throw new Error('Missing argument(s)') } @@ -111,6 +113,7 @@ class GetSMS { this._secondUrl = secondUrl this._service = service this._interval = interval + this._withoutCountryCode = withoutCountryCode return new Proxy(this, { get (target, prop) { @@ -454,7 +457,17 @@ class GetSMS { 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(':') + 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 From e4680e57ae66724903a7177ba7caeb9640e9be36 Mon Sep 17 00:00:00 2001 From: Eugene Bos Date: Thu, 22 Feb 2024 16:56:49 +0700 Subject: [PATCH 02/10] select country using 2 letter symbol code --- src/index.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/index.js b/src/index.js index 63b4705..667ccd9 100644 --- a/src/index.js +++ b/src/index.js @@ -455,6 +455,17 @@ class GetSMS { * @throws ServiceApiError */ getNumber (service, operator, country, forward, phoneException, ref) { + // if country represented by 2 letters, converting it to country ID before making a request + if (!/^-?\d+$/.test(country) && 2 === country.length) { + console.log('String to number') + const res = countries.find((el) => el.code === country) + if (res) { + country = res.smsHubId + } else { + throw new Error('Country ID is not found by 2 letter symbol code') + } + } + return this._request({ action: 'getNumber', service, operator, country, forward, phoneException, ref }) .then((response) => { let [, id, number] = response.split(':') From b0d4f54f700efd381ccb139a9b086e307c98eea3 Mon Sep 17 00:00:00 2001 From: Eugene Bos Date: Thu, 22 Feb 2024 17:05:23 +0700 Subject: [PATCH 03/10] country code support for all methods --- src/index.js | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/index.js b/src/index.js index 667ccd9..eb2f5b2 100644 --- a/src/index.js +++ b/src/index.js @@ -239,6 +239,7 @@ class GetSMS { * .getMultiServiceNumber(['ok','vk','vi','av'], 'mts', 0, [0, 1, 0, 0]) */ getMultiServiceNumber (service, operator, country, forward, ref) { + country = getCountryId(country) if (Array.isArray(service)) service = service.toString() if (Array.isArray(forward)) forward = forward.toString() if (Array.isArray(operator)) operator = operator.toString() @@ -387,6 +388,7 @@ class GetSMS { * @throws ServiceApiError */ setMaxPrice (service, maxPrice, random = true, country) { + country = getCountryId(country) return this._request({ action: 'setMaxPrice', service, maxPrice, country, random }) } @@ -455,16 +457,7 @@ class GetSMS { * @throws ServiceApiError */ getNumber (service, operator, country, forward, phoneException, ref) { - // if country represented by 2 letters, converting it to country ID before making a request - if (!/^-?\d+$/.test(country) && 2 === country.length) { - console.log('String to number') - const res = countries.find((el) => el.code === country) - if (res) { - country = res.smsHubId - } else { - throw new Error('Country ID is not found by 2 letter symbol code') - } - } + country = getCountryId(country) return this._request({ action: 'getNumber', service, operator, country, forward, phoneException, ref }) .then((response) => { @@ -589,10 +582,30 @@ class GetSMS { * @throws ServiceApiError */ getPrices (service, country) { + country = 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 + */ +function getCountryId(country) { + if (!/^-?\d+$/.test(country) && 2 === country.length) { + const res = countries.find((el) => el.code === country) + if (res) { + return res.smsHubId + } else { + throw new Error('Country ID is not found by 2 letter symbol code') + } + } +} + module.exports = { GetSMS, ServiceApiError, From b522ab8e1ac4b30eec83a70dd23dfc00b49851ce Mon Sep 17 00:00:00 2001 From: Eugene Bos Date: Tue, 18 Jun 2024 01:12:11 +0500 Subject: [PATCH 04/10] proxyUrl param support (& required update of undici to make it work) --- package-lock.json | 35 +++++++++++++++++++++++++++-------- package.json | 6 +++--- src/index.js | 34 +++++++++++++++++++++++++++++++--- 3 files changed, 61 insertions(+), 14 deletions(-) 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/index.js b/src/index.js index 3a4929f..24bd77a 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,4 @@ -const { request } = require('undici') +const { request, ProxyAgent } = require('undici') const countries = require('./countries.json'); const errorsList = { @@ -101,7 +101,7 @@ class GetSMS { * @param {InitProperties} - Options object * @throws Error */ - constructor ({ key, url, secondUrl = 'https://smshub.org/api.php', service, lang = 'ru', interval = 2000, withoutCountryCode = false }) { + 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)') } @@ -115,6 +115,7 @@ class GetSMS { this._service = service this._interval = interval this._withoutCountryCode = withoutCountryCode + this._proxyUrl = proxyUrl return new Proxy(this, { get (target, prop) { @@ -153,12 +154,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 => { From d1a7b00cfc5c1916896399f4bbe0fa50b3a8e269 Mon Sep 17 00:00:00 2001 From: Eugene Bos Date: Thu, 27 Jun 2024 19:53:13 +0500 Subject: [PATCH 05/10] maxPrice support for smshub --- src/index.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index 24bd77a..af20633 100644 --- a/src/index.js +++ b/src/index.js @@ -116,6 +116,7 @@ class GetSMS { this._interval = interval this._withoutCountryCode = withoutCountryCode this._proxyUrl = proxyUrl + this._maxPrice = null; return new Proxy(this, { get (target, prop) { @@ -408,16 +409,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) { + this._maxPrice = maxPrice; + return; + } country = getCountryId(country) + return this._request({ action: 'setMaxPrice', service, maxPrice, country, random }) } From 4f6466ac87b13aa971d3a03db367e83f164bfc8c Mon Sep 17 00:00:00 2001 From: Eugene Bos Date: Thu, 27 Jun 2024 19:53:40 +0500 Subject: [PATCH 06/10] getting country by country code bug fix --- src/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index af20633..c859c14 100644 --- a/src/index.js +++ b/src/index.js @@ -645,7 +645,7 @@ class GetSMS { * @throws Error */ function getCountryId(country) { - if (!/^-?\d+$/.test(country) && 2 === country.length) { + if (!/^-?\d+$/.test(country) && 'number' !== typeof country && 2 === country.length) { const res = countries.find((el) => el.code === country) if (res) { return res.smsHubId @@ -653,6 +653,7 @@ function getCountryId(country) { throw new Error('Country ID is not found by 2 letter symbol code') } } + return country; } module.exports = { From 41b3ac09b6192bb6099505a21353e21d185c2d90 Mon Sep 17 00:00:00 2001 From: Eugene Bos Date: Thu, 27 Jun 2024 19:54:27 +0500 Subject: [PATCH 07/10] multiple operators support with changing it automatically on fail (NO_NUMBERS) --- src/index.js | 49 +++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/src/index.js b/src/index.js index c859c14..a647ede 100644 --- a/src/index.js +++ b/src/index.js @@ -483,7 +483,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 * @@ -492,27 +492,36 @@ class GetSMS { * @throws Error * @throws ServiceApiError */ - getNumber (service, operator, country, forward, phoneException, ref) { + getNumber (service, operators, country, forward, phoneException, ref) { country = getCountryId(country) - - return this._request({ action: 'getNumber', service, operator, country, forward, phoneException, ref }) - .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), '') - } + 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 && 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 - } - }) + 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; + } + }) } /** From b99f86aeade2c51f6bfe6e01104af6a67c39ad39 Mon Sep 17 00:00:00 2001 From: Eugene Bos Date: Tue, 17 Sep 2024 01:48:22 +0500 Subject: [PATCH 08/10] smsbower support --- src/index.js | 61 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/src/index.js b/src/index.js index a647ede..633beed 100644 --- a/src/index.js +++ b/src/index.js @@ -32,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' ] } @@ -105,7 +108,7 @@ class GetSMS { 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 @@ -269,7 +272,7 @@ class GetSMS { * .getMultiServiceNumber(['ok','vk','vi','av'], 'mts', 0, [0, 1, 0, 0]) */ getMultiServiceNumber (service, operator, country, forward, ref) { - country = getCountryId(country) + 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() @@ -419,11 +422,11 @@ class GetSMS { setMaxPrice (maxPrice, random = true, country = null) { // For smshub it will be used during getNumber request - if ('smshub' === this._service) { + if ('smshub' === this._service || 'smsbower' === this._service) { this._maxPrice = maxPrice; return; } - country = getCountryId(country) + country = this._getCountryId(country) return this._request({ action: 'setMaxPrice', service, maxPrice, country, random }) } @@ -492,11 +495,11 @@ class GetSMS { * @throws Error * @throws ServiceApiError */ - getNumber (service, operators, country, forward, phoneException, ref) { - country = getCountryId(country) + 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 && this._maxPrice !== null && { maxPrice: this._maxPrice, currency: 840 }) }) + + 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(':') @@ -640,31 +643,37 @@ class GetSMS { * @throws ServiceApiError */ getPrices (service, country) { - country = getCountryId(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 - */ -function getCountryId(country) { - if (!/^-?\d+$/.test(country) && 'number' !== typeof country && 2 === country.length) { - const res = countries.find((el) => el.code === country) - if (res) { - return res.smsHubId - } else { - throw new Error('Country ID is not found by 2 letter symbol code') + /** + * 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) { + 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; } - return country; } + module.exports = { GetSMS, ServiceApiError, From a90f6a47e3254fd58bd7e3077d4fb11d7a28d0a5 Mon Sep 17 00:00:00 2001 From: Eugene Bos Date: Thu, 19 Sep 2024 22:07:49 +0500 Subject: [PATCH 09/10] smsbower support --- src/countries.json | 288 ++++++++++++++++++++++++++++++--------------- 1 file changed, 194 insertions(+), 94 deletions(-) diff --git a/src/countries.json b/src/countries.json index 6cd4d0b..8d15cfa 100644 --- a/src/countries.json +++ b/src/countries.json @@ -3,558 +3,658 @@ "code": "RU", "country": "Russia", "phoneCode": 7, - "smsHubId": 0 + "smsHubId": 0, + "smsBowerId": 0 }, { "code": "UA", "country": "Ukraine", "phoneCode": 380, - "smsHubId": 1 + "smsHubId": 1, + "smsBowerId": 1 }, { "code": "KZ", "country": "Kazakhstan", "phoneCode": 7, - "smsHubId": 2 + "smsHubId": 2, + "smsBowerId": 2 }, { "code": "CN", "country": "China", "phoneCode": 86, - "smsHubId": 3 + "smsHubId": 3, + "smsBowerId": 3 }, { "code": "PH", "country": "Philippines", "phoneCode": 63, - "smsHubId": 4 + "smsHubId": 4, + "smsBowerId": 4 }, { "code": "MM", "country": "Myanmar", "phoneCode": 95, - "smsHubId": 5 + "smsHubId": 5, + "smsBowerId": 5 }, { "code": "ID", "country": "Indonesia", "phoneCode": 62, - "smsHubId": 6 + "smsHubId": 6, + "smsBowerId": 6 }, { "code": "MY", "country": "Malaysia", "phoneCode": 60, - "smsHubId": 7 + "smsHubId": 7, + "smsBowerId": 7 }, { "code": "KE", "country": "Kenya", "phoneCode": 254, - "smsHubId": 8 + "smsHubId": 8, + "smsBowerId": 8 }, { "code": "TZ", "country": "Tanzania", "phoneCode": 255, - "smsHubId": 9 + "smsHubId": 9, + "smsBowerId": 9 }, { "code": "VN", "country": "Vietnam", "phoneCode": 84, - "smsHubId": 10 + "smsHubId": 10, + "smsBowerId": 10 }, { "code": "KG", "country": "Kyrgyzstan", "phoneCode": 996, - "smsHubId": 11 + "smsHubId": 11, + "smsBowerId": 11 }, { "code": "US", "country": "USA (Virtual)", "phoneCode": 1, - "smsHubId": 12 + "smsHubId": 12, + "smsBowerId": 12 + }, + { + "code": "US_nonvirt", + "country": "USA", + "phoneCode": 1, + "smsHubId": 187, + "smsBowerId": 187 }, { "code": "IL", "country": "Israel", "phoneCode": 972, - "smsHubId": 13 + "smsHubId": 13, + "smsBowerId": 13 }, { "code": "HK", "country": "Hong Kong", "phoneCode": 852, - "smsHubId": 14 + "smsHubId": 14, + "smsBowerId": 14 }, { "code": "PL", "country": "Poland", "phoneCode": 48, - "smsHubId": 15 + "smsHubId": 15, + "smsBowerId": 15 }, { "code": "GB", "country": "United Kingdom", "phoneCode": 44, - "smsHubId": 16 + "smsHubId": 16, + "smsBowerId": 16 }, { "code": "MG", "country": "Madagascar", "phoneCode": 261, - "smsHubId": 17 + "smsHubId": 17, + "smsBowerId": 17 }, { "code": "CD", "country": "Democratic Republic of the Congo", "phoneCode": 243, - "smsHubId": 18 + "smsHubId": 18, + "smsBowerId": 18 }, { "code": "NG", "country": "Nigeria", "phoneCode": 234, - "smsHubId": 19 + "smsHubId": 19, + "smsBowerId": 19 }, { "code": "MO", "country": "Macao", "phoneCode": 853, - "smsHubId": 20 + "smsHubId": 20, + "smsBowerId": 20 }, { "code": "EG", "country": "Egypt", "phoneCode": 20, - "smsHubId": 21 + "smsHubId": 21, + "smsBowerId": 21 }, { "code": "IN", "country": "India", "phoneCode": 91, - "smsHubId": 22 + "smsHubId": 22, + "smsBowerId": 22 }, { "code": "IE", "country": "Ireland", "phoneCode": 353, - "smsHubId": 23 + "smsHubId": 23, + "smsBowerId": 23 }, { "code": "KH", "country": "Cambodia", "phoneCode": 855, - "smsHubId": 24 + "smsHubId": 24, + "smsBowerId": 24 }, { "code": "LA", "country": "Laos", "phoneCode": 856, - "smsHubId": 25 + "smsHubId": 25, + "smsBowerId": 25 }, { "code": "HT", "country": "Haiti", "phoneCode": 509, - "smsHubId": 26 + "smsHubId": 26, + "smsBowerId": 26 }, { "code": "CI", "country": "Côte d'Ivoire", "phoneCode": 225, - "smsHubId": 27 + "smsHubId": 27, + "smsBowerId": 27 }, { "code": "GM", "country": "Gambia", "phoneCode": 220, - "smsHubId": 28 + "smsHubId": 28, + "smsBowerId": 28 }, { "code": "RS", "country": "Serbia", "phoneCode": 381, - "smsHubId": 29 + "smsHubId": 29, + "smsBowerId": 29 }, { "code": "YE", "country": "Yemen", "phoneCode": 967, - "smsHubId": 30 + "smsHubId": 30, + "smsBowerId": 30 }, { "code": "ZA", "country": "South Africa", "phoneCode": 27, - "smsHubId": 31 + "smsHubId": 31, + "smsBowerId": 31 }, { "code": "RO", "country": "Romania", "phoneCode": 40, - "smsHubId": 32 + "smsHubId": 32, + "smsBowerId": 32 }, { "code": "CO", "country": "Colombia", "phoneCode": 57, - "smsHubId": 33 + "smsHubId": 33, + "smsBowerId": 33 }, { "code": "EE", "country": "Estonia", "phoneCode": 372, - "smsHubId": 34 + "smsHubId": 34, + "smsBowerId": 34 }, { "code": "AZ", "country": "Azerbaijan", "phoneCode": 994, - "smsHubId": 35 + "smsHubId": 35, + "smsBowerId": 35 }, { "code": "CA", "country": "Canada", "phoneCode": 1, - "smsHubId": 36 + "smsHubId": 36, + "smsBowerId": 36 }, { "code": "MA", "country": "Morocco", "phoneCode": 212, - "smsHubId": 37 + "smsHubId": 37, + "smsBowerId": 37 }, { "code": "GH", "country": "Ghana", "phoneCode": 233, - "smsHubId": 38 + "smsHubId": 38, + "smsBowerId": 38 }, { "code": "AR", "country": "Argentina", "phoneCode": 54, - "smsHubId": 39 + "smsHubId": 39, + "smsBowerId": 39 }, { "code": "UZ", "country": "Uzbekistan", "phoneCode": 998, - "smsHubId": 40 + "smsHubId": 40, + "smsBowerId": 40 }, { "code": "CM", "country": "Cameroon", "phoneCode": 237, - "smsHubId": 41 + "smsHubId": 41, + "smsBowerId": 41 }, { "code": "TD", "country": "Chad", "phoneCode": 235, - "smsHubId": 42 + "smsHubId": 42, + "smsBowerId": 42 }, { "code": "DE", "country": "Germany", "phoneCode": 49, - "smsHubId": 43 + "smsHubId": 43, + "smsBowerId": 43 }, { "code": "LT", "country": "Lithuania", "phoneCode": 370, - "smsHubId": 44 + "smsHubId": 44, + "smsBowerId": 44 }, { "code": "HR", "country": "Croatia", "phoneCode": 385, - "smsHubId": 45 + "smsHubId": 45, + "smsBowerId": 45 }, { "code": "SE", "country": "Sweden", "phoneCode": 46, - "smsHubId": 46 + "smsHubId": 46, + "smsBowerId": 174 }, { "code": "IQ", "country": "Iraq", "phoneCode": 964, - "smsHubId": 47 + "smsHubId": 47, + "smsBowerId": 47 }, { "code": "NL", "country": "Netherlands", "phoneCode": 31, - "smsHubId": 48 + "smsHubId": 48, + "smsBowerId": 48 }, { "code": "LV", "country": "Latvia", "phoneCode": 371, - "smsHubId": 49 + "smsHubId": 49, + "smsBowerId": 49 }, { "code": "AT", "country": "Austria", "phoneCode": 43, - "smsHubId": 50 + "smsHubId": 50, + "smsBowerId": 50 }, { "code": "BY", "country": "Belarus", "phoneCode": 375, - "smsHubId": 51 + "smsHubId": 51, + "smsBowerId": 51 }, { "code": "TH", "country": "Thailand", "phoneCode": 66, - "smsHubId": 52 + "smsHubId": 52, + "smsBowerId": 52 }, { "code": "SA", "country": "Saudi Arabia", "phoneCode": 966, - "smsHubId": 53 + "smsHubId": 53, + "smsBowerId": 53 }, { "code": "MX", "country": "Mexico", "phoneCode": 52, - "smsHubId": 54 + "smsHubId": 54, + "smsBowerId": 54 }, { "code": "TW", "country": "Taiwan", "phoneCode": 886, - "smsHubId": 55 + "smsHubId": 55, + "smsBowerId": 55 }, { "code": "ES", "country": "Spain", "phoneCode": 34, - "smsHubId": 56 + "smsHubId": 56, + "smsBowerId": 56 }, { "code": "IR", "country": "Iran", "phoneCode": 98, - "smsHubId": 57 + "smsHubId": 57, + "smsBowerId": 57 }, { "code": "DZ", "country": "Algeria", "phoneCode": 213, - "smsHubId": 58 + "smsHubId": 58, + "smsBowerId": 58 }, { "code": "SI", "country": "Slovenia", "phoneCode": 386, - "smsHubId": 59 + "smsHubId": 59, + "smsBowerId": 59 }, { "code": "BD", "country": "Bangladesh", "phoneCode": 880, - "smsHubId": 60 + "smsHubId": 60, + "smsBowerId": 60 }, { "code": "SN", "country": "Senegal", "phoneCode": 221, - "smsHubId": 61 + "smsHubId": 61, + "smsBowerId": 61 }, { "code": "TR", "country": "Turkey", "phoneCode": 90, - "smsHubId": 62 + "smsHubId": 62, + "smsBowerId": 62 }, { "code": "CZ", "country": "Czech Republic", "phoneCode": 420, - "smsHubId": 63 + "smsHubId": 63, + "smsBowerId": 63 }, { "code": "LK", "country": "Sri Lanka", "phoneCode": 94, - "smsHubId": 64 + "smsHubId": 64, + "smsBowerId": 64 }, { "code": "PE", "country": "Peru", "phoneCode": 51, - "smsHubId": 65 + "smsHubId": 65, + "smsBowerId": 65 }, { "code": "PK", "country": "Pakistan", "phoneCode": 92, - "smsHubId": 66 + "smsHubId": 66, + "smsBowerId": 66 }, { "code": "NZ", "country": "New Zealand", "phoneCode": 64, - "smsHubId": 67 + "smsHubId": 67, + "smsBowerId": 175 }, { "code": "GN", "country": "Guinea", "phoneCode": 224, - "smsHubId": 68 + "smsHubId": 68, + "smsBowerId": 68 }, { "code": "ML", "country": "Mali", "phoneCode": 223, - "smsHubId": 69 + "smsHubId": 69, + "smsBowerId": 69 }, { "code": "VE", "country": "Venezuela", "phoneCode": 58, - "smsHubId": 70 + "smsHubId": 70, + "smsBowerId": 70 }, { "code": "ET", "country": "Ethiopia", "phoneCode": 251, - "smsHubId": 71 + "smsHubId": 71, + "smsBowerId": 71 }, { "code": "MN", "country": "Mongolia", "phoneCode": 976, - "smsHubId": 72 + "smsHubId": 72, + "smsBowerId": 72 }, { "code": "BR", "country": "Brazil", "phoneCode": 55, - "smsHubId": 73 + "smsHubId": 73, + "smsBowerId": 73 }, { "code": "AF", "country": "Afghanistan", "phoneCode": 93, - "smsHubId": 74 + "smsHubId": 74, + "smsBowerId": 74 }, { "code": "UG", "country": "Uganda", "phoneCode": 256, - "smsHubId": 75 + "smsHubId": 75, + "smsBowerId": 75 }, { "code": "AO", "country": "Angola", "phoneCode": 244, - "smsHubId": 76 + "smsHubId": 76, + "smsBowerId": 76 }, { "code": "CY", "country": "Cyprus", "phoneCode": 357, - "smsHubId": 77 + "smsHubId": 77, + "smsBowerId": 77 }, { "code": "FR", "country": "France", "phoneCode": 33, - "smsHubId": 78 + "smsHubId": 78, + "smsBowerId": 78 }, { "code": "PG", "country": "Papua New Guinea", "phoneCode": 675, - "smsHubId": 79 + "smsHubId": 79, + "smsBowerId": 79 }, { "code": "MZ", "country": "Mozambique", "phoneCode": 258, - "smsHubId": 80 + "smsHubId": 80, + "smsBowerId": 80 }, { "code": "NP", "country": "Nepal", "phoneCode": 977, - "smsHubId": 81 + "smsHubId": 81, + "smsBowerId": 81 }, { "code": "BE", "country": "Belgium", "phoneCode": 32, - "smsHubId": 82 + "smsHubId": 82, + "smsBowerId": 82 }, { "code": "BG", "country": "Bulgaria", "phoneCode": 359, - "smsHubId": 83 + "smsHubId": 83, + "smsBowerId": 83 }, { "code": "HU", "country": "Hungary", "phoneCode": 36, - "smsHubId": 84 + "smsHubId": 84, + "smsBowerId": 84 }, { "code": "DK", "country": "Denmark", "phoneCode": 45, - "smsHubId": 85 + "smsHubId": 85, + "smsBowerId": 172 }, { "code": "FI", "country": "Finland", "phoneCode": 358, - "smsHubId": 86 + "smsHubId": 86, + "smsBowerId": 163 }, { "code": "NO", "country": "Norway", "phoneCode": 47, - "smsHubId": 87 + "smsHubId": 87, + "smsBowerId": 174 }, { "code": "IS", "country": "Iceland", "phoneCode": 354, - "smsHubId": 88 + "smsHubId": 88, + "smsBowerId": 132 }, { "code": "ZW", "country": "Zimbabwe", "phoneCode": 263, - "smsHubId": 89 + "smsHubId": 89, + "smsBowerId": 89 }, { "code": "PY", "country": "Paraguay", "phoneCode": 595, - "smsHubId": 90 + "smsHubId": 90, + "smsBowerId": 87 }, { "code": "SY", "country": "Syria", "phoneCode": 963, - "smsHubId": 91 + "smsHubId": 91, + "smsBowerId": 91 }, { "code": "UG", "country": "Uganda", "phoneCode": 256, - "smsHubId": 92 + "smsHubId": 92, + "smsBowerId": 75 } -] +] \ No newline at end of file From 35e1803ad4e5855d55b5b5b2128bf63ece17b7cb Mon Sep 17 00:00:00 2001 From: Eugene Bos Date: Sat, 5 Oct 2024 01:57:32 +0500 Subject: [PATCH 10/10] US nonvirt bug support (bug fix) --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 633beed..d030265 100644 --- a/src/index.js +++ b/src/index.js @@ -656,7 +656,7 @@ class GetSMS { * @throws Error */ _getCountryId(country) { - if (!/^-?\d+$/.test(country) && 'number' !== typeof country && 2 === country.length) { + 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);