Skip to content

Commit

Permalink
add list commands action
Browse files Browse the repository at this point in the history
  • Loading branch information
CubicrootXYZ committed Apr 29, 2024
1 parent 5cbf684 commit bcf37c1
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 1 deletion.
1 change: 1 addition & 0 deletions internal/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
152 changes: 152 additions & 0 deletions internal/connectors/matrix/actions/message/list_commands.go
Original file line number Diff line number Diff line change
@@ -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,
)
}
2 changes: 1 addition & 1 deletion internal/connectors/matrix/actions/reaction/add_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions internal/connectors/matrix/database/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit bcf37c1

Please sign in to comment.