add settimezone command and timezone display in settings

- New /settimezone <IANA name> command to change user timezone
- Shows current timezone in settings menu
- timezone callback shows current value and instructions
- Updated /help with the new command
This commit is contained in:
2026-06-24 10:17:09 +03:30
parent 069bbb614e
commit 8fe43bff4d

View File

@@ -109,6 +109,8 @@ func (h *Handler) HandleMessage(update tgbotapi.Update) {
h.handleSetReportTime(msg)
case "/setaccent":
h.handleAccentMsg(msg)
case "/settimezone":
h.handleSetTimezone(msg)
default:
h.sendText(msg.Chat.ID, "Unknown command")
}
@@ -154,6 +156,8 @@ func (h *Handler) HandleCallback(update tgbotapi.Update) {
h.calTypeCallback(chatID, msgID, cb.ID)
case "accent":
h.accentCallback(chatID, msgID, cb.ID)
case "timezone":
h.timezoneCallback(chatID, msgID, cb.ID)
case "history":
h.historyCallback(chatID, msgID, cb.ID)
case "reporttoggle":
@@ -344,6 +348,36 @@ func (h *Handler) handleAccentMsg(msg *tgbotapi.Message) {
h.Bot.Send(reply)
}
func (h *Handler) handleSetTimezone(msg *tgbotapi.Message) {
arg := msg.CommandArguments()
if arg == "" {
user, err := h.getOrCreateUser(msg.Chat.ID)
if err != nil {
h.sendText(msg.Chat.ID, "Error loading profile")
return
}
h.sendText(msg.Chat.ID, fmt.Sprintf("Current timezone: %s\nUsage: /settimezone <IANA timezone>\nExamples: Asia/Tehran, Europe/London, America/New_York", user.Timezone))
return
}
loc, err := time.LoadLocation(arg)
if err != nil {
h.sendText(msg.Chat.ID, fmt.Sprintf("Invalid timezone: %s\nUse IANA format, e.g. Asia/Tehran, Europe/London", arg))
return
}
user, err := h.getOrCreateUser(msg.Chat.ID)
if err != nil {
h.sendText(msg.Chat.ID, "Error loading profile")
return
}
user.Timezone = arg
if err := h.DB.UpdateUser(user); err != nil {
h.sendText(msg.Chat.ID, "Error saving timezone")
return
}
now := time.Now().In(loc)
h.sendText(msg.Chat.ID, fmt.Sprintf("Timezone set to %s (current time: %s)", arg, now.Format("15:04")))
}
func (h *Handler) handleStart(msg *tgbotapi.Message) {
user, err := h.getOrCreateUser(msg.Chat.ID)
if err != nil {
@@ -371,6 +405,7 @@ func (h *Handler) handleHelp(msg *tgbotapi.Message) {
/reporttoggle - Enable/disable auto report
/setreporttime HH:MM - Set daily report time
/setaccent - Select accent color
/settimezone <name> - Set your timezone (e.g. Asia/Tehran)
Buttons on the main menu also work.`
h.sendText(msg.Chat.ID, text)
@@ -469,6 +504,9 @@ func (h *Handler) buildSettingsKeyboard(user *db.User) (string, tgbotapi.InlineK
reportStatus = "enabled"
}
var rows [][]tgbotapi.InlineKeyboardButton
rows = append(rows, tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData(fmt.Sprintf("Timezone: %s", user.Timezone), "timezone"),
))
rows = append(rows, tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData(fmt.Sprintf("Accent: %s", user.ExportAccent), "accent"),
))
@@ -502,6 +540,23 @@ func (h *Handler) accentCallback(chatID int64, msgID int, callbackID string) {
h.Bot.Send(edit)
}
func (h *Handler) timezoneCallback(chatID int64, msgID int, callbackID string) {
defer h.Bot.Request(tgbotapi.NewCallback(callbackID, ""))
user, err := h.getOrCreateUser(chatID)
if err != nil {
return
}
text := fmt.Sprintf("Current timezone: %s\n\nUse /settimezone <name> to change it.\nExamples: Asia/Tehran, Europe/London, America/New_York", user.Timezone)
kb := tgbotapi.NewInlineKeyboardMarkup(
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("Back to Settings", "back_settings"),
),
)
edit := tgbotapi.NewEditMessageText(chatID, msgID, text)
edit.ReplyMarkup = &kb
h.Bot.Send(edit)
}
func (h *Handler) buildAccentKeyboard(user *db.User) (string, tgbotapi.InlineKeyboardMarkup) {
accents := []struct {
name string