This repository has been archived by the owner on May 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
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 #9 from LF-Engineering/slack
add slack library
- Loading branch information
Showing
2 changed files
with
120 additions
and
0 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,77 @@ | ||
package slack | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
|
||
"gopkg.in/resty.v1" | ||
) | ||
|
||
var ( | ||
errorBadResponse = "API call gave an other-than-200 result" | ||
errorEmptyText = "refusing to send an empty message, consider calling slack.SetText() first" | ||
) | ||
|
||
// Provider provides an interface to interact with the Slack Webhook API | ||
type Provider struct { | ||
Debug bool | ||
Payload Payload | ||
WebhookURL string | ||
} | ||
|
||
// Payload holds information about the message we want to send. Any of these values except Text may be left empty to | ||
// use the defaults for the Webhook. IconEmoji and IconURL are mutually exclusive, do not set both. | ||
// https://api.slack.com/incoming-webhooks#posting_with_webhooks | ||
type Payload struct { | ||
Channel string `json:"channel,omitempty"` | ||
IconEmoji string `json:"icon_emoji,omitempty"` | ||
IconURL string `json:"icon_url,omitempty"` | ||
Text string `json:"text,omitempty"` | ||
Username string `json:"username,omitempty"` | ||
} | ||
|
||
// New creates a new invocation of this Provider wrapper | ||
func New(webhookURL string) Provider { | ||
return Provider{ | ||
WebhookURL: webhookURL, | ||
} | ||
} | ||
|
||
// SetText sets the message text, i.e. what will be displayed in the Slack window | ||
func (a *Provider) SetText(text string) { | ||
a.Payload.Text = text | ||
} | ||
|
||
// Send sends a message to Slack | ||
func (a *Provider) Send() error { | ||
if a.Payload.Text == "" { | ||
return errors.New(errorEmptyText) | ||
} | ||
body, err := json.Marshal(a.Payload) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
restyResult, err := resty. | ||
SetDebug(a.Debug). | ||
SetHostURL(a.WebhookURL). | ||
R(). | ||
SetFormData(map[string]string{"payload": string(body)}). | ||
Post(a.WebhookURL) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if restyResult.StatusCode() != 200 { | ||
return errors.New(errorBadResponse) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// SendText sets message text and sends the message, a convenience wrapper for SetText and Send | ||
func (a *Provider) SendText(text string) error { | ||
a.SetText(text) | ||
return a.Send() | ||
} |
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,43 @@ | ||
package slack | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// generic helper for loading Slack API wrapper with Webhook URL | ||
func getAPI(t *testing.T) Provider { | ||
webhookURL := os.Getenv("SLACK_WEBHOOK_URL") | ||
require.NotEmpty(t, webhookURL, "ParameterNotFound: SLACK_WEBHOOK_URL") | ||
|
||
api := New(webhookURL) | ||
api.Payload.IconEmoji = ":robot_face:" | ||
api.Payload.Username = "Insights Slack - Unit Test" | ||
|
||
return api | ||
} | ||
|
||
func TestSendEmpty(t *testing.T) { | ||
api := getAPI(t) | ||
|
||
err := api.Send() | ||
assert.EqualError(t, err, errorEmptyText) | ||
} | ||
|
||
func TestSendNotEmpty(t *testing.T) { | ||
api := getAPI(t) | ||
api.SetText("TestSendNotEmpty Test Content") | ||
|
||
err := api.Send() | ||
assert.NoError(t, err, "unexpected err from api.Send()") | ||
} | ||
|
||
func TestSendText(t *testing.T) { | ||
api := getAPI(t) | ||
|
||
err := api.SendText("TestSendText Test Content") | ||
assert.NoError(t, err, "unexpected err from api.SendText()") | ||
} |