Skip to content

Commit

Permalink
feat: upgrade everything
Browse files Browse the repository at this point in the history
  • Loading branch information
ernado committed Dec 1, 2024
1 parent 14c05f1 commit 65c8105
Show file tree
Hide file tree
Showing 16 changed files with 391 additions and 1,494 deletions.
72 changes: 30 additions & 42 deletions cmd/bot/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,29 @@ import (
"github.com/brpaz/echozap"
"github.com/cockroachdb/pebble"
"github.com/go-faster/errors"
"github.com/go-faster/sdk/app"
"github.com/google/go-github/v42/github"
"github.com/gotd/contrib/oteltg"
"github.com/gotd/td/session"
"github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/downloader"
"github.com/gotd/td/telegram/message"
"github.com/gotd/td/telegram/message/peer"
"github.com/gotd/td/telegram/message/styling"
"github.com/gotd/td/telegram/updates"
updhook "github.com/gotd/td/telegram/updates/hook"
"github.com/gotd/td/tg"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
bolt "go.etcd.io/bbolt"
"go.uber.org/multierr"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"

"github.com/gotd/td/session"
"github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/downloader"
"github.com/gotd/td/telegram/message"
"github.com/gotd/td/telegram/updates"
updhook "github.com/gotd/td/telegram/updates/hook"
"github.com/gotd/td/tg"

"github.com/gotd/bot/internal/app"
iapp "github.com/gotd/bot/internal/app"
"github.com/gotd/bot/internal/botapi"
"github.com/gotd/bot/internal/dispatch"
"github.com/gotd/bot/internal/docs"
"github.com/gotd/bot/internal/gentext"
"github.com/gotd/bot/internal/gh"
"github.com/gotd/bot/internal/storage"
)
Expand All @@ -57,21 +57,23 @@ type App struct {
mux dispatch.MessageMux
bot *dispatch.Bot

gpt2 gentext.Net
gpt3 gentext.Net
github *github.Client
http *http.Client
metrics *app.Metrics
logger *zap.Logger
github *github.Client
http *http.Client
logger *zap.Logger
}

