aboutsummaryrefslogtreecommitdiff
path: root/api
diff options
context:
space:
mode:
authorShulhan <m.shulhan@gmail.com>2021-02-03 14:08:39 +0700
committerShulhan <m.shulhan@gmail.com>2021-02-03 14:09:53 +0700
commitbff6f00e5e45cc226c5bcb5b023bce596c824c05 (patch)
treeb578aedb4c87a0fcdd8d31ff4d72d8811de242a2 /api
parenta4acb2da36563fe814ad8ccf325cd733a9e2e1f1 (diff)
downloadpakakeh.go-bff6f00e5e45cc226c5bcb5b023bce596c824c05.tar.xz
api/slack: a simple API to post message to Slack
This package only depends on standard packages. Currently, it provide function to send message using incoming webhook URL.
Diffstat (limited to 'api')
-rw-r--r--api/slack/message.go16
-rw-r--r--api/slack/slack.go48
-rw-r--r--api/slack/slack_example_test.go28
3 files changed, 92 insertions, 0 deletions
diff --git a/api/slack/message.go b/api/slack/message.go
new file mode 100644
index 00000000..fd4805be
--- /dev/null
+++ b/api/slack/message.go
@@ -0,0 +1,16 @@
+// Copyright 2021, Shulhan <ms@kilabit.info>. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package slack
+
+//
+// Message define the standard message payload with JSON tags.
+//
+type Message struct {
+ Channel string `json:"channel,omitempty"`
+ Username string `json:"username,omitempty"`
+ IconEmoji string `json:"icon_emoji,omitempty"`
+ IconUrl string `json:"icon_url,omitempty"`
+ Text string `json:"text"`
+}
diff --git a/api/slack/slack.go b/api/slack/slack.go
new file mode 100644
index 00000000..360a43db
--- /dev/null
+++ b/api/slack/slack.go
@@ -0,0 +1,48 @@
+// Copyright 2021, Shulhan <ms@kilabit.info>. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+//
+// Package slack provide a simple API for sending message to Slack using only
+// standard packages.
+//
+package slack
+
+import (
+ "bytes"
+ "encoding/json"
+ "fmt"
+ "io/ioutil"
+ "net/http"
+)
+
+//
+// PostWebhook send a message using "Incoming Webhook".
+//
+func PostWebhook(webhookUrl string, msg *Message) (err error) {
+ payload, err := json.Marshal(&msg)
+ if err != nil {
+ return fmt.Errorf("PostWebhook: %w", err)
+ }
+
+ res, err := http.DefaultClient.Post(webhookUrl, "application/json",
+ bytes.NewReader(payload))
+ if err != nil {
+ return fmt.Errorf("PostWebhook: %w", err)
+ }
+
+ if res.StatusCode != http.StatusOK {
+ resBody, err := ioutil.ReadAll(res.Body)
+ if err != nil {
+ return fmt.Errorf("PostWebhook: %w", err)
+ }
+ return fmt.Errorf("PostWebhook: %s: %s\n", res.Status, resBody)
+ }
+
+ err = res.Body.Close()
+ if err != nil {
+ return fmt.Errorf("PostWebhook: %w", err)
+ }
+
+ return nil
+}
diff --git a/api/slack/slack_example_test.go b/api/slack/slack_example_test.go
new file mode 100644
index 00000000..4f2d4e9d
--- /dev/null
+++ b/api/slack/slack_example_test.go
@@ -0,0 +1,28 @@
+// Copyright 2021, Shulhan <ms@kilabit.info>. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package slack
+
+import (
+ "log"
+ "os"
+)
+
+func ExamplePostWebhook() {
+ webhookURL := os.Getenv("SLACK_WEBHOOK_URL")
+ if len(webhookURL) == 0 {
+ return
+ }
+ msg := &Message{
+ Channel: "test",
+ Username: "Test",
+ IconEmoji: ":ghost:",
+ Text: "Hello, world!",
+ }
+ err := PostWebhook(webhookURL, msg)
+ if err != nil {
+ log.Fatal(err)
+ }
+ //Output:
+}