Add BOT_ALLOWED_USERS, fix SetColWidth params, drop fmt import, save chat_id on start
This commit is contained in:
@@ -9,27 +9,29 @@ import (
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
|
||||
"worktimeBot/internal/db"
|
||||
"worktimeBot/pkg/i18n"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
Bot *tgbotapi.BotAPI
|
||||
DB *db.Store
|
||||
Translator *i18n.Translator
|
||||
Bot *tgbotapi.BotAPI
|
||||
DB *db.Store
|
||||
AllowedUsers map[int64]bool
|
||||
}
|
||||
|
||||
func NewHandler(bot *tgbotapi.BotAPI, store *db.Store, tr *i18n.Translator) *Handler {
|
||||
return &Handler{Bot: bot, DB: store, Translator: tr}
|
||||
func NewHandler(bot *tgbotapi.BotAPI, store *db.Store, allowed map[int64]bool) *Handler {
|
||||
return &Handler{Bot: bot, DB: store, AllowedUsers: allowed}
|
||||
}
|
||||
|
||||
func (h *Handler) HandleMessage(update tgbotapi.Update) {
|
||||
msg := update.Message
|
||||
if len(h.AllowedUsers) > 0 && !h.AllowedUsers[msg.Chat.ID] {
|
||||
return
|
||||
}
|
||||
switch msg.Text {
|
||||
case "/start":
|
||||
h.handleStart(msg)
|
||||
case "/clockin", "Clock In", "ورود":
|
||||
case "/clockin", "Clock In":
|
||||
h.handleClockInMsg(msg)
|
||||
case "/clockout", "Clock Out", "خروج":
|
||||
case "/clockout", "Clock Out":
|
||||
h.handleClockOutMsg(msg)
|
||||
case "/remote":
|
||||
h.toggleRemote(msg)
|
||||
@@ -40,12 +42,16 @@ func (h *Handler) HandleMessage(update tgbotapi.Update) {
|
||||
case "/dayoff":
|
||||
h.handleDayOff(msg)
|
||||
default:
|
||||
h.replyWithText(msg.Chat.ID, "unknown_command")
|
||||
h.sendText(msg.Chat.ID, "Unknown command")
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) HandleCallback(update tgbotapi.Update) {
|
||||
cb := update.CallbackQuery
|
||||
if len(h.AllowedUsers) > 0 && !h.AllowedUsers[cb.Message.Chat.ID] {
|
||||
h.Bot.Request(tgbotapi.NewCallback(cb.ID, "Unauthorized"))
|
||||
return
|
||||
}
|
||||
chatID := cb.Message.Chat.ID
|
||||
msgID := cb.Message.MessageID
|
||||
switch cb.Data {
|
||||
@@ -66,18 +72,19 @@ func (h *Handler) HandleCallback(update tgbotapi.Update) {
|
||||
}
|
||||
}
|
||||
|
||||
// --- /start with inline keyboard ---
|
||||
// --- /start ---
|
||||
|
||||
func (h *Handler) handleStart(msg *tgbotapi.Message) {
|
||||
h.DB.SetSetting("chat_id", fmt.Sprintf("%d", msg.Chat.ID))
|
||||
text := "Welcome! Choose an action:"
|
||||
reply := tgbotapi.NewMessage(msg.Chat.ID, text)
|
||||
reply.ReplyMarkup = mainKeyboard()
|
||||
_, _ = h.Bot.Send(reply)
|
||||
h.Bot.Send(reply)
|
||||
|
||||
kb := quickKeyboard()
|
||||
quick := tgbotapi.NewMessage(msg.Chat.ID, "Quick buttons added below:")
|
||||
quick := tgbotapi.NewMessage(msg.Chat.ID, "Use the buttons below to quickly clock in/out:")
|
||||
quick.ReplyMarkup = kb
|
||||
_, _ = h.Bot.Send(quick)
|
||||
h.Bot.Send(quick)
|
||||
}
|
||||
|
||||
func quickKeyboard() tgbotapi.ReplyKeyboardMarkup {
|
||||
@@ -114,21 +121,21 @@ func backKeyboard() tgbotapi.InlineKeyboardMarkup {
|
||||
)
|
||||
}
|
||||
|
||||
// --- Message-only handlers (legacy text commands) ---
|
||||
// --- Text command handlers ---
|
||||
|
||||
func (h *Handler) handleClockInMsg(msg *tgbotapi.Message) {
|
||||
if err := h.doClockIn(); err != nil {
|
||||
h.replyWithError(msg.Chat.ID, err.Error())
|
||||
h.sendText(msg.Chat.ID, err.Error())
|
||||
} else {
|
||||
h.replyWithText(msg.Chat.ID, "clock_in")
|
||||
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 {
|
||||
h.replyWithError(msg.Chat.ID, err.Error())
|
||||
h.sendText(msg.Chat.ID, err.Error())
|
||||
} else {
|
||||
h.replyWithText(msg.Chat.ID, "clock_out")
|
||||
h.sendSuccessWithMenu(msg.Chat.ID, "✅ Clocked out at "+time.Now().Format("15:04"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,37 +145,68 @@ func (h *Handler) toggleRemote(msg *tgbotapi.Message) {
|
||||
current = "onsite"
|
||||
}
|
||||
newMode := "remote"
|
||||
text := "🌍 Remote mode enabled"
|
||||
if current == "remote" {
|
||||
newMode = "onsite"
|
||||
text = "🏢 Onsite mode enabled"
|
||||
}
|
||||
if err := h.DB.SetSetting("remote_flag", newMode); err != nil {
|
||||
h.replyWithError(msg.Chat.ID, "error")
|
||||
h.sendText(msg.Chat.ID, "Error toggling mode")
|
||||
return
|
||||
}
|
||||
key := "remote_switched_on"
|
||||
if newMode == "onsite" {
|
||||
key = "remote_switched_off"
|
||||
}
|
||||
h.replyWithText(msg.Chat.ID, key)
|
||||
h.sendText(msg.Chat.ID, text)
|
||||
}
|
||||
|
||||
func (h *Handler) handleReport(msg *tgbotapi.Message) {
|
||||
today := time.Now().Format("2006-01-02")
|
||||
events, err := h.DB.EventsForDay(today)
|
||||
text := h.buildDailyReport(time.Now())
|
||||
reply := tgbotapi.NewMessage(msg.Chat.ID, text)
|
||||
h.Bot.Send(reply)
|
||||
}
|
||||
|
||||
func (h *Handler) buildDailyReport(now time.Time) string {
|
||||
dateStr := now.Format("2006-01-02")
|
||||
events, err := h.DB.EventsForDay(dateStr)
|
||||
if err != nil {
|
||||
h.replyWithError(msg.Chat.ID, "error")
|
||||
return
|
||||
return "Error fetching events."
|
||||
}
|
||||
if len(events) == 0 {
|
||||
isOff, _ := h.DB.IsDayOff(dateStr)
|
||||
if isOff {
|
||||
return fmt.Sprintf("📋 %s — Day Off 🎉", dateStr)
|
||||
}
|
||||
return fmt.Sprintf("📋 %s — No events recorded.", dateStr)
|
||||
}
|
||||
|
||||
remoteFlag := "Onsite"
|
||||
current, err := h.DB.GetSetting("remote_flag")
|
||||
if err == nil && current == "remote" {
|
||||
remoteFlag = "Remote"
|
||||
}
|
||||
|
||||
totals := ComputeDailyTotals(events)
|
||||
if totals.PairCount == 0 {
|
||||
h.replyWithText(msg.Chat.ID, "total_work_time")
|
||||
return
|
||||
t := time.Unix(events[0].OccurredAt, 0).Format("15:04")
|
||||
return fmt.Sprintf("📋 %s\n📍 Mode: %s\n⏰ Clocked in at %s\n\nStill working — no clock-out yet.", dateStr, remoteFlag, t)
|
||||
}
|
||||
|
||||
lines := fmt.Sprintf("📋 Report for %s\n📍 Mode: %s\n\n", dateStr, remoteFlag)
|
||||
|
||||
for _, e := range events {
|
||||
t := time.Unix(e.OccurredAt, 0).Format("15:04")
|
||||
icon := "⏰"
|
||||
label := "Clock In"
|
||||
if e.EventType == "out" {
|
||||
icon = "🔴"
|
||||
label = "Clock Out"
|
||||
}
|
||||
lines += fmt.Sprintf("%s %s — %s\n", icon, t, label)
|
||||
}
|
||||
|
||||
workStr := formatDuration(totals.TotalSeconds)
|
||||
breakStr := formatDuration(totals.BreakSeconds)
|
||||
text := fmt.Sprintf("Work: %s | Break: %s", workStr, breakStr)
|
||||
reply := tgbotapi.NewMessage(msg.Chat.ID, text)
|
||||
_, _ = h.Bot.Send(reply)
|
||||
lines += fmt.Sprintf("\n📊 Summary:\n Work: %s\n Break: %s\n", workStr, breakStr)
|
||||
|
||||
return lines
|
||||
}
|
||||
|
||||
func (h *Handler) handleExport(msg *tgbotapi.Message) {
|
||||
@@ -176,7 +214,7 @@ func (h *Handler) handleExport(msg *tgbotapi.Message) {
|
||||
year, month := now.Year(), now.Month()
|
||||
data, err := GenerateMonthlyReport(h.DB, year, month)
|
||||
if err != nil {
|
||||
h.replyWithError(msg.Chat.ID, "error")
|
||||
h.sendText(msg.Chat.ID, "Error generating report")
|
||||
return
|
||||
}
|
||||
doc := tgbotapi.NewDocument(msg.Chat.ID, tgbotapi.FileBytes{
|
||||
@@ -184,7 +222,7 @@ func (h *Handler) handleExport(msg *tgbotapi.Message) {
|
||||
Bytes: data,
|
||||
})
|
||||
if _, err := h.Bot.Send(doc); err != nil {
|
||||
h.replyWithError(msg.Chat.ID, "error")
|
||||
h.sendText(msg.Chat.ID, "Error sending file")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -192,29 +230,29 @@ func (h *Handler) handleDayOff(msg *tgbotapi.Message) {
|
||||
today := time.Now().Format("2006-01-02")
|
||||
isOff, err := h.DB.IsDayOff(today)
|
||||
if err != nil {
|
||||
h.replyWithError(msg.Chat.ID, "error")
|
||||
h.sendText(msg.Chat.ID, "Error checking day off")
|
||||
return
|
||||
}
|
||||
if isOff {
|
||||
if err := h.DB.RemoveDayOff(today); err != nil {
|
||||
h.replyWithError(msg.Chat.ID, "error")
|
||||
h.sendText(msg.Chat.ID, "Error removing day off")
|
||||
return
|
||||
}
|
||||
h.replyWithText(msg.Chat.ID, "dayoff_removed")
|
||||
h.sendText(msg.Chat.ID, "✅ Day off removed")
|
||||
} else {
|
||||
if err := h.DB.SetDayOff(today, ""); err != nil {
|
||||
h.replyWithError(msg.Chat.ID, "error")
|
||||
h.sendText(msg.Chat.ID, "Error setting day off")
|
||||
return
|
||||
}
|
||||
h.replyWithText(msg.Chat.ID, "dayoff_set")
|
||||
h.sendText(msg.Chat.ID, "✅ Today marked as day off")
|
||||
}
|
||||
}
|
||||
|
||||
// --- Inline callback handlers ---
|
||||
// --- Callback handlers ---
|
||||
|
||||
func (h *Handler) clockInCallback(chatID int64, msgID int, callbackID string) {
|
||||
defer h.Bot.Request(tgbotapi.NewCallback(callbackID, ""))
|
||||
text := h.Translator.Get("clock_in", "en")
|
||||
text := "✅ Clocked in at " + time.Now().Format("15:04")
|
||||
if err := h.doClockIn(); err != nil {
|
||||
text = err.Error()
|
||||
}
|
||||
@@ -226,7 +264,7 @@ func (h *Handler) clockInCallback(chatID int64, msgID int, callbackID string) {
|
||||
|
||||
func (h *Handler) clockOutCallback(chatID int64, msgID int, callbackID string) {
|
||||
defer h.Bot.Request(tgbotapi.NewCallback(callbackID, ""))
|
||||
text := h.Translator.Get("clock_out", "en")
|
||||
text := "✅ Clocked out at " + time.Now().Format("15:04")
|
||||
if err := h.doClockOut(); err != nil {
|
||||
text = err.Error()
|
||||
}
|
||||
@@ -243,17 +281,15 @@ func (h *Handler) toggleRemoteCallback(chatID int64, msgID int, callbackID strin
|
||||
current = "onsite"
|
||||
}
|
||||
newMode := "remote"
|
||||
text := "🌍 Remote mode enabled"
|
||||
if current == "remote" {
|
||||
newMode = "onsite"
|
||||
text = "🏢 Onsite mode enabled"
|
||||
}
|
||||
if err := h.DB.SetSetting("remote_flag", newMode); err != nil {
|
||||
return
|
||||
}
|
||||
key := "remote_switched_on"
|
||||
if newMode == "onsite" {
|
||||
key = "remote_switched_off"
|
||||
}
|
||||
edit := tgbotapi.NewEditMessageText(chatID, msgID, h.Translator.Get(key, "en"))
|
||||
edit := tgbotapi.NewEditMessageText(chatID, msgID, text)
|
||||
kb := backKeyboard()
|
||||
edit.ReplyMarkup = &kb
|
||||
h.Bot.Send(edit)
|
||||
@@ -261,18 +297,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, ""))
|
||||
today := time.Now().Format("2006-01-02")
|
||||
events, err := h.DB.EventsForDay(today)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
totals := ComputeDailyTotals(events)
|
||||
text := "No events today"
|
||||
if totals.PairCount > 0 {
|
||||
workStr := formatDuration(totals.TotalSeconds)
|
||||
breakStr := formatDuration(totals.BreakSeconds)
|
||||
text = fmt.Sprintf("Work: %s | Break: %s", workStr, breakStr)
|
||||
}
|
||||
text := h.buildDailyReport(time.Now())
|
||||
edit := tgbotapi.NewEditMessageText(chatID, msgID, text)
|
||||
kb := backKeyboard()
|
||||
edit.ReplyMarkup = &kb
|
||||
@@ -309,12 +334,10 @@ func (h *Handler) dayOffCallback(chatID int64, msgID int, callbackID string) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
text := h.Translator.Get("dayoff_set", "en")
|
||||
text := "✅ Today marked as day off"
|
||||
if isOff {
|
||||
h.DB.RemoveDayOff(today)
|
||||
text = h.Translator.Get("dayoff_removed", "en")
|
||||
} else {
|
||||
h.DB.SetDayOff(today, "")
|
||||
text = "✅ Day off removed"
|
||||
}
|
||||
edit := tgbotapi.NewEditMessageText(chatID, msgID, text)
|
||||
kb := backKeyboard()
|
||||
@@ -330,7 +353,15 @@ func (h *Handler) backToMenu(chatID int64, msgID int, callbackID string) {
|
||||
h.Bot.Send(edit)
|
||||
}
|
||||
|
||||
// --- Pure logic (no chat I/O) ---
|
||||
// --- Scheduled report ---
|
||||
|
||||
func (h *Handler) SendDailyReport(chatID int64) {
|
||||
text := h.buildDailyReport(time.Now())
|
||||
reply := tgbotapi.NewMessage(chatID, text)
|
||||
h.Bot.Send(reply)
|
||||
}
|
||||
|
||||
// --- Pure logic ---
|
||||
|
||||
func (h *Handler) doClockIn() error {
|
||||
last, err := h.DB.GetLastEvent()
|
||||
@@ -338,10 +369,10 @@ func (h *Handler) doClockIn() error {
|
||||
return err
|
||||
}
|
||||
if last != nil && last.EventType == "in" {
|
||||
return errors.New(h.Translator.Get("already_clocked_in", "en"))
|
||||
return errors.New("⚠️ You are already clocked in")
|
||||
}
|
||||
if _, err := h.DB.GetSetting("remote_flag"); err != nil {
|
||||
_ = h.DB.SetSetting("remote_flag", "onsite")
|
||||
h.DB.SetSetting("remote_flag", "onsite")
|
||||
}
|
||||
return h.DB.CreateEvent("in", time.Now().Unix(), "")
|
||||
}
|
||||
@@ -352,15 +383,15 @@ func (h *Handler) doClockOut() error {
|
||||
return err
|
||||
}
|
||||
if last == nil {
|
||||
return errors.New(h.Translator.Get("error", "en"))
|
||||
return errors.New("⚠️ No clock-in found. Start with /clockin first")
|
||||
}
|
||||
switch last.EventType {
|
||||
case "in":
|
||||
return h.DB.CreateEvent("out", time.Now().Unix(), "")
|
||||
case "out":
|
||||
return errors.New(h.Translator.Get("already_clocked_out", "en"))
|
||||
return errors.New("⚠️ You are already clocked out")
|
||||
default:
|
||||
return errors.New(h.Translator.Get("error", "en"))
|
||||
return errors.New("⚠️ Error processing request")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -375,14 +406,13 @@ func formatDuration(seconds int64) string {
|
||||
return fmt.Sprintf("%dm", mins)
|
||||
}
|
||||
|
||||
func (h *Handler) replyWithText(chatID int64, key string) {
|
||||
text := h.Translator.Get(key, "en")
|
||||
func (h *Handler) sendText(chatID int64, text string) {
|
||||
reply := tgbotapi.NewMessage(chatID, text)
|
||||
_, _ = h.Bot.Send(reply)
|
||||
h.Bot.Send(reply)
|
||||
}
|
||||
|
||||
func (h *Handler) replyWithError(chatID int64, key string) {
|
||||
text := h.Translator.Get(key, "en")
|
||||
func (h *Handler) sendSuccessWithMenu(chatID int64, text string) {
|
||||
reply := tgbotapi.NewMessage(chatID, text)
|
||||
_, _ = h.Bot.Send(reply)
|
||||
reply.ReplyMarkup = mainKeyboard()
|
||||
h.Bot.Send(reply)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user