-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from theothertomelliott:theothertomelliott/iss…
…ue27 Allow retry behavior to be disabled
- Loading branch information
Showing
11 changed files
with
192 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,18 @@ | ||
package spanner | ||
|
||
import "context" | ||
|
||
type Action interface { | ||
Type() string | ||
Data() interface{} | ||
} | ||
|
||
// ActionInterceptor intercepts a single action to allow for instrumentation. | ||
// The next function must be called to perform the action itself. | ||
// The configuration for the action cannot be changed. | ||
type ActionInterceptor func(ctx context.Context, action Action, next func(context.Context) error) error | ||
|
||
// FinishInterceptor intercepts the finishing step of handling an event to allow for instrumentation. | ||
// The finish function must be called to perform the required actions. | ||
// The set of actions cannot be changed. | ||
type FinishInterceptor func(ctx context.Context, actions []Action, finish func(context.Context) error) error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"log" | ||
"os" | ||
|
||
"github.com/theothertomelliott/spanner" | ||
"github.com/theothertomelliott/spanner/slack" | ||
) | ||
|
||
func main() { | ||
botToken := os.Getenv("SLACK_BOT_TOKEN") | ||
appToken := os.Getenv("SLACK_APP_TOKEN") | ||
|
||
app, err := slack.NewApp( | ||
slack.AppConfig{ | ||
BotToken: botToken, | ||
AppToken: appToken, | ||
AckOnError: true, | ||
FinishInterceptor: func(ctx context.Context, actions []spanner.Action, finish func(context.Context) error) error { | ||
if len(actions) > 0 { | ||
var data []interface{} | ||
for _, action := range actions { | ||
data = append(data, action.Data()) | ||
} | ||
dataJson, err := json.MarshalIndent(data, "", " ") | ||
if err != nil { | ||
log.Println("marshalling action data:", err) | ||
} | ||
log.Println("Will attempt actions: ", string(dataJson)) | ||
} | ||
return finish(ctx) | ||
}, | ||
ActionInterceptor: func(ctx context.Context, action spanner.Action, exec func(context.Context) error) error { | ||
err := exec(ctx) | ||
if err != nil { | ||
dataJson, jsonErr := json.MarshalIndent(action.Data(), "", " ") | ||
if jsonErr != nil { | ||
log.Println("marshalling action data:", err) | ||
} | ||
log.Printf("error: %q, when executing action: %v", err, string(dataJson)) | ||
} | ||
return err | ||
}, | ||
}, | ||
) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
err = app.Run(func(ctx context.Context, ev spanner.Event) error { | ||
if msg := ev.ReceiveMessage(); msg != nil && msg.Text() == "hello" { | ||
|
||
replyGood := ev.SendMessage(msg.Channel().ID()) | ||
replyGood.PlainText("This message should succeed") | ||
|
||
replyBad := ev.SendMessage("invalid_channel") | ||
replyBad.PlainText("This message will always fail to post") | ||
|
||
replySkipped := ev.SendMessage(msg.Channel().ID()) | ||
replySkipped.PlainText("This message should be skipped because of the previous error") | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters