-
Notifications
You must be signed in to change notification settings - Fork 16
Command
Go Hagiwara edited this page Jul 27, 2019
·
5 revisions
sarah.Command
interface represents a plugin that receives user input and returns a response. Command.Match
is called against user input in Bot.Respond
. If it returns true, the command is considered "corresponds to user input," and hence its Execute
method is called.
Any struct that satisfies sarah.Command
interface can be fed to Bot.AppendCommand
as a command. However sarah.CommandPropsBuilder
is provided to easily implement sarah.Command
interface on the fly. The use of sarah.CommandPropsBuilder
has some advantages such as the live configuration update. See CommandPropsBuilder for detail.
The simplest form of sarah.Command
implementation can be something like below:
type HelloCommand struct {
}
func (hello *HelloCommand) Identifier() string {
return "hello"
}
func (hello *HelloCommand) Execute(_ context.Context, input sarah.Input) (*sarah.CommandResponse, error) {
return slack.Response(input, "Hello!"), nil
}
func (hello *HelloCommand) Instruction(_ *sarah.HelpInput) string {
// Developers may refer to the incoming HelpInput to selectively return an empty string.
// When an empty string is returned, the command information is not returned to the user.
// This is useful when a command is only available for a limited chat room.
return `Input ".hello" to greet.`
}
func (hello *HelloCommand) Match(input sarah.Input) bool {
// sarah.Input contains more information than a simple text representation of the input.
// Depending on the Input implementation, this gives developers a capability to selectively allow the usage of the command.
// e.g. Always return false as long as the input is sent in a non-admin group.
return strings.TrimSpace(input.Message()) == ".hello"
}
To have a grasp of overall architecture, have a look at Components.