added proper logging to most of the code

This commit is contained in:
watn3y 2023-09-07 03:24:31 +02:00
parent 65c6020e49
commit 7d4c9c5ab9
21 changed files with 218 additions and 283 deletions

View file

@ -2,7 +2,7 @@ package notify
import (
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"log"
"github.com/rs/zerolog/log"
"regexp"
"strconv"
"strings"
@ -11,6 +11,7 @@ import (
)
func Reminder(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
log.Debug().Str("args", update.Message.CommandArguments()).Msg("parsing new reminder")
commandArgs := strings.Fields(update.Message.CommandArguments())
var timeArg string
@ -20,6 +21,7 @@ func Reminder(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
timeArg = strings.ToLower(commandArgs[0])
textArg = strings.Replace(update.Message.CommandArguments(), timeArg, "", 1)
} else {
log.Error().Str("args", update.Message.CommandArguments()).Msg("invalid new reminder")
message := tgbotapi.MessageConfig{
BaseChat: tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID},
ParseMode: "html",
@ -30,11 +32,10 @@ func Reminder(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
return
}
log.Println("[notify] Attempting to set reminder for user " + strconv.FormatInt(update.Message.From.ID, 10) + " in chat " + strconv.FormatInt(update.Message.Chat.ID, 10) + " with arguments " + timeArg)
isValidFormat := regexp.MustCompile(`(?m)^\d{1,3}[mhd]$`)
if !isValidFormat.MatchString(timeArg) {
log.Error().Str("args", update.Message.CommandArguments()).Msg("invalid new reminder")
message := tgbotapi.MessageConfig{
BaseChat: tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID},
ParseMode: "html",
@ -64,9 +65,7 @@ func Reminder(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
notifyTime := time.Now().UTC().Add(time.Duration(number) * modifyTime)
var reminder reminderConfig
reminder = reminderConfig{
reminder := reminderConfig{
updateID: update.UpdateID,
notifyTime: notifyTime.Unix(),
chatID: update.Message.Chat.ID,
@ -75,6 +74,8 @@ func Reminder(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
reminderText: textArg,
}
log.Info().Int("ID", reminder.updateID).Int64("chat", reminder.chatID).Int64("user", reminder.userID).Int64("time", reminder.notifyTime).Msg("adding new reminder")
sqlAddReminder(reminder)
message := tgbotapi.MessageConfig{
@ -95,17 +96,19 @@ func NotifyHandler(bot *tgbotapi.BotAPI) {
for updateID, reminderTime := range reminders {
if reminderTime <= time.Now().UTC().Unix() {
log.Info().Int("ID", updateID).Msg("reminder is due")
details := sqlGetReminderDetails(updateID)
message := tgbotapi.MessageConfig{
BaseChat: tgbotapi.BaseChat{ChatID: details.chatID, ReplyToMessageID: details.messageToReplyToID},
ParseMode: "html",
DisableWebPagePreview: false,
Text: details.reminderText,
Text: "Reminder: " + details.reminderText,
}
log.Info().Int64("chat", details.chatID).Int64("user", details.userID).Str("text", details.reminderText).Msg("sent reminder")
botIO.SendMessage(message, bot)
sqlDeleteReminder(updateID)
}