func InitApp(m *app.Metrics, logger *zap.Logger) (_ *App, rerr error) {
func InitApp(m *app.Metrics, mm *iapp.Metrics, logger *zap.Logger) (_ *App, rerr error) {
// Reading app id from env (never hardcode it!).
appID, err := strconv.Atoi(os.Getenv("APP_ID"))
if err != nil {
return nil, errors.Wrapf(err, "APP_ID not set or invalid %q", os.Getenv("APP_ID"))
}

mw, err := oteltg.New(m.MeterProvider(), m.TracerProvider())
if err != nil {
return nil, errors.Wrap(err, "oteltg")
}

appHash := os.Getenv("APP_HASH")
if appHash == "" {
return nil, errors.New("no APP_HASH provided")
Expand Down Expand Up @@ -132,7 +134,7 @@ func InitApp(m *app.Metrics, logger *zap.Logger) (_ *App, rerr error) {
gaps, logger.Named("updates"),
),
Middlewares: []telegram.Middleware{
m.Middleware,
mw,
updhook.UpdateHook(func(ctx context.Context, u tg.UpdatesClass) error {
go func() {
if err := gaps.Handle(ctx, u); err != nil {
Expand All @@ -153,7 +155,7 @@ func InitApp(m *app.Metrics, logger *zap.Logger) (_ *App, rerr error) {
}

mux := dispatch.NewMessageMux()
var h dispatch.MessageHandler = app.NewMiddleware(mux, dd, m, app.MiddlewareOptions{
var h dispatch.MessageHandler = iapp.NewMiddleware(mux, dd, mm, iapp.MiddlewareOptions{
BotAPI: botapi.NewClient(token, botapi.Options{
HTTPClient: httpClient,
}),
Expand All @@ -180,7 +182,6 @@ func InitApp(m *app.Metrics, logger *zap.Logger) (_ *App, rerr error) {
mux: mux,
bot: b,
http: httpClient,
metrics: m,
logger: logger,
}

Expand All @@ -193,18 +194,6 @@ func InitApp(m *app.Metrics, logger *zap.Logger) (_ *App, rerr error) {
b.OnInline(docs.New(search))
}

gpt2, err := setupGPT2(a.http)
if err != nil {
return nil, errors.Wrap(err, "setup gpt2")
}
a.gpt2 = gpt2

gpt3, err := setupGPT3(a.http)
if err != nil {
return nil, errors.Wrap(err, "setup gpt3")
}
a.gpt3 = gpt3

if v, ok := os.LookupEnv("GITHUB_APP_ID"); ok {
ghClient, err := setupGithub(v, httpTransport)
if err != nil {
Expand Down Expand Up @@ -300,11 +289,6 @@ func (b *App) Run(ctx context.Context) error {
)
}

if err := b.gaps.Auth(ctx, b.raw, status.User.ID, status.User.Bot, false); err != nil {
return err
}
defer func() { _ = b.gaps.Logout() }()

if _, disableRegister := os.LookupEnv("DISABLE_COMMAND_REGISTER"); !disableRegister {
if err := b.mux.RegisterCommands(ctx, b.raw); err != nil {
return errors.Wrap(err, "register commands")
Expand All @@ -328,7 +312,7 @@ func (b *App) Run(ctx context.Context) error {
options = append(options,
styling.Plain("🚀 Started "),
styling.Italic(fmt.Sprintf("(%s, %s, layer: %d) ",
info.GoVersion, app.GetVersion(), tg.Layer),
info.GoVersion, iapp.GetVersion(), tg.Layer),
),
styling.Code(commit),
)
Expand All @@ -337,15 +321,19 @@ func (b *App) Run(ctx context.Context) error {
}
}

<-ctx.Done()
return ctx.Err()
self, err := b.client.Self(ctx)
if err != nil {
return errors.Wrap(err, "self")
}

return b.gaps.Run(ctx, b.client.API(), self.ID, updates.AuthOptions{IsBot: true})
})
})
return group.Wait()
}

func runBot(ctx context.Context, m *app.Metrics, logger *zap.Logger) (rerr error) {
a, err := InitApp(m, logger)
func runBot(ctx context.Context, m *app.Metrics, mm *iapp.Metrics, logger *zap.Logger) (rerr error) {
a, err := InitApp(m, mm, logger)
if err != nil {
return errors.Wrap(err, "initialize")
}
Expand Down
23 changes: 12 additions & 11 deletions cmd/bot/bolt_state.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"encoding/binary"
"fmt"

Expand Down Expand Up @@ -29,7 +30,7 @@ type BoltState struct {

func NewBoltState(db *bolt.DB) *BoltState { return &BoltState{db} }

func (s *BoltState) GetState(userID int64) (state updates.State, found bool, err error) {
func (s *BoltState) GetState(ctx context.Context, userID int64) (state updates.State, found bool, err error) {
tx, err := s.db.Begin(false)
if err != nil {
return updates.State{}, false, err
Expand Down Expand Up @@ -65,7 +66,7 @@ func (s *BoltState) GetState(userID int64) (state updates.State, found bool, err
}, true, nil
}

func (s *BoltState) SetState(userID int64, state updates.State) error {
func (s *BoltState) SetState(ctx context.Context, userID int64, state updates.State) error {
return s.db.Update(func(tx *bolt.Tx) error {
user, err := tx.CreateBucketIfNotExists(i642b(userID))
if err != nil {
Expand All @@ -92,7 +93,7 @@ func (s *BoltState) SetState(userID int64, state updates.State) error {
})
}

func (s *BoltState) SetPts(userID int64, pts int) error {
func (s *BoltState) SetPts(ctx context.Context, userID int64, pts int) error {
return s.db.Update(func(tx *bolt.Tx) error {
user, err := tx.CreateBucketIfNotExists(i642b(userID))
if err != nil {
Expand All @@ -107,7 +108,7 @@ func (s *BoltState) SetPts(userID int64, pts int) error {
})
}

func (s *BoltState) SetQts(userID int64, qts int) error {
func (s *BoltState) SetQts(ctx context.Context, userID int64, qts int) error {
return s.db.Update(func(tx *bolt.Tx) error {
user, err := tx.CreateBucketIfNotExists(i642b(userID))
if err != nil {
Expand All @@ -122,7 +123,7 @@ func (s *BoltState) SetQts(userID int64, qts int) error {
})
}

func (s *BoltState) SetDate(userID int64, date int) error {
func (s *BoltState) SetDate(ctx context.Context, userID int64, date int) error {
return s.db.Update(func(tx *bolt.Tx) error {
user, err := tx.CreateBucketIfNotExists(i642b(userID))
if err != nil {
Expand All @@ -137,7 +138,7 @@ func (s *BoltState) SetDate(userID int64, date int) error {
})
}

func (s *BoltState) SetSeq(userID int64, seq int) error {
func (s *BoltState) SetSeq(ctx context.Context, userID int64, seq int) error {
return s.db.Update(func(tx *bolt.Tx) error {
user, err := tx.CreateBucketIfNotExists(i642b(userID))
if err != nil {
Expand All @@ -152,7 +153,7 @@ func (s *BoltState) SetSeq(userID int64, seq int) error {
})
}

func (s *BoltState) SetDateSeq(userID int64, date, seq int) error {
func (s *BoltState) SetDateSeq(ctx context.Context, userID int64, date, seq int) error {
return s.db.Update(func(tx *bolt.Tx) error {
user, err := tx.CreateBucketIfNotExists(i642b(userID))
if err != nil {
Expand All @@ -170,7 +171,7 @@ func (s *BoltState) SetDateSeq(userID int64, date, seq int) error {
})
}

func (s *BoltState) GetChannelPts(userID, channelID int64) (int, bool, error) {
func (s *BoltState) GetChannelPts(ctx context.Context, userID, channelID int64) (int, bool, error) {
tx, err := s.db.Begin(false)
if err != nil {
return 0, false, err
Expand All @@ -195,7 +196,7 @@ func (s *BoltState) GetChannelPts(userID, channelID int64) (int, bool, error) {
return b2i(pts), true, nil
}

func (s *BoltState) SetChannelPts(userID, channelID int64, pts int) error {
func (s *BoltState) SetChannelPts(ctx context.Context, userID, channelID int64, pts int) error {
return s.db.Update(func(tx *bolt.Tx) error {
user, err := tx.CreateBucketIfNotExists(i642b(userID))
if err != nil {
Expand All @@ -210,7 +211,7 @@ func (s *BoltState) SetChannelPts(userID, channelID int64, pts int) error {
})
}

func (s *BoltState) ForEachChannels(userID int64, f func(channelID int64, pts int) error) error {
func (s *BoltState) ForEachChannels(ctx context.Context, userID int64, f func(ctx context.Context, channelID int64, pts int) error) error {
return s.db.Update(func(tx *bolt.Tx) error {
user, err := tx.CreateBucketIfNotExists(i642b(userID))
if err != nil {
Expand All @@ -223,7 +224,7 @@ func (s *BoltState) ForEachChannels(userID int64, f func(channelID int64, pts in
}

return channels.ForEach(func(k, v []byte) error {
return f(b2i64(k), b2i(v))
return f(ctx, b2i64(k), b2i(v))
})
})
}
8 changes: 1 addition & 7 deletions cmd/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (

"github.com/gotd/bot/internal/app"
"github.com/gotd/bot/internal/dispatch"
"github.com/gotd/bot/internal/gentext"
"github.com/gotd/bot/internal/gpt"
"github.com/gotd/bot/internal/inspect"
"github.com/gotd/bot/internal/tts"
)
Expand Down Expand Up @@ -49,11 +47,7 @@ func setupBot(a *App) error {

a.mux.Handle("/pp", "Pretty print replied message", inspect.Pretty())
a.mux.Handle("/json", "Print JSON of replied message", inspect.JSON())
a.mux.Handle("/stat", "Metrics and version", app.NewHandler(a.metrics))
a.mux.Handle("/stat", "Version", app.NewHandler())
a.mux.Handle("/tts", "Text to speech", tts.New(a.http))
a.mux.Handle("/gpt2", "Complete text with GPT2",
gpt.New(gentext.NewGPT2().WithClient(a.http)))
a.mux.Handle("/gpt3", "Complete text with GPT3",
gpt.New(gentext.NewGPT3().WithClient(a.http)))
return nil
}
36 changes: 0 additions & 36 deletions cmd/bot/gpt.go

This file was deleted.

37 changes: 17 additions & 20 deletions cmd/bot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,30 @@ package main

import (
"context"
"os"

"github.com/go-faster/errors"
"github.com/go-faster/sdk/app"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"

"github.com/gotd/bot/internal/app"
iapp "github.com/gotd/bot/internal/app"
)

func main() {
app.Run(func(ctx context.Context, lg *zap.Logger) error {
m, err := app.NewMetrics(lg, app.Config{
Name: "bot",
Namespace: "gotd",
Addr: os.Getenv("METRICS_ADDR"),
})
if err != nil {
return errors.Wrap(err, "metrics")
app.Run(func(ctx context.Context, lg *zap.Logger, m *app.Metrics) error {
mx := &iapp.Metrics{}
{
var err error
meter := m.MeterProvider().Meter("gotd.bot")
if mx.Bytes, err = meter.Int64Counter("gotd.bot.bytes"); err != nil {
return errors.Wrap(err, "bytes")
}
if mx.Messages, err = meter.Int64Counter("gotd.bot.messages"); err != nil {
return errors.Wrap(err, "messages")
}
if mx.Responses, err = meter.Int64Counter("gotd.bot.responses"); err != nil {
return errors.Wrap(err, "responses")
}
}
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error {
return runBot(ctx, m, lg.Named("bot"))
})
g.Go(func() error {
return m.Run(ctx)
})

return g.Wait()
return runBot(ctx, m, mx, lg.Named("bot"))
})
}
Loading

0 comments on commit 65c8105

Please sign in to comment.