Skip to content

Commit

Permalink
Merge pull request #24 from skybet/nlopes_upgrade
Browse files Browse the repository at this point in the history
Nlopes slack upgrade
  • Loading branch information
robiball authored Dec 7, 2018
2 parents 4e7490c + 2168f68 commit 2681d0b
Show file tree
Hide file tree
Showing 72 changed files with 1,867 additions and 1,842 deletions.
36 changes: 15 additions & 21 deletions Gopkg.lock

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

6 changes: 1 addition & 5 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,9 @@
# go-tests = true
# unused-packages = true

[[constraint]]
name = "github.com/nlopes/slack"
version = "0.3.0"

[[constraint]]
branch = "master"
name = "github.com/BeepBoopHQ/go-slackbot"
name = "github.com/nlopes/slack"

[prune]
go-tests = true
Expand Down
4 changes: 2 additions & 2 deletions handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func HelpRequest(res *server.Response, req *server.Request, ctx interface{}) err
if !ok {
return fmt.Errorf("Expected a slack.SlashCommand to be passed to the handler")
}
descriptionElement := slack.DialogTextElement{
descriptionElement := slack.DialogInput{
Type: "text",
Label: "Help Request Description",
Placeholder: "Describe what you would like help with ...",
Expand All @@ -47,7 +47,7 @@ func HelpRequest(res *server.Response, req *server.Request, ctx interface{}) err
}

dialog := slack.Dialog{
CallbackId: "HelpRequest",
CallbackID: "HelpRequest",
Title: "Request Help",
SubmitLabel: "Create",
NotifyOnCancel: true,
Expand Down
74 changes: 19 additions & 55 deletions server/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ import (
"strings"
"time"

"github.com/mitchellh/mapstructure"
"github.com/nlopes/slack"
"regexp"
)

// Request wraps http.Request
type Request struct {
*http.Request
payload CallbackPayload
payload *slack.InteractionCallback
}

// Validate the request comes from Slack
Expand Down Expand Up @@ -79,69 +78,34 @@ func (r *Request) Validate(secret string, dnHeader *string) error {
}

// CallbackPayload returns the parsed payload if it exists and is valid
func (r *Request) CallbackPayload() (CallbackPayload, error) {
if r.payload == nil {
if err := r.parsePayload(); err != nil {
return nil, err
}
if err := r.payload.Validate(); err != nil {
return nil, err
}
func (r *Request) CallbackPayload() (*slack.InteractionCallback, error) {
if err := r.parsePayload(); err != nil {
return nil, err
}
return r.payload, nil
}

func (r *Request) parsePayload() error {
var payload CallbackPayload
j := r.Form.Get("payload")
if j == "" {
return errors.New("empty payload")
}
if err := json.Unmarshal([]byte(j), &payload); err != nil {
return fmt.Errorf("error parsing payload JSON: %s", err)
}
r.payload = payload
return nil
}

// CallbackPayload represents the data sent by Slack on a user initiated event
type CallbackPayload map[string]interface{}

// Validate the payload
func (c CallbackPayload) Validate() error {
var errs []string
if c["type"] == nil {
if r.payload.Type == "" {
errs = append(errs, "Missing value for 'type' key")
}
if c["callback_id"] == nil {
if r.payload.CallbackID == "" {
errs = append(errs, "Missing value for 'callback_id' key")
}
if len(errs) > 0 {
return fmt.Errorf("%s", strings.Join(errs, ", "))
return nil, fmt.Errorf("%s", strings.Join(errs, ", "))
}
return nil
}

// MatchRoute determines if we can route this request based on the payload
func (c CallbackPayload) MatchRoute(r *Route) bool {
if c["type"] == r.InteractionType && c["callback_id"] == r.CallbackID {
return true
}
return false
return r.payload, nil
}

// Mutate the payload into a go type matching it's type field
func (c CallbackPayload) Mutate() (interface{}, error) {
switch c["type"] {
case "dialog_submission":
var result slack.DialogCallback
err := mapstructure.Decode(c, &result)
return &result, err
case "dialog_suggestion":
var result slack.DialogSuggestionCallback
err := mapstructure.Decode(c, &result)
return &result, err
default:
return c, nil
func (r *Request) parsePayload() error {
var payload slack.InteractionCallback
j := r.Form.Get("payload")
if j == "" {
return errors.New("empty payload")
}
}
if err := json.Unmarshal([]byte(j), &payload); err != nil {
return fmt.Errorf("error parsing payload JSON: %s", err)
}
r.payload = &payload
return nil
}
14 changes: 6 additions & 8 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,13 @@ func (h *SlackHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.Logf("Error parsing payload: %s", err)
}
// Loop through all our routes and attempt a match on the InteractionType / CallbackID pair
for _, rt := range h.Routes {
if payload.MatchRoute(rt) {
// Send the payload as context
t, err := payload.Mutate()
if err != nil {
h.Logf("Error mutating %s payload: %s", payload["type"], err)
if payload != nil {
for _, rt := range h.Routes {
if string(payload.Type) == rt.InteractionType && payload.CallbackID == rt.CallbackID {
// Send the payload as context
serve(rt.Handler, payload)
return
}
serve(rt.Handler, t)
return
}
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ func TestDialogSubmissionEvent(t *testing.T) {

raw := "payload=%7B%22type%22%3A%20%22dialog_submission%22%2C%22submission%22%3A%20%7B%22name%22%3A%20%22Sigourney%20Dreamweaver%22%2C%22email%22%3A%20%22sigdre%40example.com%22%2C%22phone%22%3A%20%22%2B1%20800-555-1212%22%2C%22meal%22%3A%20%22burrito%22%2C%22comment%22%3A%20%22No%20sour%20cream%20please%22%2C%22team_channel%22%3A%20%22C0LFFBKPB%22%2C%22who_should_sing%22%3A%20%22U0MJRG1AL%22%7D%2C%22callback_id%22%3A%20%22employee_offsite_1138b%22%2C%22team%22%3A%20%7B%22id%22%3A%20%22T1ABCD2E12%22%2C%22domain%22%3A%20%22coverbands%22%7D%2C%22user%22%3A%20%7B%22id%22%3A%20%22W12A3BCDEF%22%2C%22name%22%3A%20%22dreamweaver%22%7D%2C%22channel%22%3A%20%7B%22id%22%3A%20%22C1AB2C3DE%22%2C%22name%22%3A%20%22coverthon-1999%22%7D%2C%22action_ts%22%3A%20%22936893340.702759%22%2C%22token%22%3A%20%22TOKEN%22%2C%22response_url%22%3A%20%22https%3A%2F%2Fhooks.slack.com%2Fapp%2FT012AB0A1%2F123456789%2FJpmK0yzoZDeRiqfeduTBYXWQ%22%7D"
h := func(res *Response, req *Request, ctx interface{}) error {
d, ok := ctx.(*slack.DialogCallback)
d, ok := ctx.(*slack.InteractionCallback)
if !ok {
t.Fatalf("Expected a *slack.DialogCallback to be passed to the handler")
t.Fatalf("Expected a *slack.InteractionCallback to be passed to the handler")
}
if d.User.ID != "W12A3BCDEF" {
t.Fatalf("Unexpected value for User.Id: %s", d.User.ID)
Expand Down
5 changes: 0 additions & 5 deletions vendor/github.com/BeepBoopHQ/go-slackbot/.travis.yml

This file was deleted.

21 changes: 0 additions & 21 deletions vendor/github.com/BeepBoopHQ/go-slackbot/LICENSE

This file was deleted.

43 changes: 0 additions & 43 deletions vendor/github.com/BeepBoopHQ/go-slackbot/README.md

This file was deleted.

Loading

0 comments on commit 2681d0b

Please sign in to comment.