Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
robiball committed Oct 25, 2019
1 parent 2681d0b commit 351a7a4
Show file tree
Hide file tree
Showing 493 changed files with 83,822 additions and 23,907 deletions.
120 changes: 60 additions & 60 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
# unused-packages = true

[[constraint]]
branch = "master"
name = "github.com/nlopes/slack"
version = "0.6.0"

[prune]
go-tests = true
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func main() {
handlers.Init(sw)
log.Info("Connected to Slack API")
// Start a server to respond to callbacks from Slack
s := server.NewSlackHandler("/slack", appToken, signingSecret, nil, log.Error, log.Errorf)
s := server.NewSlackHandler("/slack", appToken, signingSecret, nil, log.Info, log.Infof, log.Error, log.Errorf)
s.HandleCommand("/help-me", handlers.HelpRequest)
s.HandleCallback("dialog_submission", "HelpRequest", handlers.HelpCallback)
addr := viper.GetString("listen-address")
Expand Down
42 changes: 34 additions & 8 deletions server/server.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package server

import (
"encoding/json"
"github.com/nlopes/slack"
"github.com/nlopes/slack/slackevents"
"io/ioutil"
"net/http"
"strings"
"github.com/nlopes/slack"
)

// LogFunc is an abstraction that allows using any external logger with a Print signature
Expand All @@ -29,6 +32,8 @@ type Route struct {
type SlackHandler struct {
Log LogFunc
Logf LogfFunc
ErrorLog LogFunc
ErrorLogf LogfFunc
Routes []*Route
DefaultRoute SlackHandlerFunc
basePath string
Expand All @@ -38,18 +43,20 @@ type SlackHandler struct {
}

// NewSlackHandler returns an initialised SlackHandler
func NewSlackHandler(basePath, appToken, secretToken string, dnHeader *string, l LogFunc, lf LogfFunc) *SlackHandler {
func NewSlackHandler(basePath, appToken, secretToken string, dnHeader *string, l LogFunc, lf LogfFunc, el LogFunc, elf LogfFunc) *SlackHandler {
return &SlackHandler{
DefaultRoute: func(res *Response, req *Request, ctx interface{}) error {
res.Text(http.StatusNotFound, "Not found")
return nil
},
Log: l,
Logf: lf,
ErrorLog: el,
ErrorLogf: elf,
basePath: basePath,
appToken: appToken,
secretToken: secretToken,
dnHeader: dnHeader,
dnHeader: dnHeader,
}
}

Expand Down Expand Up @@ -87,22 +94,41 @@ func (h *SlackHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Generic serve function which captures and logs handler errors
serve := func(f SlackHandlerFunc, ctx interface{}) {
if err := f(res, req, ctx); err != nil {
h.Logf("HTTP handler error: %s", err)
h.ErrorLogf("HTTP handler error: %s", err)
}
}

// If the request did not look like it came from slack, 400 and abort
if err := req.Validate(h.secretToken, h.dnHeader); err != nil {
h.Logf("Bad request from slack: %s", err)
h.ErrorLogf("Bad request from slack: %s", err)
res.Text(400, "invalid slack request")
return
}

// First check if path matches our BasePath and has valid form data
// If yes then attempt to decode it to match on Command or CallbackID / InteractionType
// If yes then attempt to decode it to match on Command, Events challenge, or CallbackID / InteractionType
// If no then match custom paths
err := r.ParseForm()
if strings.HasPrefix(r.URL.Path, h.basePath) && err == nil {
if strings.HasPrefix(r.URL.Path, h.basePath) && err == nil {
// Is this a url challenge from Slack?
body, err := ioutil.ReadAll(r.Body)
// This has a body, lets do stuff with it
if err == nil {
// Decode the potential challenge payload
var verificationEvent slackevents.EventsAPIURLVerificationEvent
err := json.Unmarshal(body, &verificationEvent)
if err == nil {
// This seems to be a url verification request from Slack, check it is and respond accordingly
if verificationEvent.Type == "url_verification" {
if _, err := w.Write([]byte(verificationEvent.Challenge)); err != nil {
h.ErrorLogf("Failed writing challenge back to verificationEvent: %w", err)
}
h.ErrorLogf("Successfully responded to URL verification requested from Slack")
return
}
}
}

// Is it a slash command?
if r.Form.Get("command") != "" {
sc, _ := slack.SlashCommandParse(r)
Expand All @@ -119,7 +145,7 @@ func (h *SlackHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Does it have a valid callback payload? - If so, it's a callback
payload, err := req.CallbackPayload()
if err != nil {
h.Logf("Error parsing payload: %s", err)
h.ErrorLogf("Error parsing payload: %s", err)
}
// Loop through all our routes and attempt a match on the InteractionType / CallbackID pair
if payload != nil {
Expand Down

0 comments on commit 351a7a4

Please sign in to comment.