Birthday implementation (mostly working!).
This commit is contained in:
parent
ab0c96b33f
commit
ff6aba11c6
2 changed files with 60 additions and 3 deletions
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
"github.com/robfig/cron/v3"
|
"github.com/robfig/cron/v3"
|
||||||
|
|
@ -59,7 +60,10 @@ func (a *App) loadCron() error {
|
||||||
a.settings.System.TimeZone,
|
a.settings.System.TimeZone,
|
||||||
a.settings.DailyScripture.Schedule), a.scripture)
|
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()
|
a.cron.Start()
|
||||||
fmt.Println("cron started")
|
fmt.Println("cron started")
|
||||||
|
|
||||||
|
|
@ -89,6 +93,58 @@ func (a *App) loadEnv() error {
|
||||||
return nil
|
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("**<u>Birthdays this week (Sunday-to-Sunday):</u>**\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("**<u>Today's Birthday")
|
||||||
|
if len(bdays) > 1 {
|
||||||
|
sb.WriteString("s")
|
||||||
|
}
|
||||||
|
sb.WriteString(":</u>**\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() {
|
func (a *App) scripture() {
|
||||||
fmt.Println("scripture send triggered")
|
fmt.Println("scripture send triggered")
|
||||||
fmt.Println("sending to chats:")
|
fmt.Println("sending to chats:")
|
||||||
|
|
|
||||||
|
|
@ -62,8 +62,9 @@ func (bm *BirthdayModel) BirthdaysThisWeek(chatid int64) ([]*Birthday, error) {
|
||||||
format('%02d', month), '-',
|
format('%02d', month), '-',
|
||||||
format('%02d', day))) as birthdate
|
format('%02d', day))) as birthdate
|
||||||
from birthdays where
|
from birthdays where
|
||||||
chatid = ? and (
|
chatid = ? and
|
||||||
birthdate >= date('now', 'localtime', 'weekday 0') 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;`
|
birthdate <= date('now', 'localtime', 'weekday 0', '+7 days')) order by birthdate asc;`
|
||||||
err := bm.db.SelectContext(bm.ctx, &bdays, q, chatid)
|
err := bm.db.SelectContext(bm.ctx, &bdays, q, chatid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue