parent
10d54bb19c
commit
7a1b9c8a36
@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"html"
|
"html"
|
||||||
"log"
|
"log"
|
||||||
|
"net/url"
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"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
|
// 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
|
// wether we are dealing with a command, and if it is one of the supported
|
||||||
// ones.
|
// 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) {
|
func (s *SlackService) SendCommand(channelID string, message string) (bool, error) {
|
||||||
// First check if it begins with slash and a command
|
// First check if it begins with slash and a command
|
||||||
r, err := regexp.Compile(`^/\w+`)
|
r, err := regexp.Compile(`^/\w+`)
|
||||||
@ -367,6 +374,31 @@ func (s *SlackService) SendCommand(channelID string, message string) (bool, erro
|
|||||||
return false, err
|
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
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user