This commit is contained in:
parent
21e4c434fd
commit
72c6dd6982
12 changed files with 695 additions and 48 deletions
|
@ -17,10 +17,12 @@ import (
|
|||
"git.nakama.town/fmartingr/butterrobot/internal/admin"
|
||||
"git.nakama.town/fmartingr/butterrobot/internal/config"
|
||||
"git.nakama.town/fmartingr/butterrobot/internal/db"
|
||||
"git.nakama.town/fmartingr/butterrobot/internal/model"
|
||||
"git.nakama.town/fmartingr/butterrobot/internal/platform"
|
||||
"git.nakama.town/fmartingr/butterrobot/internal/plugin"
|
||||
"git.nakama.town/fmartingr/butterrobot/internal/plugin/fun"
|
||||
"git.nakama.town/fmartingr/butterrobot/internal/plugin/ping"
|
||||
"git.nakama.town/fmartingr/butterrobot/internal/plugin/reminder"
|
||||
"git.nakama.town/fmartingr/butterrobot/internal/plugin/social"
|
||||
"git.nakama.town/fmartingr/butterrobot/internal/queue"
|
||||
)
|
||||
|
@ -86,12 +88,19 @@ func (a *App) Run() error {
|
|||
plugin.Register(social.NewTwitterExpander())
|
||||
plugin.Register(social.NewInstagramExpander())
|
||||
|
||||
// Register reminder plugin
|
||||
reminderPlugin := reminder.New(a.db)
|
||||
plugin.Register(reminderPlugin)
|
||||
|
||||
// Initialize routes
|
||||
a.initializeRoutes()
|
||||
|
||||
// Start message queue worker
|
||||
a.queue.Start(a.handleMessage)
|
||||
|
||||
// Start reminder scheduler
|
||||
a.queue.StartReminderScheduler(a.handleReminder)
|
||||
|
||||
// Create server
|
||||
addr := fmt.Sprintf(":%s", a.config.Port)
|
||||
srv := &http.Server{
|
||||
|
@ -304,3 +313,73 @@ func (a *App) handleMessage(item queue.Item) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// handleReminder handles reminder processing
|
||||
func (a *App) handleReminder(reminder *model.Reminder) {
|
||||
// When called with nil, it means we should check for pending reminders
|
||||
if reminder == nil {
|
||||
// Get pending reminders
|
||||
reminders, err := a.db.GetPendingReminders()
|
||||
if err != nil {
|
||||
a.logger.Error("Error getting pending reminders", "error", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Process each reminder
|
||||
for _, r := range reminders {
|
||||
a.processReminder(r)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Otherwise, process the specific reminder
|
||||
a.processReminder(reminder)
|
||||
}
|
||||
|
||||
// processReminder processes an individual reminder
|
||||
func (a *App) processReminder(reminder *model.Reminder) {
|
||||
a.logger.Info("Processing reminder",
|
||||
"id", reminder.ID,
|
||||
"platform", reminder.Platform,
|
||||
"channel", reminder.ChannelID,
|
||||
"trigger_at", reminder.TriggerAt,
|
||||
)
|
||||
|
||||
// Get the platform handler
|
||||
p, err := platform.Get(reminder.Platform)
|
||||
if err != nil {
|
||||
a.logger.Error("Error getting platform for reminder", "error", err, "platform", reminder.Platform)
|
||||
return
|
||||
}
|
||||
|
||||
// Get the channel
|
||||
channel, err := a.db.GetChannelByPlatform(reminder.Platform, reminder.ChannelID)
|
||||
if err != nil {
|
||||
a.logger.Error("Error getting channel for reminder", "error", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Create the reminder message
|
||||
reminderText := fmt.Sprintf("@%s reminding you of this", reminder.Username)
|
||||
|
||||
message := &model.Message{
|
||||
Text: reminderText,
|
||||
Chat: reminder.ChannelID,
|
||||
Channel: channel,
|
||||
Author: "bot",
|
||||
FromBot: true,
|
||||
Date: time.Now(),
|
||||
ReplyTo: reminder.ReplyToID, // Reply to the original message
|
||||
}
|
||||
|
||||
// Send the reminder message
|
||||
if err := p.SendMessage(message); err != nil {
|
||||
a.logger.Error("Error sending reminder", "error", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Mark the reminder as processed
|
||||
if err := a.db.MarkReminderAsProcessed(reminder.ID); err != nil {
|
||||
a.logger.Error("Error marking reminder as processed", "error", err)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue