Skip to content

Commit

Permalink
Add support for params in slack commands (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbrazier committed Jul 4, 2021
1 parent f028e5c commit 7b1a019
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
6 changes: 6 additions & 0 deletions api/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ func ValidateKey(key string) bool {
r, _ := regexp.Compile("^[\\w-]+$")
return r.MatchString(key)
}

// ValidateKeyPath validates a key with optional parameters
func ValidateKeyPath(key string) bool {
r, _ := regexp.Compile("^[\\w-\\/]+$")
return r.MatchString(key)
}
7 changes: 4 additions & 3 deletions api/handler/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,17 @@ func (h *Handler) SlackCommand(c echo.Context) error {
}

key := payload.Text
if ValidateKey(key) == false {
if ValidateKeyPath(key) == false {
response.Text = "The URL key you provided is an invalid format. You can only provide one key at a time, e.g. `/go help`"
return c.JSON(http.StatusOK, response)
}
appURI := config.GetConfig().AppURI
url, err := urlModel.Find(key)
if err != nil || url == nil {
urls, err := urlModel.GetUrlsFromKeys([]string{key})
if err != nil || len(urls) == 0 {
response.Text = fmt.Sprintf("The URL key you provided was not found. Why not add it? %s/go/%s", appURI, key)
return c.JSON(http.StatusOK, response)
}
url := urls[0]
response.Text = fmt.Sprintf("%s/%s", appURI, key)
if url.URL == "" {
aliasURLs := make([]string, len(url.Alias))
Expand Down
17 changes: 9 additions & 8 deletions api/slackbot/slackbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type SlackBot struct {
}

func (s *SlackBot) getKeyFromText(text string) string {
r, _ := regexp.Compile("\\bgo ([\\w-]+)\\b")
r, _ := regexp.Compile("\\bgo ([\\w-\\/]+)\\b")

matches := r.FindStringSubmatch(text)

Expand Down Expand Up @@ -78,12 +78,12 @@ func (s *SlackBot) Init() {
}
// Edit message if previous one exists
if oldKey != "" {
url, err := urlModel.Find(oldKey)
urls, err := urlModel.GetUrlsFromKeys([]string{oldKey})
if err != nil {
fmt.Printf("An error occurred while finding old key %v", err)
break
}
if url != nil {
if len(urls) > 0 {
edited = true
}
}
Expand All @@ -98,28 +98,29 @@ func (s *SlackBot) Init() {
break
}

url, err := urlModel.Find(key)
urls, err := urlModel.GetUrlsFromKeys([]string{key})
if err != nil {
fmt.Printf("An error occurred while finding key %v", err)
break
}
if url == nil {
if len(urls) == 0 {
if ev.PreviousMessage != nil {
s.deleteMessage(ev.Channel, ev.PreviousMessage.Timestamp)
}
break
}
url := urls[0]
appURI := appConfig.AppURI

extraText := url.URL
if url.URL == "" {
urls, err := urlModel.GetUrlsFromKeys(url.Alias)
aliasUrls, err := urlModel.GetUrlsFromKeys(url.Alias)
if err != nil {
fmt.Println("Error while processing slackbot aliases")
break
}
urlStrings := make([]string, len(urls))
for i, url := range urls {
urlStrings := make([]string, len(aliasUrls))
for i, url := range aliasUrls {
urlStrings[i] = url.URL
}
extraText = strings.Join(urlStrings, "\n")
Expand Down

0 comments on commit 7b1a019

Please sign in to comment.