teldrive/internal/tgc/tgc.go

96 lines
2.9 KiB
Go
Raw Normal View History

2023-09-20 03:20:44 +08:00
package tgc
import (
"context"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/divyam234/teldrive/internal/config"
2023-12-03 03:47:23 +08:00
"github.com/divyam234/teldrive/internal/kv"
"github.com/divyam234/teldrive/internal/recovery"
"github.com/divyam234/teldrive/internal/retry"
2023-09-20 03:20:44 +08:00
"github.com/gotd/contrib/middleware/floodwait"
"github.com/gotd/contrib/middleware/ratelimit"
tdclock "github.com/gotd/td/clock"
"github.com/gotd/td/session"
"github.com/gotd/td/telegram"
"golang.org/x/time/rate"
)
func defaultMiddlewares(ctx context.Context) ([]telegram.Middleware, error) {
2023-11-25 12:31:29 +08:00
return []telegram.Middleware{
recovery.New(ctx, Backoff(tdclock.System)),
2023-11-25 12:31:29 +08:00
retry.New(5),
floodwait.NewSimpleWaiter(),
}, nil
}
2023-09-20 03:20:44 +08:00
2024-02-13 00:02:55 +08:00
func New(ctx context.Context, config *config.TGConfig, handler telegram.UpdateHandler, storage session.Storage, middlewares ...telegram.Middleware) *telegram.Client {
2023-09-20 03:20:44 +08:00
opts := telegram.Options{
Device: telegram.DeviceConfig{
DeviceModel: config.DeviceModel,
SystemVersion: config.SystemVersion,
AppVersion: config.AppVersion,
SystemLangCode: config.SystemLangCode,
LangPack: config.LangPack,
LangCode: config.LangCode,
},
2023-09-20 03:20:44 +08:00
SessionStorage: storage,
2023-11-25 12:31:29 +08:00
RetryInterval: time.Second,
MaxRetries: 10,
2023-09-20 03:20:44 +08:00
DialTimeout: 10 * time.Second,
Middlewares: middlewares,
UpdateHandler: handler,
}
return telegram.NewClient(config.AppId, config.AppHash, opts)
2023-09-20 03:20:44 +08:00
}
2024-02-13 00:02:55 +08:00
func NoAuthClient(ctx context.Context, config *config.TGConfig, handler telegram.UpdateHandler, storage session.Storage) *telegram.Client {
middlewares, _ := defaultMiddlewares(ctx)
2023-09-20 03:20:44 +08:00
middlewares = append(middlewares, ratelimit.New(rate.Every(time.Millisecond*100), 5))
return New(ctx, config, handler, storage, middlewares...)
2023-09-20 03:20:44 +08:00
}
2024-02-13 00:02:55 +08:00
func AuthClient(ctx context.Context, config *config.TGConfig, sessionStr string) (*telegram.Client, error) {
2023-09-20 03:20:44 +08:00
data, err := session.TelethonSession(sessionStr)
if err != nil {
return nil, err
}
var (
storage = new(session.StorageMemory)
loader = session.Loader{Storage: storage}
)
if err := loader.Save(context.TODO(), data); err != nil {
return nil, err
}
middlewares, _ := defaultMiddlewares(ctx)
middlewares = append(middlewares, ratelimit.New(rate.Every(time.Millisecond*
time.Duration(config.Rate)), config.RateBurst))
return New(ctx, config, nil, storage, middlewares...), nil
2023-09-20 03:20:44 +08:00
}
2024-02-13 00:02:55 +08:00
func BotClient(ctx context.Context, KV kv.KV, config *config.TGConfig, token string) (*telegram.Client, error) {
storage := kv.NewSession(KV, kv.Key("botsession", token))
middlewares, _ := defaultMiddlewares(ctx)
if config.RateLimit {
middlewares = append(middlewares, ratelimit.New(rate.Every(time.Millisecond*
time.Duration(config.Rate)), config.RateBurst))
2023-11-25 12:31:29 +08:00
}
return New(ctx, config, nil, storage, middlewares...), nil
2023-11-25 12:31:29 +08:00
}
func Backoff(_clock tdclock.Clock) backoff.BackOff {
b := backoff.NewExponentialBackOff()
b.Multiplier = 1.1
b.MaxElapsedTime = time.Duration(120) * time.Second
b.Clock = _clock
return b
2023-09-20 03:20:44 +08:00
}