From ff6aba11c6ac3e7c468cba3ef04ecc1ee058a35b Mon Sep 17 00:00:00 2001 From: "Benjamin A. Shelton" Date: Fri, 23 Oct 2026 23:26:48 -0600 Subject: [PATCH] Birthday implementation (mostly working!). --- internal/app/app.go | 58 +++++++++++++++++++++++++++++++++++- internal/models/birthdays.go | 5 ++-- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/internal/app/app.go b/internal/app/app.go index 4dd4248..b13caef 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "strings" + "time" "github.com/joho/godotenv" "github.com/robfig/cron/v3" @@ -59,7 +60,10 @@ func (a *App) loadCron() error { a.settings.System.TimeZone, a.settings.DailyScripture.Schedule), a.scripture) - fmt.Println("using schedule:", fmt.Sprintf("TZ=%s %s", a.settings.System.TimeZone, a.settings.DailyScripture.Schedule)) + a.cron.AddFunc(fmt.Sprintf("TZ=%s %s", + a.settings.System.TimeZone, + a.settings.Scheduler.Announcements.Daily), a.daily) + a.cron.Start() fmt.Println("cron started") @@ -89,6 +93,58 @@ func (a *App) loadEnv() error { return nil } +func (a *App) daily() { + fmt.Println("daily trigger") + + chats, err := a.db.Chats().ListChats() + if err != nil { + a.logger.Error("unable to retrieve chats:", err) + return + } + + sb := &strings.Builder{} + for _, chat := range chats { + if time.Now().Format("Monday") == a.settings.Scheduler.Announcements.Weekly { + bdays, err := a.db.Birthdays().BirthdaysThisWeek(chat.ID) + if err == nil && len(bdays) > 0 { + sb.WriteString("**Birthdays this week (Sunday-to-Sunday):**\n\n") + for _, bday := range bdays { + d, derr := time.Parse("2006-01-02", bday.Birthdate) + if derr == nil { + sb.WriteString(" * ") + sb.WriteString(bday.Name) + sb.WriteString(" (") + sb.WriteString(d.Format("Mon, January _2")) + sb.WriteString(")\n") + } else { + a.logger.Error("cannot parse birthday:", bday.Birthdate) + } + } + sb.WriteByte('\n') + } else { + a.logger.Error("could not retrieve this week's birthdays:", err) + } + } + bdays, err := a.db.Birthdays().BirthdaysToday(chat.ID) + if err == nil && len(bdays) > 0 { + sb.WriteString("**Today's Birthday") + if len(bdays) > 1 { + sb.WriteString("s") + } + sb.WriteString(":**\n\n") + for _, bday := range bdays { + sb.WriteString(" * ") + sb.WriteString(bday.Name) + sb.WriteString("\n") + } + sb.WriteString("\n🎉🎊🎈Happy Birthday!🎉🎊🎈") + } else { + a.logger.Error("could not retrieve this week's birthdays:", err) + } + a.bot.SendMarkdownMessage(chat.ID, sb.String()) + } +} + func (a *App) scripture() { fmt.Println("scripture send triggered") fmt.Println("sending to chats:") diff --git a/internal/models/birthdays.go b/internal/models/birthdays.go index 6ea6adb..a7bbad5 100644 --- a/internal/models/birthdays.go +++ b/internal/models/birthdays.go @@ -62,8 +62,9 @@ func (bm *BirthdayModel) BirthdaysThisWeek(chatid int64) ([]*Birthday, error) { format('%02d', month), '-', format('%02d', day))) as birthdate from birthdays where - chatid = ? and ( - birthdate >= date('now', 'localtime', 'weekday 0') and + chatid = ? and + birthdate != date('now', 'localtime') and + (birthdate >= date('now', 'localtime', 'weekday 0') and birthdate <= date('now', 'localtime', 'weekday 0', '+7 days')) order by birthdate asc;` err := bm.db.SelectContext(bm.ctx, &bdays, q, chatid) if err != nil {