slacknotifier

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2021 License: Apache-2.0 Imports: 3 Imported by: 0

README

Slack Notifier

A command line tool and a GitHub Action to post message notifications to Slack.

This app uses the latest recommended, non-legacy Slack API to post messages, including Block Kit for the messages.

GitHub Action

See action.yml for details on inputs and outputs.

Example:

- uses: ronny/slacknotifier@v1
  with:
    slack-token: ${{ secrets.SLACK_TOKEN }}
    bot-name: deploybot
    bot-icon-emoji: ":mega:"
    channel-id: "C12345"
    message: "Deployed `todo-service`"
    message-context: "Env: `production` | By: `${{ github.actor }}` | Commit: `${{ github.sha }}`"

You can also use the docker image directly, but you need to specify all of the inputs including the ones that have default values in action.yml.

- uses: docker://ronny/slacknotifier:1
  with:
    slack-token: ${{ secrets.SLACK_TOKEN }}
    bot-name: deploybot
    bot-icon-emoji: ":mega:"
    channel-id: "C12345"
    message: "Deployed `todo-service`"
    message-context: "Env: `production` | By: `${{ github.actor }}` | Commit: `${{ github.sha }}`"
    # These are the defaults:
    markdown: 'true'
    replace-timestamp: ''
    timeout: 30s

CLI tool

go install github.com/ronny/slacknotifier/cmd/slack-notify
$GOPATH/bin/slack-notify

Or, if you have the source locally:

make install
$GOPATH/bin/slack-notify

Slack token

The recommended token type is a bot token. Follow the guide to create a new Slack app, the scopes needed are: chat:write, chat:write.public (if you want the bot to be able to post messages without being invited to a channel), and chat:write.customize (if you want to customize the bot's name and icon in a step).

Once you created your app, you can find the bot token from the "OAuth & Permissions" section of your app, the URL of the page looks like https://api.slack.com/apps/Axxxxxxxx/oauth.

Channel ID

It’s recommended to use a channel’s canonical ID, which can be found at the bottom of the pop-up dialog when you click a channel’s name.

Using #name or just name sometimes work, but not when updating/replacing a message. So it’s best to use the canonical channel ID always.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Notifier

type Notifier struct {
	BotUsername  string
	BotIconEmoji string
	Client       SlackClient
}

func (*Notifier) Notify

func (n *Notifier) Notify(ctx context.Context, input NotifyInput) (*NotifyOutput, error)

type NotifyInput

type NotifyInput struct {
	ChannelID                   string
	Message                     string
	MessageContext              string
	Markdown                    bool
	TimestampOfMessageToReplace string
}

type NotifyOutput

type NotifyOutput struct {
	ChannelID string
	Timestamp string
}

type SlackClient

type SlackClient interface {
	UpdateMessageContext(ctx context.Context, channelID string, timestamp string, options ...slack.MsgOption) (_chan string, _timestamp string, _text string, err error)
	PostMessageContext(ctx context.Context, channelID string, options ...slack.MsgOption) (_chan string, _timestamp string, err error)
}

Directories

Path Synopsis
cmd
slack-notify command

Jump to

Keyboard shortcuts

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