Apply go fix ./... analysers (Go 1.26) across backend and examples:
- interface{} → any (type alias, no behaviour change)
- for i := 0; i < N; i++ → for range N / for i := range N
- slices.Contains / slices.ContainsFunc replacing manual loops
- strings.SplitSeq replacing strings.Split in range (avoids allocation)
- strings.CutPrefix replacing HasPrefix+TrimPrefix
- min() replacing manual if/else
- fmt.Appendf replacing []byte(fmt.Sprintf(...))
- strings.Builder replacing string += concatenation
- wg.Go(func(){}) replacing wg.Add(1)/go/wg.Done() pattern
- removed redundant ii := i loop variable copies (unnecessary since Go 1.22)
omitempty on struct-typed JSON fields: go fix removed omitempty from
struct-typed fields (time.Time, PostInfo, UserDetailEntry) because
encoding/json's omitempty never applied to struct types — it was always
a no-op. Kept as bare tags (no omitzero replacement) to preserve the
existing serialisation behaviour.
72 lines
1.9 KiB
Go
72 lines
1.9 KiB
Go
package providers
|
|
|
|
// Both Telegram auth and notifications need to receive messages received by Telegram bot in the loop,
|
|
// and below is the implementation of such loop which dispatched received events to both receivers,
|
|
// so that they could work at the same time.
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"time"
|
|
|
|
log "github.com/go-pkgz/lgr"
|
|
ntf "github.com/go-pkgz/notify"
|
|
)
|
|
|
|
type tgRequester interface {
|
|
Request(ctx context.Context, method string, b []byte, data any) error
|
|
}
|
|
|
|
// TGUpdatesReceiver used to dispatch telegram updates to multiple receivers
|
|
type TGUpdatesReceiver interface {
|
|
fmt.Stringer
|
|
ProcessUpdate(ctx context.Context, textUpdate string) error
|
|
}
|
|
|
|
// DispatchTelegramUpdates dispatches telegram updates to provided list of receivers
|
|
// Blocks caller
|
|
func DispatchTelegramUpdates(ctx context.Context, requester tgRequester, receivers []TGUpdatesReceiver, period time.Duration) {
|
|
// identifier of the first update to be requested.
|
|
// should be equal to LastSeenUpdateID + 1
|
|
// See https://core.telegram.org/bots/api#getupdates
|
|
var updateOffset int
|
|
|
|
processUpdatedTicker := time.NewTicker(period)
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
processUpdatedTicker.Stop()
|
|
return
|
|
case <-processUpdatedTicker.C:
|
|
url := `getUpdates?allowed_updates=["message"]`
|
|
if updateOffset != 0 {
|
|
url += fmt.Sprintf("&offset=%d", updateOffset)
|
|
}
|
|
|
|
var update ntf.TelegramUpdate
|
|
|
|
err := requester.Request(ctx, url, nil, &update)
|
|
if err != nil {
|
|
log.Printf("[WARN] failed to fetch updates: %v", err)
|
|
continue
|
|
}
|
|
|
|
for _, u := range update.Result {
|
|
if u.UpdateID >= updateOffset {
|
|
updateOffset = u.UpdateID + 1
|
|
}
|
|
}
|
|
|
|
if raw, err := json.Marshal(update); err == nil {
|
|
for _, r := range receivers {
|
|
e := r.ProcessUpdate(ctx, string(raw))
|
|
if e != nil {
|
|
log.Printf("[ERROR] failure from destination %s on processing telegram update %v", r, e)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|