node-telegram-bot-api icon indicating copy to clipboard operation
node-telegram-bot-api copied to clipboard

Support for customizable client-side timeout in addition to server-side timeout

Open zwa73 opened this issue 9 months ago • 1 comments

While using the package for long-polling scenarios, I've noticed a limitation when dealing with network instability. Specifically, the timeout parameter in request is tied directly to server-side timeout behavior.

When using @cypress/request-promise within a function that sends requests, I observed that the timeout parameter is primarily passed as part of the data payload to the API (options.form.timeout).

While this successfully controls server-side timeout in the API (e.g., the Telegram API), there seems to be no clear way to enforce a client-side timeout for the network request itself.

I would like to request the ability to define client-side timeouts independently, to ensure requests don't hang indefinitely due to network issues or server-side delays.

And for the current implementation

_request(_path, options = {}) {
    if (!this.token) {
      return Promise.reject(new errors.FatalError('Telegram Bot Token not provided!'));
    }

    if (this.options.request) {
      Object.assign(options, this.options.request);
    }

    if (options.form) {
      this._fixReplyMarkup(options.form);
      this._fixEntitiesField(options.form);
      this._fixReplyParameters(options.form);
    }
    if (options.qs) {
      this._fixReplyMarkup(options.qs);
      this._fixReplyParameters(options.qs);
    }

    options.method = 'POST';
    options.url = this._buildURL(_path);
    options.simple = false;
    options.resolveWithFullResponse = true;
    options.forever = true;
    debug('HTTP request: %j', options);
    return request(options)
      .then(resp => {
        let data;
        try {
          data = resp.body = JSON.parse(resp.body);
        } catch (err) {
          throw new errors.ParseError(`Error parsing response: ${resp.body}`, resp);
        }

        if (data.ok) {
          return data.result;
        }

        throw new errors.TelegramError(`${data.error_code} ${data.description}`, resp);
      }).catch(error => {
        // TODO: why can't we do `error instanceof errors.BaseError`?
        if (error.response) throw error;
        throw new errors.FatalError(error);
      });
  }

Can I directly set a value on options.timeout as a temporary workaround?

like

if(_path=="getUpdates"){
	options.timeout = options.form.timeout*1000 + 5000;
}

zwa73 avatar Mar 07 '25 16:03 zwa73

something is very off with Telegram recently my long polling bot worked like a charm for more than a year, but recently messaging is slow with constant timeouts

keerah avatar Mar 09 '25 06:03 keerah