notify_slack
notify_slack copied to clipboard
To post a message to Slack from the command line
notify_slack
Post to Slack from the command line. If you pass the standard output of the command to notify_slack by pipe, it will post to slack once a second (can be changed with the -interval option).
https://user-images.githubusercontent.com/1249910/155869750-48f7500f-4481-49b6-9d65-b93205f2b94f.mp4
(same movie) https://www.youtube.com/watch?v=wmKSr9Aoz-Y
Installation
I recommend you to use the binaries on GitHub Releases. Please download the latest version and use it.
If you have a development environment for the Go language, you can compile and install it by yourself.
go install github.com/catatsuy/notify_slack/cmd/notify_slack@latest
If you want to develop it, you can use the make. It requires Go 1.17 or higher.
make
If you use make, the output of notify_slack -version is git commit ID.
usage
./bin/notify_slack posts to Slack. You specify the setting in command line option or toml setting file.
If both settings are specified, command line option will always take precedence.
./bin/output | ./bin/notify_slack
./bin/output is used for testing. While buffering, to post to slack.
./bin/notify_slack README.md
You post the file as a snippet. token and channel is required to use the Slack Web API.
If you want to upload to snippet via standard input, you must specify -snippet. If you specify filename, you can change the file name on Slack.
git diff | ./bin/notify_slack -snippet -filename git.diff
Slack's API can specify filetype. You can also specify -filetype. But it is automatically determined from the extension of the file.
You make sure to give the appropriate extension.
CLI options
-c string
config file name
-channel string
specify channel (unavailable for new Incoming Webhooks)
-filename string
specify a file name (for uploading to snippet)
-filetype string
specify a filetype (for uploading to snippet)
-icon-emoji string
specify icon emoji (unavailable for new Incoming Webhooks)
-interval duration
interval (default 1s)
-slack-url string
slack url (Incoming Webhooks URL)
-snippet
switch to snippet uploading mode
-token string
token (for uploading to snippet)
-username string
specify username (unavailable for new Incoming Webhooks)
-version
Print version information and quit
toml configuration file
By default check the following files.
- a file specified with
-c $HOME/.notify_slack.toml$HOME/etc/notify_slack.toml/etc/notify_slack.toml
The contents of the toml file are as follows.
[slack]
url = "https://hooks.slack.com/services/**"
token = "xoxp-xxxxx"
channel = "#general"
username = "tester"
icon_emoji = ":rocket:"
interval = "1s"
Note:
urlis necessary if you want to post to slack as text.- You can specify
channel,username,icon_emojiandinterval. - Now, you cannot override
channel,username,icon_emojidue to the specification change of Incoming Webhooks. Please refer to https://api.slack.com/messaging/webhooks#advanced_message_formatting - Incoming Webhooks url can be created on https://slack.com/services/new/incoming-webhook
- You can specify
tokenandchannelis necessary if you want to post to snippet.usernameandicon_emojiare ignored in this case.- Please see the next section for how to create token.
Tips:
- If you want to default to another channel only for snippet, you can use
snippet_channel.
How to create a token
You need to create a token if you use snippet uploading mode.
Create New App
At first, you need to create new app. Please access https://api.slack.com/apps.
- click
Create New Appand clickFrom scratch - input application name to
App Name - select your workspace on
Pick a workspace to develop your app in: - click
Create App
Basic Information
- click
PermissionsonAdd features and functionality - select
files:writeonScopesand clickSave Changes. You are able to chooseBot Token ScopesorUser Token Scopes
OAuth & Permissions
- click
Install to WorkspaceonOAuth Tokens for Your Workspace - install your app
- copy
OAuth Access Tokenbeginning withxoxp-orBot User OAuth Access Tokenbeginning withxoxb-
Add apps
- click channel name on the channel which you want to post
- click
IntegrationsandAdd an AppinApps - choose your app
(Advanced) Environment Variables
Some settings can be given by the following environment variables.
NOTIFY_SLACK_WEBHOOK_URL
NOTIFY_SLACK_TOKEN
NOTIFY_SLACK_CHANNEL
NOTIFY_SLACK_SNIPPET_CHANNEL
NOTIFY_SLACK_USERNAME
NOTIFY_SLACK_ICON_EMOJI
NOTIFY_SLACK_INTERVAL
It will be useful if you want to use it on a container. If you use it, you don't need a configuration file anymore.