-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
61416e2
commit 3bbbe7e
Showing
10 changed files
with
243 additions
and
30 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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package agerr | ||
|
||
import "fmt" | ||
|
||
// Wrap wraps an error with `wrapmsg` | ||
func Wrap(wrapmsg string, err error) error { | ||
if err != nil { | ||
return fmt.Errorf(wrapmsg, err) | ||
} | ||
return nil | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package log | ||
|
||
import "github.com/agflow/tools/notification/slack" | ||
|
||
// NewSlackHook returns a hook for slack | ||
func NewSlackHook(token string) Hook { | ||
return func(info MetaInfo) error { | ||
var color string | ||
switch info.Lvl { | ||
case InfoLvl: | ||
color = slack.ColorGood | ||
case FatalLvl, PanicLvl, ErrorLvl: | ||
color = slack.ColorDanger | ||
default: | ||
color = slack.ColorWarning | ||
} | ||
msg := info.Msg + "\n" | ||
slackCli := slack.New(token, true) | ||
return slackCli.SendWithColor("feed-worker", msg, color) | ||
} | ||
} |
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,6 @@ | ||
package notification | ||
|
||
// Service is an interface of notification.Service | ||
type Service interface { | ||
Send(string, string) 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package slack | ||
|
||
import ( | ||
"github.com/slack-go/slack" | ||
|
||
"github.com/agflow/tools/agerr" | ||
) | ||
|
||
const ( | ||
defaultChannel = "test-notifications" | ||
// ColorGood is slack's color "good" | ||
ColorGood = "good" | ||
// ColorWarning is slack's color "warning" | ||
ColorWarning = "warning" | ||
// ColorDanger is slack's color "danger" | ||
ColorDanger = "danger" | ||
) | ||
|
||
// Client is wrapper of a slack.Client | ||
type Client struct { | ||
slackCli *slack.Client | ||
enabled bool | ||
} | ||
|
||
// New return a new notifications/slack.Client | ||
func New(token string, enabled bool) *Client { | ||
return &Client{slackCli: slack.New(token), enabled: enabled} | ||
} | ||
|
||
func getChannel(channel string) string { | ||
if channel == "" { | ||
return defaultChannel | ||
} | ||
return channel | ||
} | ||
|
||
// Send sends a notification message to slack | ||
func (c *Client) Send(channel, msg string) error { | ||
if !c.enabled { | ||
return nil | ||
} | ||
|
||
channel = getChannel(channel) | ||
|
||
_, _, err := c.slackCli.PostMessage( | ||
channel, | ||
slack.MsgOptionAsUser(true), | ||
slack.MsgOptionText(msg, false)) | ||
return agerr.Wrap("can't send slack notification: %w", err) | ||
} | ||
|
||
// SendWithColor sends a notification message to slack as an attachment with color | ||
func (c *Client) SendWithColor(channel, msg, color string) error { | ||
if !c.enabled { | ||
return nil | ||
} | ||
|
||
channel = getChannel(channel) | ||
attachment := slack.Attachment{ | ||
Text: msg, | ||
Color: color, | ||
} | ||
|
||
_, _, err := c.slackCli.PostMessage(channel, | ||
slack.MsgOptionAttachments(attachment), | ||
slack.MsgOptionAsUser(true), | ||
) | ||
return agerr.Wrap("can't send slack notification with color: %w", 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package slack | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSend(t *testing.T) { | ||
slackCli := New("xoxb-2314993037-3480243399810-eKklHxCNGvH7E3dnGaknRpZL", true) | ||
require.Nil(t, slackCli.Send(defaultChannel, "this is a test notification")) | ||
require.Nil(t, slackCli.SendWithColor( | ||
defaultChannel, | ||
"this is a test notification with a color", | ||
ColorGood, | ||
), | ||
) | ||
} |
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