From bcf37c1bc94ad592cec42695455f2bbcb90dcf09 Mon Sep 17 00:00:00 2001 From: cubicroot Date: Mon, 29 Apr 2024 20:07:41 +0200 Subject: [PATCH] add list commands action --- internal/cmd/run.go | 1 + .../matrix/actions/message/list_commands.go | 152 ++++++++++++++++++ .../matrix/actions/reaction/add_time.go | 2 +- .../connectors/matrix/database/interface.go | 1 + 4 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 internal/connectors/matrix/actions/message/list_commands.go diff --git a/internal/cmd/run.go b/internal/cmd/run.go index d7eb762c..3a3574ba 100644 --- a/internal/cmd/run.go +++ b/internal/cmd/run.go @@ -194,6 +194,7 @@ func assembleMatrixConfig(config *Config, icalConnector ical.Service) *matrix.Co &message.ListEventsAction{}, &message.RegenICalTokenAction{}, &message.ChangeEventAction{}, + &message.ListCommandsAction{}, ) cfg.ReactionActions = append(cfg.ReactionActions, diff --git a/internal/connectors/matrix/actions/message/list_commands.go b/internal/connectors/matrix/actions/message/list_commands.go new file mode 100644 index 00000000..e64630cd --- /dev/null +++ b/internal/connectors/matrix/actions/message/list_commands.go @@ -0,0 +1,152 @@ +package message + +import ( + "regexp" + + "github.com/CubicrootXYZ/gologger" + "github.com/CubicrootXYZ/matrix-reminder-and-calendar-bot/internal/connectors/matrix" + "github.com/CubicrootXYZ/matrix-reminder-and-calendar-bot/internal/connectors/matrix/actions/reaction" + "github.com/CubicrootXYZ/matrix-reminder-and-calendar-bot/internal/connectors/matrix/actions/reply" + matrixdb "github.com/CubicrootXYZ/matrix-reminder-and-calendar-bot/internal/connectors/matrix/database" + "github.com/CubicrootXYZ/matrix-reminder-and-calendar-bot/internal/connectors/matrix/format" + "github.com/CubicrootXYZ/matrix-reminder-and-calendar-bot/internal/connectors/matrix/mautrixcl" + "github.com/CubicrootXYZ/matrix-reminder-and-calendar-bot/internal/connectors/matrix/messenger" + "github.com/CubicrootXYZ/matrix-reminder-and-calendar-bot/internal/connectors/matrix/msghelper" + "github.com/CubicrootXYZ/matrix-reminder-and-calendar-bot/internal/database" +) + +// TODO add tests! +var listCommandsRegex = regexp.MustCompile("(?i)^(((show|list)( all| the| my)( command| commands))|commands|help)[ ]*$") + +// ListCommandsAction sets the time for the daily reminder. +type ListCommandsAction struct { + logger gologger.Logger + client mautrixcl.Client + messenger messenger.Messenger + matrixDB matrixdb.Service + db database.Service + storer *msghelper.Storer +} + +// Configure is called on startup and sets all dependencies. +func (action *ListCommandsAction) Configure(logger gologger.Logger, client mautrixcl.Client, messenger messenger.Messenger, matrixDB matrixdb.Service, db database.Service, _ *matrix.BridgeServices) { + action.logger = logger + action.client = client + action.matrixDB = matrixDB + action.db = db + action.messenger = messenger + action.storer = msghelper.NewStorer(matrixDB, messenger, logger) +} + +// Name of the action +func (action *ListCommandsAction) Name() string { + return "List Commands" +} + +// GetDocu returns the documentation for the action. +func (action *ListCommandsAction) GetDocu() (title, explaination string, examples []string) { + return "List Commands", + "List available commands", + []string{"show all commands", "list the commands", "commands"} +} + +// Selector defines a regex on what messages the action should be used. +func (action *ListCommandsAction) Selector() *regexp.Regexp { + return listCommandsRegex +} + +// HandleEvent is where the message event get's send to if it matches the Selector. +func (action *ListCommandsAction) HandleEvent(event *matrix.MessageEvent) { + action.listMessageCommands(event) + action.listReplyCommands(event) + action.listReactions(event) +} + +func (action *ListCommandsAction) listMessageCommands(event *matrix.MessageEvent) { + msg := format.Formater{} + msg.Title("RemindMe Commands") + msg.TextLine("Try messaging me with the following messages and I will give my best to assist you!") + + for _, action := range []interface { + GetDocu() (string, string, []string) + }{ + &AddUserAction{}, + &ChangeEventAction{}, + &ChangeTimezoneAction{}, + &DeleteEventAction{}, + &EnableICalExportAction{}, + &ListCommandsAction{}, + &ListEventsAction{}, + &NewEventAction{}, + &RegenICalTokenAction{}, + &SetDailyReminderAction{}, + } { + title, explain, examples := action.GetDocu() + msg.BoldLine(title) + msg.TextLine(explain) + msg.TextLine("For example: ") + msg.List(examples) + } + + message, messageFormatted := msg.Build() + action.storer.SendAndStoreMessage( + message, + messageFormatted, + matrixdb.MessageTypeListCommands, + *event, + ) +} + +func (action *ListCommandsAction) listReplyCommands(event *matrix.MessageEvent) { + msg := format.Formater{} + msg.Title("RemindMe Reply Commands") + msg.TextLine("I can understand the following messages if you reply with them to certain messages.") + + for _, action := range []interface { + GetDocu() (string, string, []string) + }{ + &reply.ChangeTimeAction{}, + &reply.DeleteEventAction{}, + &reply.MakeRecurringAction{}, + } { + title, explain, examples := action.GetDocu() + msg.BoldLine(title) + msg.TextLine(explain) + msg.TextLine("For example: ") + msg.List(examples) + } + + message, messageFormatted := msg.Build() + action.storer.SendAndStoreMessage( + message, + messageFormatted, + matrixdb.MessageTypeListCommands, + *event, + ) +} + +func (action *ListCommandsAction) listReactions(event *matrix.MessageEvent) { + msg := format.Formater{} + msg.Title("RemindMe Reactions") + msg.TextLine("You can react to some messages to let me know I should act.") + + for _, action := range []interface { + GetDocu() (string, string, []string) + }{ + &reaction.AddTimeAction{}, + &reaction.DeleteEventAction{}, + &reaction.MarkDoneAction{}, + } { + title, explain, _ := action.GetDocu() + msg.BoldLine(title) + msg.TextLine(explain) + } + + message, messageFormatted := msg.Build() + action.storer.SendAndStoreMessage( + message, + messageFormatted, + matrixdb.MessageTypeListCommands, + *event, + ) +} diff --git a/internal/connectors/matrix/actions/reaction/add_time.go b/internal/connectors/matrix/actions/reaction/add_time.go index aed16318..172ce58b 100644 --- a/internal/connectors/matrix/actions/reaction/add_time.go +++ b/internal/connectors/matrix/actions/reaction/add_time.go @@ -46,7 +46,7 @@ Or use ▶️/⏩ to move the event to tomorrow/next week.`, // Selector defines on which reactions this action should be called. func (action *AddTimeAction) Selector() []string { - return []string{"1️⃣", "2️⃣", "3️⃣", "4️⃣", "5️⃣", "6️⃣", "7️⃣", "8️⃣", "9️⃣", "🔟", "➕"} + return []string{"1️⃣", "2️⃣", "3️⃣", "4️⃣", "5️⃣", "6️⃣", "7️⃣", "8️⃣", "9️⃣", "🔟", "➕", "⏩", "▶️"} } // HandleEvent is where the reaction event and the related message get's send to if it matches the Selector. diff --git a/internal/connectors/matrix/database/interface.go b/internal/connectors/matrix/database/interface.go index 6a0f9e90..c8fad805 100644 --- a/internal/connectors/matrix/database/interface.go +++ b/internal/connectors/matrix/database/interface.go @@ -62,6 +62,7 @@ type MatrixMessageType string var ( MessageTypeWelcome = MatrixMessageType("WELCOME") + MessageTypeListCommands = MatrixMessageType("LIST_COMMANDS") MessageTypeNewEvent = MatrixMessageType("EVENT_NEW") MessageTypeEvent = MatrixMessageType("EVENT") MessageTypeEventDelete = MatrixMessageType("EVENT_DELETE")