From 798a3dfd2c9000845c34c5cee06ef3b10b848413 Mon Sep 17 00:00:00 2001 From: Krombel Date: Tue, 16 Oct 2018 18:35:39 +0200 Subject: [PATCH 1/3] implement !help --- commands.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/commands.go b/commands.go index 5b795fc..66463d9 100644 --- a/commands.go +++ b/commands.go @@ -73,6 +73,8 @@ func (handler *CommandHandler) Handle(roomID types.MatrixRoomID, user *User, mes } } +const cmdLoginHelp = `!login - Authenticate this Bridge as WhatsApp Web Client` + func (handler *CommandHandler) CommandLogin(ce *CommandEvent) { if ce.User.Session != nil { ce.Reply("You're already logged in.") @@ -83,6 +85,8 @@ func (handler *CommandHandler) CommandLogin(ce *CommandEvent) { ce.User.Login(ce.RoomID) } +const cmdLogoutHelp = `!logout - Logout from WhatsApp` + func (handler *CommandHandler) CommandLogout(ce *CommandEvent) { if ce.User.Session == nil { ce.Reply("You're not logged in.") @@ -100,6 +104,10 @@ func (handler *CommandHandler) CommandLogout(ce *CommandEvent) { ce.Reply("Logged out successfully.") } +// CommandHelp handles !help command func (handler *CommandHandler) CommandHelp(ce *CommandEvent) { - ce.Reply("Help is not yet implemented 3:") + ce.Reply(strings.Join([]string{ + cmdLoginHelp, + cmdLogoutHelp, + }, "\n")) } From c107abbeb51c96976cca38cb95d8e7ad3cc19ea9 Mon Sep 17 00:00:00 2001 From: Krombel Date: Tue, 16 Oct 2018 19:15:38 +0200 Subject: [PATCH 2/3] add reply for unknown command; use configured command prefix --- commands.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/commands.go b/commands.go index 66463d9..88cbc6f 100644 --- a/commands.go +++ b/commands.go @@ -70,10 +70,12 @@ func (handler *CommandHandler) Handle(roomID types.MatrixRoomID, user *User, mes handler.CommandLogout(ce) case "help": handler.CommandHelp(ce) + default: + ce.Reply("Unknown Command") } } -const cmdLoginHelp = `!login - Authenticate this Bridge as WhatsApp Web Client` +const cmdLoginHelp = `login - Authenticate this Bridge as WhatsApp Web Client` func (handler *CommandHandler) CommandLogin(ce *CommandEvent) { if ce.User.Session != nil { @@ -85,7 +87,7 @@ func (handler *CommandHandler) CommandLogin(ce *CommandEvent) { ce.User.Login(ce.RoomID) } -const cmdLogoutHelp = `!logout - Logout from WhatsApp` +const cmdLogoutHelp = `logout - Logout from WhatsApp` func (handler *CommandHandler) CommandLogout(ce *CommandEvent) { if ce.User.Session == nil { @@ -104,10 +106,14 @@ func (handler *CommandHandler) CommandLogout(ce *CommandEvent) { ce.Reply("Logged out successfully.") } +const cmdHelpHelp = `help - Prints this help` + // CommandHelp handles !help command func (handler *CommandHandler) CommandHelp(ce *CommandEvent) { + cmdPrefix := handler.bridge.Config.Bridge.CommandPrefix + " " ce.Reply(strings.Join([]string{ - cmdLoginHelp, - cmdLogoutHelp, + cmdPrefix + cmdHelpHelp, + cmdPrefix + cmdLoginHelp, + cmdPrefix + cmdLogoutHelp, }, "\n")) } From ed79002592d693f0cd640656343605381cce1a5d Mon Sep 17 00:00:00 2001 From: Krombel Date: Tue, 16 Oct 2018 19:16:13 +0200 Subject: [PATCH 3/3] extend comments on methods of commands.go --- commands.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/commands.go b/commands.go index 88cbc6f..213c4e1 100644 --- a/commands.go +++ b/commands.go @@ -29,6 +29,7 @@ type CommandHandler struct { log maulogger.Logger } +// NewCommandHandler creates a CommandHandler func NewCommandHandler(bridge *Bridge) *CommandHandler { return &CommandHandler{ bridge: bridge, @@ -36,6 +37,7 @@ func NewCommandHandler(bridge *Bridge) *CommandHandler { } } +// CommandEvent stores all data which might be used to handle commands type CommandEvent struct { Bot *appservice.IntentAPI Bridge *Bridge @@ -45,6 +47,7 @@ type CommandEvent struct { Args []string } +// Reply sends a reply to command as notice func (ce *CommandEvent) Reply(msg string) { _, err := ce.Bot.SendNotice(string(ce.RoomID), msg) if err != nil { @@ -52,6 +55,7 @@ func (ce *CommandEvent) Reply(msg string) { } } +// Handle handles messages to the bridge func (handler *CommandHandler) Handle(roomID types.MatrixRoomID, user *User, message string) { args := strings.Split(message, " ") cmd := strings.ToLower(args[0]) @@ -77,6 +81,7 @@ func (handler *CommandHandler) Handle(roomID types.MatrixRoomID, user *User, mes const cmdLoginHelp = `login - Authenticate this Bridge as WhatsApp Web Client` +// CommandLogin handles login command func (handler *CommandHandler) CommandLogin(ce *CommandEvent) { if ce.User.Session != nil { ce.Reply("You're already logged in.") @@ -89,6 +94,7 @@ func (handler *CommandHandler) CommandLogin(ce *CommandEvent) { const cmdLogoutHelp = `logout - Logout from WhatsApp` +// CommandLogout handles !logout command func (handler *CommandHandler) CommandLogout(ce *CommandEvent) { if ce.User.Session == nil { ce.Reply("You're not logged in.") @@ -108,7 +114,7 @@ func (handler *CommandHandler) CommandLogout(ce *CommandEvent) { const cmdHelpHelp = `help - Prints this help` -// CommandHelp handles !help command +// CommandHelp handles help command func (handler *CommandHandler) CommandHelp(ce *CommandEvent) { cmdPrefix := handler.bridge.Config.Bridge.CommandPrefix + " " ce.Reply(strings.Join([]string{