v0.13.0: Interceptors
Interceptors are used to modify or process the request before it is sent to the server and the response before it is returned to the caller. It's like a [tgb.Middleware], but for outgoing requests.
All interceptors should be registered on the client before the request is made.
client := tg.New("<TOKEN>",
tg.WithClientInterceptors(
tg.Interceptor(func(ctx context.Context, req *tg.Request, dst any, invoker tg.InterceptorInvoker) error {
started := time.Now()
// before request
err := invoker(ctx, req, dst)
// after request
log.Print("call %s took %s", req.Method, time.Since(started))
return err
}),
),
)
Arguments of the interceptor are:
ctx
- context of the request;req
- request object tg.Request;dst
- pointer to destination for the response, can benil
if the request is made withDoVoid
method;invoker
- function for calling the next interceptor or the actual request.
Contrib package has some useful interceptors:
- InterceptorRetryFloodError - retry request if the server returns a flood error. Parameters can be customized via options;
- InterceptorRetryInternalServerError - retry request if the server returns an error. Parameters can be customized via options;
- InterceptorMethodFilter - call underlying interceptor only for specified methods;
- InterceptorDefaultParseMethod - set default
parse_mode
for messages if not specified.
Interceptors are called in the order they are registered.
Example of using retry flood interceptor: examples/retry-flood
Full Changelog: v0.12.0...v0.13.0