Conversation
- Raise Telegram::Bot::TooManyRequests (with retry_after) for 429 responses instead of a generic Error, so callers can back off correctly. - Fix UpdatesPoller crashing on HTTPClient::TimeoutError: it only rescued Timeout::Error, which HTTPClient's own timeout errors don't inherit from. get_updates is a long poll and safe to retry, since a dropped connection can't cause duplicate processing. - Filter `text` out of UpdatesController::LogSubscriber logs by default, to avoid leaking users' message content into logs. Configurable via LogSubscriber.filtered_parameters=. - Add `via_webhook: true` to the reply helpers (respond_with, reply_with, answer_inline_query, answer_callback_query, answer_pre_checkout_query, answer_shipping_query, edit_message) to answer directly in the webhook HTTP response instead of making a separate API call, as described in https://core.telegram.org/bots/faq#how-can-i-make-requests-in-response-to-updates. Only the first such call per update takes effect; everything else (and any call at all outside webhook mode) falls back to a regular API call.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A few independent robustness/feature improvements, happy to split into separate PRs if that's easier to review:
error_for_responseonly special-cased 403/404, so a 429 flood-control response just raised a genericErrorand silently droppedparameters.retry_after. AddedTelegram::Bot::TooManyRequests < Errorwith aretry_afterreader, raised for 429 responses.UpdatesPollercrash on timeout (HTTPClient::ReceiveTimeoutError: execution expired #245):fetch_updatesonly rescuedTimeout::Error, butHTTPClient::ReceiveTimeoutError(what actually gets raised on a stalled long-poll connection) doesn't inherit from it, so the poller loop died instead of just retrying. Widened the rescue toHTTPClient::TimeoutErroras well. This is safe to retry unconditionally:get_updatesis a long poll keyed byoffset, so a dropped connection can't cause duplicate processing, unlike most other bot methods (sendMessageetc.), which I deliberately left alone since Telegram gives no idempotency guarantee on those - blindly retrying those on a timeout could double-send.LogSubscriberlogged the full update verbatim, including message text. AddedLogSubscriber.filtered_parameters(viaActiveSupport::ParameterFilter, defaults to%i[text]) sotextis redacted by default; assign[](or your own list) to change that.via_webhook: trueto all the reply helpers (respond_with,reply_with,answer_inline_query,answer_callback_query,answer_pre_checkout_query,answer_shipping_query,edit_message), implementing https://core.telegram.org/bots/faq#how-can-i-make-requests-in-response-to-updates. When set and the controller is running in webhook mode, the call is encoded as{method: ..., ...params}and handed toMiddlewareto return directly as the webhook HTTP response body, instead of making a separate API request - saves a request and helps with rate limits. Only the first such call in a given update takes effect (subsequent ones, and any call at all in poller mode, fall back to a normal API call), since Telegram only lets you answer once this way. I went with an explicit opt-in flag per call rather than trying to auto-detect "the last call in the action", since with multiple calls in one action (e.g.answer_callback_query+edit_message) there's no way to know upfront which one should get the free ride.Middleware#callunconditionally returned[200, {}, ['']]before; now it returns whateverdispatchclaimed as the webhook response, without changing.dispatch's own return value (some specs, e.g.Session, rely on it echoing the action's return value).Test plan
bundle exec rspec- 364 examples, 0 failuresbundle exec rubocop- no offensesvia_webhook: trueend-to-end throughMiddleware#callwith a realActionDispatch::Request/Rack env, confirmed no API call is made and the JSON body matches what Telegram expectsLogSubscriberredactstextby default