Skip to content

Unwanted cache when using the same prefix #16

@romanesko

Description

@romanesko

When sending a new calendar with the same prefix, everything works fine until switching between months.
After switching the calendar shows the dates of the first state.

Sample code showing undesirable behavior:

package main

import (
	"context"
	"fmt"
	"github.com/go-telegram/bot"
	"github.com/go-telegram/bot/models"
	"github.com/go-telegram/ui/datepicker"
	"os"
	"os/signal"
	"time"
)

var (
	availableDate = time.Now()
)

func main() {
	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
	defer cancel()

	telegramBotToken := os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN")

	opts := []bot.Option{
		bot.WithDefaultHandler(defaultHandler),
	}

	if b, err := bot.New(telegramBotToken, opts...); err != nil {
		panic(err)
	} else {
		b.Start(ctx)
	}
}

func sendCalendar(ctx context.Context, b *bot.Bot, chatId int64, date time.Time, msg string) {

	dpOpts := []datepicker.Option{
		datepicker.Dates(datepicker.DateModeInclude, []time.Time{date}),
		datepicker.WithPrefix("datepicker-custom"),
	}

	b.SendMessage(ctx, &bot.SendMessageParams{
		ChatID:      chatId,
		Text:        msg,
		ReplyMarkup: datepicker.New(b, onDatepickerSimpleSelect, dpOpts...),
	})
}

func defaultHandler(ctx context.Context, b *bot.Bot, update *models.Update) {
	msg := fmt.Sprintf("Available date: " + availableDate.Format("2006-01-02"))
	sendCalendar(ctx, b, update.Message.Chat.ID, availableDate, msg)
	availableDate = availableDate.AddDate(0, 0, 1)
	msg = fmt.Sprintf("Second calendar. Available date should be: %s, but if you check next month and go back, it will be cached from previous ", availableDate.Format("2006-01-02"))
	sendCalendar(ctx, b, update.Message.Chat.ID, availableDate, msg)
}

func onDatepickerSimpleSelect(ctx context.Context, b *bot.Bot, update models.MaybeInaccessibleMessage, date time.Time) {
	b.SendMessage(ctx, &bot.SendMessageParams{
		ChatID: update.Message.Chat.ID,
		Text:   "You select " + date.Format("2006-01-02"),
	})
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions