slackposter

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 19, 2017 License: MIT Imports: 5 Imported by: 0

README

skackposter

Godoc Reference

Post a payload to your Slack incoming webhook.

Usage

You can send a simple message.

package main

import (
	"github.com/mnkd/slackposter"
)

func main() {
    config := slackposter.Config{
        "#channel_name or channel_id",
        ":ghost:",
        "Ghost",
        "https://hooks.slack.com/services/xxxx/xxx/xxx",
    }

    slack := slackposter.NewSlack(config)
    slack.PostMessage("Hello world!")
}

message

You can send a customized message (payload).

package main

import (
	"time"

	slack "github.com/mnkd/slackposter"
)

func main() {
	config := slack.Config{
		Channel:    "#your-channel",
		IconEmoji:  ":octocat:",
		Username:   "GitHub | Status",
		WebhookURL: "https://hooks.slack.com/services/xxx/xxx/xxx",
	}

	poster := slack.NewSlackPoster(config)

	payload := poster.NewPayload()
	payload.Mrkdwn = true

	statusField := slack.Field{
		Title: "Status",
		Value: "Good",
		Short: true,
	}

	dateString := time.Now().Format("2006-01-02 15:04")
	dateField := slack.Field{
		Title: "Date",
		Value: dateString,
		Short: true,
	}

	attachment := slack.Attachment{
		Fallback: "GitHub Status: Good - https://status.github.com",
		Text:     "<https://status.github.com/|GitHub Status> : *Good*",
		Color:    "good",
		Fields:   []slack.Field{statusField, dateField},
		MrkdwnIn: []string{"text"},
	}

	payload.Attachments = []slack.Attachment{attachment}

	poster.PostPayload(payload)
}

payload

I would recommend you read Attaching content and links to messages | Slack.

Example Apps

github-status
  • Notify GitHub Site Status to Slack incoming webhook.

github-status

prnotify
  • Notify GitHub pull requests to Slack incoming webhook.
qiiotd
  • Qiita:Team (Qiita) の n 年前の今日の記事を Slack の Incoming webhook へ post する。

qiiotd

Documentation

Overview

Package slackposter posts a payload to Slack incoming webhook.

See README.md for more info.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attachment

type Attachment struct {
	AuthorIcon string   `json:"author_icon"`
	AuthorLink string   `json:"author_link"`
	AuthorName string   `json:"author_name"`
	Color      string   `json:"color"`
	Fallback   string   `json:"fallback"`
	Fields     []Field  `json:"fields"`
	FooterIcon string   `json:"footer_icon"`
	Footer     string   `json:"footer"`
	ImageURL   string   `json:"image_url"`
	MrkdwnIn   []string `json:"mrkdwn_in"`
	Pretext    string   `json:"pretext"`
	Text       string   `json:"text"`
	ThumbURL   string   `json:"thumb_url"`
	TitleLink  string   `json:"title_link"`
	Title      string   `json:"title"`
	Ts         int64    `json:"ts"`
}

Attachment represents a `attachment`.

type Config

type Config struct {
	Channel    string `json:"channel"`
	IconEmoji  string `json:"icon_emoji"`
	Username   string `json:"username"`
	WebhookURL string `json:"webhook_url"`
}

Config is the configuration of SlackPoster.

type Field

type Field struct {
	Short bool   `json:"short"`
	Title string `json:"title"`
	Value string `json:"value"`
}

Field represents a `field`.

type Payload

type Payload struct {
	Attachments []Attachment `json:"attachments"`
	Channel     string       `json:"channel"`
	IconEmoji   string       `json:"icon_emoji"`
	LinkNames   bool         `json:"link_names"`
	Mrkdwn      bool         `json:"mrkdwn"`
	Text        string       `json:"text"`
	Username    string       `json:"username"`
}

Payload is represented a Slack messsage format.

Refer to: Attaching content and links to messages | Slack https://api.slack.com/docs/message-attachments

func (*Payload) AppendField

func (payload *Payload) AppendField(field Field, attachmentIndex int)

AppendField append a Field to Attachments.

type SlackPoster

type SlackPoster struct {
	Channel    string
	DryRun     bool
	IconEmoji  string
	Username   string
	WebhookURL string
}

SlackPoster represents a poster for Slack incoming webhook. SlackPoster must be created by calling NewSlackPoster().

func NewSlackPoster

func NewSlackPoster(c Config) SlackPoster

NewSlackPoster create a new SlackPoster given a Config.

func (SlackPoster) NewPayload

func (sp SlackPoster) NewPayload() Payload

NewPayload create a new Payload.

func (SlackPoster) PostMessage

func (sp SlackPoster) PostMessage(message string) error

PostMessage posts a message to WebhookURL.

func (SlackPoster) PostPayload

func (sp SlackPoster) PostPayload(payload Payload) error

PostPayload posts a payload to WebhookURL.

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL