2captcha / 2captcha-go

Golang Module for easy integration with the API of 2captcha captcha solving service to bypass recaptcha, funcaptcha, geetest and solve any other captchas.
https://2captcha.com
MIT License
108 stars 42 forks source link

Solve method doesn't return id of captcha task. #23

Closed Insadem closed 5 months ago

Insadem commented 11 months ago

If you call client.Solve(req), you recieve token or error. If you have desire to report captcha task - you have to use callback parameter to receive id of each task. Simple solution would be to return id from this method too.

My implementation of this solution:

type SolveResult struct {
    ID    string
    Token string
}

func (c *Client) Solve(req Request) (SolveResult, error) {
    if c.Callback != "" {
        _, ok := req.Params["pingback"]
        if !ok {
            // set default pingback
            req.Params["pingback"] = c.Callback
        }
    }

    pingback, hasPingback := req.Params["pingback"]
    if pingback == "" {
        delete(req.Params, "pingback")
        hasPingback = false
    }

    _, ok := req.Params["soft_id"]
    if c.SoftId != 0 && !ok {
        req.Params["soft_id"] = strconv.FormatInt(int64(c.SoftId), 10)
    }

    id, err := c.Send(req)
    if err != nil {
        return SolveResult{}, err
    }

    // don't wait for result if Callback is used
    if hasPingback {
        return SolveResult{
            ID: id,
        }, nil
    }

    timeout := c.DefaultTimeout
    if req.Params["method"] == "userrecaptcha" {
        timeout = c.RecaptchaTimeout
    }

    result, resultErr := c.WaitForResult(id, timeout, c.PollingInterval)
    return SolveResult{ID: id, Token: result}, resultErr
}
PBadicean commented 5 months ago

@kratzky https://github.com/2captcha/2captcha-go/pull/30

PBadicean commented 5 months ago

@Insadem We have made new changes to this method, try using it with the latest adjustments.