Initial commit from mattermost-plugin-starter-template
This commit is contained in:
commit
acbc69f7eb
57 changed files with 27772 additions and 0 deletions
64
server/command/command.go
Normal file
64
server/command/command.go
Normal file
|
@ -0,0 +1,64 @@
|
|||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/mattermost/mattermost/server/public/model"
|
||||
"github.com/mattermost/mattermost/server/public/pluginapi"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
client *pluginapi.Client
|
||||
}
|
||||
|
||||
type Command interface {
|
||||
Handle(args *model.CommandArgs) (*model.CommandResponse, error)
|
||||
executeHelloCommand(args *model.CommandArgs) *model.CommandResponse
|
||||
}
|
||||
|
||||
const helloCommandTrigger = "hello"
|
||||
|
||||
// Register all your slash commands in the NewCommandHandler function.
|
||||
func NewCommandHandler(client *pluginapi.Client) Command {
|
||||
err := client.SlashCommand.Register(&model.Command{
|
||||
Trigger: helloCommandTrigger,
|
||||
AutoComplete: true,
|
||||
AutoCompleteDesc: "Say hello to someone",
|
||||
AutoCompleteHint: "[@username]",
|
||||
AutocompleteData: model.NewAutocompleteData(helloCommandTrigger, "[@username]", "Username to say hello to"),
|
||||
})
|
||||
if err != nil {
|
||||
client.Log.Error("Failed to register command", "error", err)
|
||||
}
|
||||
return &Handler{
|
||||
client: client,
|
||||
}
|
||||
}
|
||||
|
||||
// ExecuteCommand hook calls this method to execute the commands that were registered in the NewCommandHandler function.
|
||||
func (c *Handler) Handle(args *model.CommandArgs) (*model.CommandResponse, error) {
|
||||
trigger := strings.TrimPrefix(strings.Fields(args.Command)[0], "/")
|
||||
switch trigger {
|
||||
case helloCommandTrigger:
|
||||
return c.executeHelloCommand(args), nil
|
||||
default:
|
||||
return &model.CommandResponse{
|
||||
ResponseType: model.CommandResponseTypeEphemeral,
|
||||
Text: fmt.Sprintf("Unknown command: %s", args.Command),
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Handler) executeHelloCommand(args *model.CommandArgs) *model.CommandResponse {
|
||||
if len(strings.Fields(args.Command)) < 2 {
|
||||
return &model.CommandResponse{
|
||||
ResponseType: model.CommandResponseTypeEphemeral,
|
||||
Text: "Please specify a username",
|
||||
}
|
||||
}
|
||||
username := strings.Fields(args.Command)[1]
|
||||
return &model.CommandResponse{
|
||||
Text: "Hello, " + username,
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue