Skip to content

Commit

Permalink
Add slash command support
Browse files Browse the repository at this point in the history
Fixes #101
  • Loading branch information
jpbruinsslot committed May 26, 2019
1 parent 10d54bb commit 7a1b9c8
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions service/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"html"
"log"
"net/url"
"regexp"
"sort"
"strconv"
Expand Down Expand Up @@ -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+`)
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 7a1b9c8

Please sign in to comment.