Skip to content

Commit

Permalink
Use botID instead of full bot info
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulSonOfLars committed Sep 25, 2024
1 parent dd7351b commit 846d280
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
21 changes: 19 additions & 2 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ import (
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"time"
)

//go:generate go run ./scripts/generate

var (
ErrNilBotClient = errors.New("nil BotClient")
ErrInvalidTokenFormat = errors.New("invalid token format")
)

// Bot is the default Bot struct used to send and receive messages to the telegram API.
type Bot struct {
// Token stores the bot's secret token obtained from t.me/BotFather, and used to interact with telegram's API.
Expand Down Expand Up @@ -76,6 +83,18 @@ func NewBot(token string, opts *BotOpts) (*Bot, error) {
return nil, fmt.Errorf("failed to check bot token: %w", err)
}
b.User = *botUser
} else {
// If token checks are disabled, we populate the bot's ID from the token.
split := strings.Split(token, ":")
if len(split) != 2 {
return nil, fmt.Errorf("%w: expected '123:abcd', got %s", ErrInvalidTokenFormat, token)
}

id, err := strconv.ParseInt(split[0], 10, 64)
if err != nil {
return nil, fmt.Errorf("failed to parse bot ID from token: %w", err)
}
b.User.Id = id
}

return &b, nil
Expand All @@ -89,8 +108,6 @@ func (bot *Bot) UseMiddleware(mw func(client BotClient) BotClient) *Bot {
return bot
}

var ErrNilBotClient = errors.New("nil BotClient")

func (bot *Bot) Request(method string, params map[string]string, data map[string]FileReader, opts *RequestOpts) (json.RawMessage, error) {
return bot.RequestWithContext(context.Background(), method, params, data, opts)
}
Expand Down
9 changes: 4 additions & 5 deletions ext/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import (
type Context struct {
// gotgbot.Update is inlined so that we can access all fields immediately if necessary.
*gotgbot.Update
// We store the info of the Bot that received this update so we can keep track of update ownership.
// We do NOT store full gotgbot.Bot struct, as that would leak the bot token and bot client information in what
// should be a data-only struct.
Bot gotgbot.User
// BotID represents the ID of the Bot that received this update, so we can keep track of update ownership.
// We do not store more, as we may not have that data (eg, in the cases of token validation being disabled).
BotId int64
// Data represents update-local storage.
// This can be used to pass data across handlers - for example, to cache operations relevant to the current update,
// such as admin checks.
Expand Down Expand Up @@ -166,7 +165,7 @@ func NewContext(b *gotgbot.Bot, update *gotgbot.Update, data map[string]interfac

return &Context{
Update: update,
Bot: b.User,
BotId: b.User.Id,
Data: data,
EffectiveMessage: msg,
EffectiveChat: chat,
Expand Down
6 changes: 3 additions & 3 deletions ext/handlers/conversation/key_strategies.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ func KeyStrategySenderAndChat(ctx *ext.Context) (string, error) {
if ctx.EffectiveSender == nil || ctx.EffectiveChat == nil {
return "", fmt.Errorf("missing sender or chat fields: %w", ErrEmptyKey)
}
return fmt.Sprintf("%d/%d/%d", ctx.Bot.Id, ctx.EffectiveSender.Id(), ctx.EffectiveChat.Id), nil
return fmt.Sprintf("%d/%d/%d", ctx.BotId, ctx.EffectiveSender.Id(), ctx.EffectiveChat.Id), nil
}

// KeyStrategySender gives a unique conversation to each sender, and that single conversation is available in all chats.
func KeyStrategySender(ctx *ext.Context) (string, error) {
if ctx.EffectiveSender == nil {
return "", fmt.Errorf("missing sender field: %w", ErrEmptyKey)
}
return fmt.Sprintf("%d/%d", ctx.Bot.Id, ctx.EffectiveSender.Id()), nil
return fmt.Sprintf("%d/%d", ctx.BotId, ctx.EffectiveSender.Id()), nil
}

// KeyStrategyChat gives a unique conversation to each chat, which all senders can interact in together.
func KeyStrategyChat(ctx *ext.Context) (string, error) {
if ctx.EffectiveChat == nil {
return "", fmt.Errorf("missing chat field: %w", ErrEmptyKey)
}
return fmt.Sprintf("%d/%d", ctx.Bot.Id, ctx.EffectiveChat.Id), nil
return fmt.Sprintf("%d/%d", ctx.BotId, ctx.EffectiveChat.Id), nil
}

// StateKey provides a sane default for handling incoming updates.
Expand Down

0 comments on commit 846d280

Please sign in to comment.