From 7781f513229d603b2827d7855588c58b38ed6bc6 Mon Sep 17 00:00:00 2001 From: Paul Larsen Date: Sun, 3 Nov 2024 19:41:25 +0000 Subject: [PATCH] Improve overall data --- bot.go | 8 +++++++- ext/context.go | 8 ++++---- ext/handlers/common_test.go | 2 +- ext/handlers/conversation/key_strategies.go | 6 +++--- ext/handlers/conversation_test.go | 11 +++++++++++ 5 files changed, 26 insertions(+), 9 deletions(-) diff --git a/bot.go b/bot.go index 34078cd..7f6ab2f 100644 --- a/bot.go +++ b/bot.go @@ -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: "", + Username: "", + } } return &b, nil diff --git a/ext/context.go b/ext/context.go index 73e1e8d..2a55152 100644 --- a/ext/context.go +++ b/ext/context.go @@ -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. @@ -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, diff --git a/ext/handlers/common_test.go b/ext/handlers/common_test.go index fcb7e42..1cd2a1e 100644 --- a/ext/handlers/common_test.go +++ b/ext/handlers/common_test.go @@ -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: "", diff --git a/ext/handlers/conversation/key_strategies.go b/ext/handlers/conversation/key_strategies.go index 1a0e066..b8f9b9b 100644 --- a/ext/handlers/conversation/key_strategies.go +++ b/ext/handlers/conversation/key_strategies.go @@ -26,7 +26,7 @@ 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. @@ -34,7 +34,7 @@ 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. @@ -42,7 +42,7 @@ 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. diff --git a/ext/handlers/conversation_test.go b/ext/handlers/conversation_test.go index 61428bb..697ac67 100644 --- a/ext/handlers/conversation_test.go +++ b/ext/handlers/conversation_test.go @@ -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) { @@ -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()) @@ -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) @@ -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) {