Skip to content

Commit b17e7ad

Browse files
Add Audio method
1 parent a7086f7 commit b17e7ad

File tree

5 files changed

+106
-1
lines changed

5 files changed

+106
-1
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Examples of API requests for different captcha types are available on the [JavaS
4343
- [Cutcaptcha](#cutcaptcha)
4444
- [Tencent](#tencent)
4545
- [atbCAPTCHA](#atbcaptcha)
46+
- [Audio Captcha](#audio-captcha)
4647
- [Other methods](#other-methods)
4748
- [goodReport](#goodreport)
4849
- [badReport](#badreport)
@@ -646,6 +647,25 @@ console.log(err);
646647
})
647648
```
648649

650+
### Audio Captcha
651+
652+
<sup>[API method description.](https://2captcha.com/2captcha-api#audio-recognition)</sup>
653+
654+
Use the following method to bypass an audio captcha (`mp3` formats only). You must provide the language as `lang = 'en'`. Supported languages are "en", "ru", "de", "el", "pt", "fr".
655+
656+
```js
657+
solver.audio({
658+
body: "SUQzBAAAAAAAHFRTU0UAAAA...",
659+
lang: "en"
660+
})
661+
.then((res) => {
662+
console.log(res);
663+
})
664+
.catch((err) => {
665+
console.log(err);
666+
})
667+
```
668+
649669
## Other methods
650670

651671
### goodReport

examples/audio.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const TwoCaptcha = require("../dist/index.js");
2+
require('dotenv').config();
3+
const APIKEY = process.env.APIKEY
4+
const solver = new TwoCaptcha.Solver(APIKEY);
5+
const fs = require('fs')
6+
const audioCaptchaBase64 = fs.readFileSync("./media/example.mp3", "base64")
7+
8+
solver.audio({
9+
body: audioCaptchaBase64,
10+
lang: 'en'
11+
})
12+
.then((res) => {
13+
console.log(res);
14+
})
15+
.catch((err) => {
16+
console.log(err);
17+
})

examples/media/example.mp3

33.5 KB
Binary file not shown.

src/structs/2captcha.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,12 @@ export interface paramsAtbCaptcha{
291291
proxytype?: string
292292
}
293293

294+
export interface paramsAudioCaptcha {
295+
body: string,
296+
lang: string,
297+
pingback?: string,
298+
}
299+
294300
/**
295301
* An object containing properties of the captcha solution.
296302
* @typedef {Object} CaptchaAnswer
@@ -1851,6 +1857,63 @@ public async atbCaptcha(params: paramsAtbCaptcha): Promise<CaptchaAnswer> {
18511857
}
18521858
}
18531859

1860+
1861+
/**
1862+
* ### Method for solving Audio captcha.
1863+
*
1864+
* Use the following method to bypass an audio captcha (`mp3` formats only). You must provide the language as `lang = 'en'`. Supported languages are "en", "ru", "de", "el", "pt", "fr".
1865+
* [Read more about audio captcha parameters](https://2captcha.com/2captcha-api#audio).
1866+
*
1867+
* @param {{ body, lang, pingback }} params Object containing parameters for the audio captcha.
1868+
* @param {string} params.body Base64 encoded audio file in `mp3` format. Max file size: 1 MB.
1869+
* @param {string} params.lang The language of audio record. Supported languages are: "en", "ru", "de", "el", "pt", "fr".
1870+
* @param {string} [params.pingback] URL for pingback response once captcha is solved.
1871+
*
1872+
* @returns {Promise<CaptchaAnswer>} The result from solving the audio captcha.
1873+
* @throws APIError
1874+
* @example
1875+
* solver.audio({
1876+
* body: "SUQzBAAAAAAAHFRTU0UAAAA...",
1877+
* lang: "en"
1878+
* })
1879+
* .then((res) => {
1880+
* console.log(res);
1881+
* })
1882+
* .catch((err) => {
1883+
* console.log(err);
1884+
* })
1885+
*/
1886+
public async audio(params: paramsAudioCaptcha): Promise<CaptchaAnswer> {
1887+
checkCaptchaParams(params, "audio")
1888+
1889+
const payload = {
1890+
...params,
1891+
method: "audio",
1892+
...this.defaultPayload
1893+
}
1894+
1895+
const response = await fetch(this.in, {
1896+
method: 'post',
1897+
body: JSON.stringify(payload),
1898+
headers: {'Content-Type': 'application/json'}
1899+
})
1900+
1901+
const result = await response.text()
1902+
1903+
let data;
1904+
try {
1905+
data = JSON.parse(result)
1906+
} catch {
1907+
throw new APIError(result)
1908+
}
1909+
1910+
if (data.status == 1) {
1911+
return this.pollResponse(data.request)
1912+
} else {
1913+
throw new APIError(data.request)
1914+
}
1915+
}
1916+
18541917
/**
18551918
* Reports a captcha as correctly solved.
18561919
*

src/utils/checkCaptchaParams.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Captcha methods for which parameter checking is available
22
const supportedMethods = ["userrecaptcha", "hcaptcha", "geetest", "geetest_v4","yandex","funcaptcha","lemin","amazon_waf",
3-
"turnstile", "base64", "capy","datadome", "cybersiara", "mt_captcha", "bounding_box", 'friendly_captcha', 'grid', 'textcaptcha', 'canvas', 'rotatecaptcha', 'keycaptcha', 'cutcaptcha', 'tencent', 'atb_captcha']
3+
"turnstile", "base64", "capy","datadome", "cybersiara", "mt_captcha", "bounding_box", 'friendly_captcha', 'grid',
4+
'textcaptcha', 'canvas', 'rotatecaptcha', 'keycaptcha', 'cutcaptcha', 'tencent', 'atb_captcha', 'audio']
45

56
// Names of required fields that must be contained in the parameters captcha
67
const recaptchaRequiredFields = ['pageurl','googlekey']
@@ -28,6 +29,7 @@ const keycaptchaRequiredFields = ['pageurl', 's_s_c_user_id', 's_s_c_session_id
2829
const cutcaptchaRequiredFields = ['pageurl', 'misery_key', 'api_key']
2930
const tencentRequiredFields = ['pageurl', 'app_id']
3031
const atbCaptchaRequiredFields = ['pageurl', 'app_id', 'api_server']
32+
const audioRequiredFields = ['body', 'lang']
3133

3234
/**
3335
* Getting required arguments for a captcha.
@@ -111,6 +113,9 @@ const getRequiredFildsArr = (method: string):Array<string> => {
111113
case "atb_captcha":
112114
requiredFieldsArr = atbCaptchaRequiredFields
113115
break;
116+
case "audio":
117+
requiredFieldsArr = audioRequiredFields
118+
break;
114119
}
115120
return requiredFieldsArr
116121
}

0 commit comments

Comments
 (0)