Skip to content

Commit

Permalink
Improve overall data
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulSonOfLars committed Nov 3, 2024
1 parent ab746c9 commit 7781f51
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
8 changes: 7 additions & 1 deletion bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@ func NewBot(token string, opts *BotOpts) (*Bot, error) {
if err != nil {
return nil, fmt.Errorf("failed to parse bot ID from token: %w", err)
}
b.User.Id = id
b.User = User{
Id: id,
IsBot: true,
// We mark these fields as missing so we can know why they're not available
FirstName: "<missing>",
Username: "<missing>",
}
}

return &b, nil
Expand Down
8 changes: 4 additions & 4 deletions ext/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
type Context struct {
// gotgbot.Update is inlined so that we can access all fields immediately if necessary.
*gotgbot.Update
// 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
// Bot represents gotgbot.User behind the Bot that received this update, so we can keep track of update ownership.
// Note: this information may be incomplete in the case where token validation is disabled.
Bot gotgbot.User
// 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 @@ -165,7 +165,7 @@ func NewContext(b *gotgbot.Bot, update *gotgbot.Update, data map[string]interfac

return &Context{
Update: update,
BotId: b.User.Id,
Bot: b.User,
Data: data,
EffectiveMessage: msg,
EffectiveChat: chat,
Expand Down
2 changes: 1 addition & 1 deletion ext/handlers/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func NewTestBot() *gotgbot.Bot {
return &gotgbot.Bot{
Token: "use-me",
User: gotgbot.User{
Id: 0,
Id: rand.Int63(),
IsBot: false,
FirstName: "gobot",
LastName: "",
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.BotId, ctx.EffectiveSender.Id(), ctx.EffectiveChat.Id), nil
return fmt.Sprintf("%d/%d/%d", ctx.Bot.Id, 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.BotId, ctx.EffectiveSender.Id()), nil
return fmt.Sprintf("%d/%d", ctx.Bot.Id, 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.BotId, ctx.EffectiveChat.Id), nil
return fmt.Sprintf("%d/%d", ctx.Bot.Id, ctx.EffectiveChat.Id), nil
}

// StateKey provides a sane default for handling incoming updates.
Expand Down
11 changes: 11 additions & 0 deletions ext/handlers/conversation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ func TestBasicKeyedConversation(t *testing.T) {

// But user two doesnt exist
checkExpectedState(t, &conv, messageFromTwo, "")

b2 := NewTestBot()
messageTo2 := NewMessage(b2, userIdOne, chatId, "message")
// And bot two hasn't changed either
checkExpectedState(t, &conv, messageTo2, "")
}

func TestBasicConversationExit(t *testing.T) {
Expand Down Expand Up @@ -358,6 +363,8 @@ func TestEmptyKeyConversation(t *testing.T) {

// runHandler ensures that the incoming update will trigger the conversation.
func runHandler(t *testing.T, b *gotgbot.Bot, conv *handlers.Conversation, message *ext.Context, currentState string, nextState string) {
t.Helper()

willRunHandler(t, b, conv, message, currentState)
if err := conv.HandleUpdate(b, message); err != nil {
t.Fatalf("unexpected error from handler: %s", err.Error())
Expand All @@ -368,6 +375,8 @@ func runHandler(t *testing.T, b *gotgbot.Bot, conv *handlers.Conversation, messa

// willRunHandler ensures that the incoming update will trigger the conversation.
func willRunHandler(t *testing.T, b *gotgbot.Bot, conv *handlers.Conversation, message *ext.Context, expectedState string) {
t.Helper()

t.Logf("conv %p: checking message for %d in %d with text: %s", conv, message.EffectiveSender.Id(), message.EffectiveChat.Id, message.Message.Text)

checkExpectedState(t, conv, message, expectedState)
Expand All @@ -378,6 +387,8 @@ func willRunHandler(t *testing.T, b *gotgbot.Bot, conv *handlers.Conversation, m
}

func checkExpectedState(t *testing.T, conv *handlers.Conversation, message *ext.Context, nextState string) {
t.Helper()

currentState, err := conv.StateStorage.Get(message)
if err != nil {
if nextState == "" && errors.Is(err, conversation.ErrKeyNotFound) {
Expand Down

0 comments on commit 7781f51

Please sign in to comment.