Clean up: remove unused packages, switch to slog, tidy Makefile

This commit is contained in:
2026-06-23 22:34:01 +03:30
parent 32446a99d1
commit b4fab29d45
7 changed files with 122 additions and 60 deletions

View File

@@ -4,6 +4,7 @@ import (
"database/sql"
"errors"
"fmt"
"log/slog"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
@@ -24,8 +25,10 @@ func NewHandler(bot *tgbotapi.BotAPI, store *db.Store, allowed map[int64]bool) *
func (h *Handler) HandleMessage(update tgbotapi.Update) {
msg := update.Message
if len(h.AllowedUsers) > 0 && !h.AllowedUsers[msg.Chat.ID] {
slog.Warn("unauthorized message", "chat_id", msg.Chat.ID, "user", msg.From.UserName, "text", msg.Text)
return
}
slog.Info("message", "chat_id", msg.Chat.ID, "text", msg.Text)
switch msg.Text {
case "/start":
h.handleStart(msg)
@@ -49,9 +52,11 @@ func (h *Handler) HandleMessage(update tgbotapi.Update) {
func (h *Handler) HandleCallback(update tgbotapi.Update) {
cb := update.CallbackQuery
if len(h.AllowedUsers) > 0 && !h.AllowedUsers[cb.Message.Chat.ID] {
slog.Warn("unauthorized callback", "chat_id", cb.Message.Chat.ID, "data", cb.Data)
h.Bot.Request(tgbotapi.NewCallback(cb.ID, "Unauthorized"))
return
}
slog.Info("callback", "chat_id", cb.Message.Chat.ID, "data", cb.Data)
chatID := cb.Message.Chat.ID
msgID := cb.Message.MessageID
switch cb.Data {
@@ -76,15 +81,11 @@ func (h *Handler) HandleCallback(update tgbotapi.Update) {
func (h *Handler) handleStart(msg *tgbotapi.Message) {
h.DB.SetSetting("chat_id", fmt.Sprintf("%d", msg.Chat.ID))
slog.Info("start", "chat_id", msg.Chat.ID)
text := "Welcome! Choose an action:"
reply := tgbotapi.NewMessage(msg.Chat.ID, text)
reply.ReplyMarkup = mainKeyboard()
h.Bot.Send(reply)
kb := quickKeyboard()
quick := tgbotapi.NewMessage(msg.Chat.ID, "—")
quick.ReplyMarkup = kb
h.Bot.Send(quick)
}
func quickKeyboard() tgbotapi.ReplyKeyboardMarkup {
@@ -125,16 +126,20 @@ func backKeyboard() tgbotapi.InlineKeyboardMarkup {
func (h *Handler) handleClockInMsg(msg *tgbotapi.Message) {
if err := h.doClockIn(); err != nil {
slog.Error("clock in failed", "chat_id", msg.Chat.ID, "error", err)
h.sendText(msg.Chat.ID, err.Error())
} else {
slog.Info("clocked in", "chat_id", msg.Chat.ID)
h.sendSuccessWithMenu(msg.Chat.ID, "✅ Clocked in at "+time.Now().Format("15:04"))
}
}
func (h *Handler) handleClockOutMsg(msg *tgbotapi.Message) {
if err := h.doClockOut(); err != nil {
slog.Error("clock out failed", "chat_id", msg.Chat.ID, "error", err)
h.sendText(msg.Chat.ID, err.Error())
} else {
slog.Info("clocked out", "chat_id", msg.Chat.ID)
h.sendSuccessWithMenu(msg.Chat.ID, "✅ Clocked out at "+time.Now().Format("15:04"))
}
}
@@ -151,14 +156,17 @@ func (h *Handler) toggleRemote(msg *tgbotapi.Message) {
text = "🏢 Onsite mode enabled"
}
if err := h.DB.SetSetting("remote_flag", newMode); err != nil {
slog.Error("toggle remote", "error", err)
h.sendText(msg.Chat.ID, "Error toggling mode")
return
}
slog.Info("remote toggled", "mode", newMode)
h.sendText(msg.Chat.ID, text)
}
func (h *Handler) handleReport(msg *tgbotapi.Message) {
text := h.buildDailyReport(time.Now())
slog.Info("report", "chat_id", msg.Chat.ID)
reply := tgbotapi.NewMessage(msg.Chat.ID, text)
h.Bot.Send(reply)
}
@@ -212,16 +220,20 @@ func (h *Handler) buildDailyReport(now time.Time) string {
func (h *Handler) handleExport(msg *tgbotapi.Message) {
now := time.Now()
year, month := now.Year(), now.Month()
slog.Info("export requested", "year", year, "month", month)
data, err := GenerateMonthlyReport(h.DB, year, month)
if err != nil {
slog.Error("generate report", "error", err)
h.sendText(msg.Chat.ID, "Error generating report")
return
}
slog.Info("report generated", "bytes", len(data))
doc := tgbotapi.NewDocument(msg.Chat.ID, tgbotapi.FileBytes{
Name: fmt.Sprintf("worktime_%s.xlsx", now.Format("2006_01")),
Bytes: data,
})
if _, err := h.Bot.Send(doc); err != nil {
slog.Error("send document", "error", err)
h.sendText(msg.Chat.ID, "Error sending file")
}
}
@@ -230,20 +242,25 @@ func (h *Handler) handleDayOff(msg *tgbotapi.Message) {
today := time.Now().Format("2006-01-02")
isOff, err := h.DB.IsDayOff(today)
if err != nil {
slog.Error("day off check", "error", err)
h.sendText(msg.Chat.ID, "Error checking day off")
return
}
if isOff {
if err := h.DB.RemoveDayOff(today); err != nil {
slog.Error("remove day off", "error", err)
h.sendText(msg.Chat.ID, "Error removing day off")
return
}
slog.Info("day off removed")
h.sendText(msg.Chat.ID, "✅ Day off removed")
} else {
if err := h.DB.SetDayOff(today, ""); err != nil {
slog.Error("set day off", "error", err)
h.sendText(msg.Chat.ID, "Error setting day off")
return
}
slog.Info("day off set")
h.sendText(msg.Chat.ID, "✅ Today marked as day off")
}
}
@@ -255,6 +272,9 @@ func (h *Handler) clockInCallback(chatID int64, msgID int, callbackID string) {
text := "✅ Clocked in at " + time.Now().Format("15:04")
if err := h.doClockIn(); err != nil {
text = err.Error()
slog.Error("clock in failed", "chat_id", chatID, "error", err)
} else {
slog.Info("clocked in", "chat_id", chatID)
}
edit := tgbotapi.NewEditMessageText(chatID, msgID, text)
kb := backKeyboard()
@@ -267,6 +287,9 @@ func (h *Handler) clockOutCallback(chatID int64, msgID int, callbackID string) {
text := "✅ Clocked out at " + time.Now().Format("15:04")
if err := h.doClockOut(); err != nil {
text = err.Error()
slog.Error("clock out failed", "chat_id", chatID, "error", err)
} else {
slog.Info("clocked out", "chat_id", chatID)
}
edit := tgbotapi.NewEditMessageText(chatID, msgID, text)
kb := backKeyboard()
@@ -287,8 +310,10 @@ func (h *Handler) toggleRemoteCallback(chatID int64, msgID int, callbackID strin
text = "🏢 Onsite mode enabled"
}
if err := h.DB.SetSetting("remote_flag", newMode); err != nil {
slog.Error("toggle remote", "error", err)
return
}
slog.Info("remote toggled", "mode", newMode)
edit := tgbotapi.NewEditMessageText(chatID, msgID, text)
kb := backKeyboard()
edit.ReplyMarkup = &kb
@@ -298,6 +323,7 @@ func (h *Handler) toggleRemoteCallback(chatID int64, msgID int, callbackID strin
func (h *Handler) reportCallback(chatID int64, msgID int, callbackID string) {
defer h.Bot.Request(tgbotapi.NewCallback(callbackID, ""))
text := h.buildDailyReport(time.Now())
slog.Info("report", "chat_id", chatID)
edit := tgbotapi.NewEditMessageText(chatID, msgID, text)
kb := backKeyboard()
edit.ReplyMarkup = &kb
@@ -308,14 +334,17 @@ func (h *Handler) exportCallback(chatID int64, msgID int, callbackID string) {
defer h.Bot.Request(tgbotapi.NewCallback(callbackID, ""))
now := time.Now()
year, month := now.Year(), now.Month()
slog.Info("export", "chat_id", chatID, "year", year, "month", month)
data, err := GenerateMonthlyReport(h.DB, year, month)
if err != nil {
slog.Error("generate report", "error", err)
edit := tgbotapi.NewEditMessageText(chatID, msgID, "Error generating report")
kb := backKeyboard()
edit.ReplyMarkup = &kb
h.Bot.Send(edit)
return
}
slog.Info("report generated", "bytes", len(data))
edit := tgbotapi.NewEditMessageText(chatID, msgID, "Here is your report:")
kb := backKeyboard()
edit.ReplyMarkup = &kb
@@ -324,7 +353,9 @@ func (h *Handler) exportCallback(chatID int64, msgID int, callbackID string) {
Name: fmt.Sprintf("worktime_%s.xlsx", now.Format("2006_01")),
Bytes: data,
})
h.Bot.Send(doc)
if _, err := h.Bot.Send(doc); err != nil {
slog.Error("send document", "error", err)
}
}
func (h *Handler) dayOffCallback(chatID int64, msgID int, callbackID string) {
@@ -332,12 +363,16 @@ func (h *Handler) dayOffCallback(chatID int64, msgID int, callbackID string) {
today := time.Now().Format("2006-01-02")
isOff, err := h.DB.IsDayOff(today)
if err != nil {
slog.Error("day off check", "error", err)
return
}
text := "✅ Today marked as day off"
if isOff {
h.DB.RemoveDayOff(today)
text = "✅ Day off removed"
slog.Info("day off removed")
} else {
slog.Info("day off set")
}
edit := tgbotapi.NewEditMessageText(chatID, msgID, text)
kb := backKeyboard()
@@ -358,6 +393,7 @@ func (h *Handler) backToMenu(chatID int64, msgID int, callbackID string) {
func (h *Handler) SendDailyReport(chatID int64) {
text := h.buildDailyReport(time.Now())
reply := tgbotapi.NewMessage(chatID, text)
reply.ReplyMarkup = quickKeyboard()
h.Bot.Send(reply)
}
@@ -408,6 +444,7 @@ func formatDuration(seconds int64) string {
func (h *Handler) sendText(chatID int64, text string) {
reply := tgbotapi.NewMessage(chatID, text)
reply.ReplyMarkup = quickKeyboard()
h.Bot.Send(reply)
}