remove reply keyboard; keep Clock In/Out as inline buttons only
- Removed smartKeyboard, defaultKeyboard, updateReplyKeyboard - Removed statusMsg/keyboardMsg tracking from Handler - sendText now sends plain messages (no reply keyboard) - handleActionMsg sends result with mainKeyboard() inline keyboard - handleActionCallback edits message with backKeyboard (unchanged) - handleStart sends welcome + menu with mainKeyboard - SendDailyReport sends plain text report - Removed text-trigger 'Clock In'/'Clock Out' message handlers (only /clockin and /clockout commands remain)
This commit is contained in:
@@ -21,7 +21,6 @@ type Handler struct {
|
|||||||
DB *db.Store
|
DB *db.Store
|
||||||
AllowedUsers map[int64]bool
|
AllowedUsers map[int64]bool
|
||||||
lastMsgTime map[int64]time.Time
|
lastMsgTime map[int64]time.Time
|
||||||
keyboardMsg map[int64]int
|
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,7 +30,6 @@ func NewHandler(bot *tgbotapi.BotAPI, store *db.Store, allowed map[int64]bool) *
|
|||||||
DB: store,
|
DB: store,
|
||||||
AllowedUsers: allowed,
|
AllowedUsers: allowed,
|
||||||
lastMsgTime: make(map[int64]time.Time),
|
lastMsgTime: make(map[int64]time.Time),
|
||||||
keyboardMsg: make(map[int64]int),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,10 +50,11 @@ func (h *Handler) handleActionMsg(msg *tgbotapi.Message, fn actionFunc) {
|
|||||||
text, err := fn(msg.Chat.ID)
|
text, err := fn(msg.Chat.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.sendText(msg.Chat.ID, err.Error())
|
h.sendText(msg.Chat.ID, err.Error())
|
||||||
} else {
|
return
|
||||||
h.sendSuccessWithMenu(msg.Chat.ID, text)
|
|
||||||
h.updateReplyKeyboard(msg.Chat.ID)
|
|
||||||
}
|
}
|
||||||
|
reply := tgbotapi.NewMessage(msg.Chat.ID, text)
|
||||||
|
reply.ReplyMarkup = mainKeyboard()
|
||||||
|
h.Bot.Send(reply)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) handleActionCallback(chatID int64, msgID int, callbackID string, fn actionFunc) {
|
func (h *Handler) handleActionCallback(chatID int64, msgID int, callbackID string, fn actionFunc) {
|
||||||
@@ -68,7 +67,6 @@ func (h *Handler) handleActionCallback(chatID int64, msgID int, callbackID strin
|
|||||||
kb := backKeyboard()
|
kb := backKeyboard()
|
||||||
edit.ReplyMarkup = &kb
|
edit.ReplyMarkup = &kb
|
||||||
h.Bot.Send(edit)
|
h.Bot.Send(edit)
|
||||||
h.updateReplyKeyboard(chatID)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) HandleMessage(update tgbotapi.Update) {
|
func (h *Handler) HandleMessage(update tgbotapi.Update) {
|
||||||
@@ -87,9 +85,9 @@ func (h *Handler) HandleMessage(update tgbotapi.Update) {
|
|||||||
switch msg.Text {
|
switch msg.Text {
|
||||||
case "/start":
|
case "/start":
|
||||||
h.handleStart(msg)
|
h.handleStart(msg)
|
||||||
case "/clockin", "Clock In":
|
case "/clockin":
|
||||||
h.handleActionMsg(msg, h.clockIn)
|
h.handleActionMsg(msg, h.clockIn)
|
||||||
case "/clockout", "Clock Out":
|
case "/clockout":
|
||||||
h.handleActionMsg(msg, h.clockOut)
|
h.handleActionMsg(msg, h.clockOut)
|
||||||
case "/worktype":
|
case "/worktype":
|
||||||
h.handleWorkTypeMsg(msg)
|
h.handleWorkTypeMsg(msg)
|
||||||
@@ -352,31 +350,12 @@ func (h *Handler) handleStart(msg *tgbotapi.Message) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
slog.Info("start", "user_id", user.ID, "chat_id", msg.Chat.ID)
|
slog.Info("start", "user_id", user.ID, "chat_id", msg.Chat.ID)
|
||||||
reply := tgbotapi.NewMessage(msg.Chat.ID, "We hope you have a happy day working.")
|
h.sendText(msg.Chat.ID, "We hope you have a happy day working.")
|
||||||
reply.ReplyMarkup = h.smartKeyboard(msg.Chat.ID)
|
|
||||||
h.Bot.Send(reply)
|
|
||||||
menu := tgbotapi.NewMessage(msg.Chat.ID, h.buildStatusText(msg.Chat.ID))
|
menu := tgbotapi.NewMessage(msg.Chat.ID, h.buildStatusText(msg.Chat.ID))
|
||||||
menu.ReplyMarkup = mainKeyboard()
|
menu.ReplyMarkup = mainKeyboard()
|
||||||
h.Bot.Send(menu)
|
h.Bot.Send(menu)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) updateReplyKeyboard(chatID int64) {
|
|
||||||
h.mu.Lock()
|
|
||||||
oldID := h.keyboardMsg[chatID]
|
|
||||||
h.mu.Unlock()
|
|
||||||
if oldID != 0 {
|
|
||||||
h.Bot.Request(tgbotapi.NewDeleteMessage(chatID, oldID))
|
|
||||||
}
|
|
||||||
msg := tgbotapi.NewMessage(chatID, "\u200C")
|
|
||||||
msg.ReplyMarkup = h.smartKeyboard(chatID)
|
|
||||||
sent, err := h.Bot.Send(msg)
|
|
||||||
if err == nil {
|
|
||||||
h.mu.Lock()
|
|
||||||
h.keyboardMsg[chatID] = sent.MessageID
|
|
||||||
h.mu.Unlock()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Handler) handleWorkTypeMsg(msg *tgbotapi.Message) {
|
func (h *Handler) handleWorkTypeMsg(msg *tgbotapi.Message) {
|
||||||
user, day, err := h.getUserToday(msg.Chat.ID)
|
user, day, err := h.getUserToday(msg.Chat.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -445,7 +424,6 @@ func (h *Handler) selectWorkType(chatID int64, msgID int, callbackID, idStr stri
|
|||||||
if err := h.DB.UpdateDay(day); err != nil {
|
if err := h.DB.UpdateDay(day); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
h.updateReplyKeyboard(chatID)
|
|
||||||
text := fmt.Sprintf("Work type set to: %s", wt.Name)
|
text := fmt.Sprintf("Work type set to: %s", wt.Name)
|
||||||
edit := tgbotapi.NewEditMessageText(chatID, msgID, text)
|
edit := tgbotapi.NewEditMessageText(chatID, msgID, text)
|
||||||
kb := backKeyboard()
|
kb := backKeyboard()
|
||||||
@@ -544,7 +522,6 @@ func (h *Handler) selectAccent(chatID int64, msgID int, callbackID, name string)
|
|||||||
if err := h.DB.UpdateUser(user); err != nil {
|
if err := h.DB.UpdateUser(user); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
h.updateReplyKeyboard(chatID)
|
|
||||||
text := fmt.Sprintf("Accent set to: %s", name)
|
text := fmt.Sprintf("Accent set to: %s", name)
|
||||||
edit := tgbotapi.NewEditMessageText(chatID, msgID, text)
|
edit := tgbotapi.NewEditMessageText(chatID, msgID, text)
|
||||||
kb := tgbotapi.NewInlineKeyboardMarkup(
|
kb := tgbotapi.NewInlineKeyboardMarkup(
|
||||||
@@ -634,7 +611,6 @@ func (h *Handler) selectCalendar(chatID int64, msgID int, callbackID, name strin
|
|||||||
if err := h.DB.UpdateUser(user); err != nil {
|
if err := h.DB.UpdateUser(user); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
h.updateReplyKeyboard(chatID)
|
|
||||||
text, kb := h.buildSettingsKeyboard(user)
|
text, kb := h.buildSettingsKeyboard(user)
|
||||||
edit := tgbotapi.NewEditMessageText(chatID, msgID, text)
|
edit := tgbotapi.NewEditMessageText(chatID, msgID, text)
|
||||||
edit.ReplyMarkup = &kb
|
edit.ReplyMarkup = &kb
|
||||||
@@ -1366,39 +1342,10 @@ func (h *Handler) SendDailyReport(userID int64, chatID int64) {
|
|||||||
}
|
}
|
||||||
text := h.buildDailyReport(user, day, events)
|
text := h.buildDailyReport(user, day, events)
|
||||||
reply := tgbotapi.NewMessage(chatID, text)
|
reply := tgbotapi.NewMessage(chatID, text)
|
||||||
reply.ReplyMarkup = h.smartKeyboard(chatID)
|
|
||||||
h.Bot.Send(reply)
|
h.Bot.Send(reply)
|
||||||
h.DB.MarkReportSent(user.ID, today)
|
h.DB.MarkReportSent(user.ID, today)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) smartKeyboard(chatID int64) tgbotapi.ReplyKeyboardMarkup {
|
|
||||||
user, err := h.getOrCreateUser(chatID)
|
|
||||||
if err != nil {
|
|
||||||
return defaultKeyboard()
|
|
||||||
}
|
|
||||||
last, err := h.DB.GetLastEvent(user.ID)
|
|
||||||
if err != nil || last == nil || last.EventType == "out" {
|
|
||||||
return tgbotapi.NewReplyKeyboard(
|
|
||||||
tgbotapi.NewKeyboardButtonRow(
|
|
||||||
tgbotapi.NewKeyboardButton("Clock In"),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
return tgbotapi.NewReplyKeyboard(
|
|
||||||
tgbotapi.NewKeyboardButtonRow(
|
|
||||||
tgbotapi.NewKeyboardButton("Clock Out"),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
func defaultKeyboard() tgbotapi.ReplyKeyboardMarkup {
|
|
||||||
return tgbotapi.NewReplyKeyboard(
|
|
||||||
tgbotapi.NewKeyboardButtonRow(
|
|
||||||
tgbotapi.NewKeyboardButton("Clock In"),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
func mainKeyboard() tgbotapi.InlineKeyboardMarkup {
|
func mainKeyboard() tgbotapi.InlineKeyboardMarkup {
|
||||||
return tgbotapi.NewInlineKeyboardMarkup(
|
return tgbotapi.NewInlineKeyboardMarkup(
|
||||||
tgbotapi.NewInlineKeyboardRow(
|
tgbotapi.NewInlineKeyboardRow(
|
||||||
@@ -1437,13 +1384,5 @@ func formatDuration(seconds int64) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) sendText(chatID int64, text string) {
|
func (h *Handler) sendText(chatID int64, text string) {
|
||||||
reply := tgbotapi.NewMessage(chatID, text)
|
h.Bot.Send(tgbotapi.NewMessage(chatID, text))
|
||||||
reply.ReplyMarkup = h.smartKeyboard(chatID)
|
|
||||||
h.Bot.Send(reply)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Handler) sendSuccessWithMenu(chatID int64, text string) {
|
|
||||||
reply := tgbotapi.NewMessage(chatID, text)
|
|
||||||
reply.ReplyMarkup = mainKeyboard()
|
|
||||||
h.Bot.Send(reply)
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user