Add slash command support

Fixes #101
This commit is contained in:
erroneousboat 2019-05-26 12:05:52 +02:00
parent 10d54bb19c
commit 7a1b9c8a36

View File

@ -5,6 +5,7 @@ import (
"fmt"
"html"
"log"
"net/url"
"regexp"
"sort"
"strconv"
@ -337,6 +338,12 @@ func (s *SlackService) SendReply(channelID string, threadID string, message stri
// SendCommand will send a specific command to slack. First we check
// wether we are dealing with a command, and if it is one of the supported
// ones.
//
// NOTE: slack slash commands that are sent to the slack api are undocumented,
// and as such we need to update the message option that direct it to the
// correct api endpoint.
//
// https://github.com/ErikKalkoken/slackApiDoc/blob/master/chat.command.md
func (s *SlackService) SendCommand(channelID string, message string) (bool, error) {
// First check if it begins with slash and a command
r, err := regexp.Compile(`^/\w+`)
@ -367,6 +374,31 @@ func (s *SlackService) SendCommand(channelID string, message string) (bool, erro
return false, err
}
return true, nil
default:
r := regexp.MustCompile(`(?P<cmd>^/\w+) (?P<text>.*)`)
subMatch := r.FindStringSubmatch(message)
if len(subMatch) < 3 {
return false, errors.New("slash command malformed")
}
cmd := subMatch[1]
text := subMatch[2]
msgOption := slack.UnsafeMsgOptionEndpoint(
fmt.Sprintf("%s%s", slack.APIURL, "chat.command"),
func(urlValues url.Values) {
urlValues.Add("command", cmd)
urlValues.Add("text", text)
},
)
_, _, err := s.Client.PostMessage(channelID, msgOption)
if err != nil {
return false, err
}
return true, nil
}