10
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GoでSlackにPostする

Last updated at Posted at 2019-01-05

SlackにGo言語でMessageをPostするコードを書いたので備忘録です。
Slackにエラーログを送ったり、アラート通知やリソース情報通知などに活用する。

SlackPost.go
package main

import (
	"github.com/ashwanthkumar/slack-go-webhook"
	"os"
)

const (
	WEBHOOKURL = "https://hooks.slack.com/services/XXXX"
	CHANNEL    = "dev"
	USERNAME   = "GoBot"
)

func main() {
	PostSlack("HelloWorld!!")
}

func PostSlack(msg string) {
	field1 := slack.Field{Title: "Message", Value: msg}
	field2 := slack.Field{Title: "AnythingKey", Value: "AnythingValue"}

	attachment := slack.Attachment{}
	attachment.AddField(field1).AddField(field2)
	color := "good"
	attachment.Color = &color
	payload := slack.Payload{
		Username:    USERNAME,
		Channel:     CHANNEL,
		Attachments: []slack.Attachment{attachment},
	}
	err := slack.Send(WEBHOOKURL, "", payload)
	if err != nil {
		os.Exit(1)
	}
}

Slackへの通知結果

GoSlackPost.png

まとめ

Go言語を使って、SlackにPostするコードを作成しました。

10
9
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?