Skip to content

colegatron/slacktee

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

slacktee

slacktee is a bash script that works like tee command. Instead of writing the standard input to files, slacktee posts it to Slack.

Image

Requirements

slacktee uses curl command to communicate with Slack.

Installation

# Clone git repository
git clone https://github.com/course-hero/slacktee.git

# Install slacktee.sh
bash ./slacktee/install.sh

install.sh copies slacktee.sh in /usr/local/bin and sets executable permission.

If you'd like to install it in the different directory such as /usr/bin, pass the target directory as a parameter of install.sh. By default, /usr/local/bin may not be included in your $PATH environment variable (you should be aware of this when you use slacktee in crontab). So, if you would like to use slacktee without specifying its full path, coping it to /usr/bin may be a good idea.

# Install slacktee.sh in /usr/bin
bash ./slacktee/install.sh /usr/bin

Also, you can rename slacktee.sh during the installation. If you would like to give a different name to slacktee.sh, simply append it to the target directory.

# Install slacktee.sh in /usr/local/bin as 'slacktee'
bash ./slacktee/install.sh /usr/local/bin/slacktee

After the installation, interactive setup starts automatically.

Configuration

Before start using slacktee, please set following variables in the script configuration file. slacktee reads the global configuration (/etc/slacktee.conf) first, then reads your local configuration (~/.slacktee). You can set up your local configuration file using interactive setup mode (--setup option).

For more details about tokens, visit Slack's API page.

webhook_url=""      # Incoming Webhooks integration URL. See https://my.slack.com/services/new/incoming-webhook
upload_token=""     # The user's API authentication token, only used for file uploads. See https://api.slack.com/#auth
channel=""          # Default channel to post messages. '#' is prepended, if it doesn't start with '#' or '@'.
tmp_dir="/tmp"      # Temporary file is created in this directory.
username="slacktee" # Default username to post messages.
icon="ghost"        # Default emoji or a direct url to an image to post messages. You don't have to wrap emoji with ':'. See http://www.emoji-cheat-sheet.com.
attachment=""       # Default color of the attachments. If an empty string is specified, the attachments are not used.

Usage

Feed input to slacktee through the pipe.

usage: slacktee.sh [options]
  options:
    -h, --help                        Show this help.
    -n, --no-buffering                Post input values without buffering.
    -f, --file                        Post input values as a file.
    -l, --link                        Add a URL link to the message.
    -c, --channel channel_name        Post input values to specified channel or user.
    -u, --username user_name          This username is used for posting.
    -i, --icon emoji_name|url         This icon is used for posting. You can use a word
                                      from http://www.emoji-cheat-sheet.com or a direct url to an image.
    -t, --title title_string          This title is added to posts.
    -m, --message-formatting format   Switch message formatting (none|link_names|full).
                                      See https://api.slack.com/docs/formatting for more details.
    -p, --plain-text                  Don't surround the post with triple backticks.
    -a, --attachment [color]          Use attachment (richly-formatted message)
                                      Color can be 'good','warning','danger' or any hex color code (eg. #439FE0)
                                      See https://api.slack.com/docs/attachments for more details.
    -e, --field title value           Add a field to the attachment. You can specify this multiple times.
    -s, --short-field title value     Add a short field to the attachment. You can specify this multiple times.
    -o, --cond-color color pattern    Change the attachment color if the specified Regex pattern matches the input.
                                      You can specify this multile times.
                                      If more than one pattern matches, the latest matched pattern is used.
    -d, --cond-prefix prefix pattern  This prefix is added to the message, if the specified Regex pattern matches the input.
                                      You can specify this multile times.
                                      If more than one pattern matches, the latest matched pattern is used.
    --config config_file              Specify the location of the config file.
    --setup                           Setup slacktee interactively.

If you'd like to post the output of ls command, you can do it like this.

ls | slacktee.sh

To post the output of tail -f command line by line, use -n option.

tail -f foobar.log | slacktee.sh -n

To post the output of find command as a file, use -f option.

find /var -name "foobar" | slacktee.sh -f

You can specify channel, username, icon, title, and link too.

ls | slacktee.sh -c "general" -u "slacktee" -i "shipit" -t "ls" -l "http://en.wikipedia.org/wiki/Ls"

Instead of emoji icon, you may provide an image url.

ls | slacktee.sh -c "general" -u "slacktee" -i "http://mirrors.creativecommons.org/presskit/icons/cc.png" -t "ls" -l "http://en.wikipedia.org/wiki/Ls"

Of course, you can connect another command with pipe.

ls | slacktee.sh | email "ls" foo@example.com

Would you like to use richly-formatted message? Use -a, -e and -s options.

cat error.log | slacktee.sh -a "danger" -e "Date and Time" "$(date)" -s "Host" "$(hostname)"

Direct message to your teammate 'chuck'? Easy!

echo "Submit Your Expense Reimbursement Form By Friday!" | slacktee.sh -c "@chuck"

Conditional coloring and prefix helps you to notice important messages easier. If a specified Regex pattern matches the input, its corresponding color or prefix is used for posting the message. In the example below, the message color is green (good) by default, but the color becomes yellow (warning) if an input log starts with "Warning:". Also, it becomes red (danger) and the prefix @channel is added to the mssage if the log starts with "Error:". It's pretty useful, isn't it?

# To enable @command, '-m link_names' must be specified
tail -f app.log | slacktee.sh -n -a "good" -o "warning" "^Warning:" -o "danger" "^Error:" -d "@channel" "^Error:" -m link_names

You can find more examples on Course Hero blog.

Travis-CI Integration

You may want to integrate slacktee into Travis-CI in order to send additional logging information to your Slack channel. In this case, it is recommended that you do not expose your Incoming WebHook URL and API authentication token as plaintext values inside your slacktee.conf file.

Instead, use the encrypt command of the Travis client to set the SLACKTEE_WEBHOOK and SLACKTEE_TOKEN environment variables, and leave the webhook_url and upload_token values in your slacktee.conf empty. When slacktee runs, it will give priority to the environment variables, which Travis-CI will decrypt and set automatically during the build process. In this way those two values are kept secure.

Example

Modify slacktee.conf

webhook_url=""
upload_token=""
channel="Travis-CI"
tmp_dir="/tmp"
username="slacktee"
icon="ghost"

Add the encrypted environment variables to the .travis.yml file in your git repository

travis encrypt SLACKTEE_WEBHOOK='https://hooks.slack.com/services/afternoonTEE/BMP2vsT72/ohNoDontTellUs' --add
travis encrypt SLACKTEE_TOKEN='yoho-0987654321-1234567890-4488116622-abc123' --add

Looking at your travis.yml you will now see the following added

env:
  global:
  - secure: 2YZabH8+UdzqyBWckojRDP9uudnCSYyxOOx1y85el69YdHwLDMD+dt49rAgIrmCWsWCWpUZ0ZRWV8vU2VFMffIhmikiqG7VoKHuN5PyY8qBwr9hq/ZI8gdwgjgfRIGtv/U89BTjMmc1g/6nJkSvMtiSUSK3Lopg0JCyuZsiyhzs=
  - secure: TKpohmywdOneQkqGxJiF+S1N8oCdTWWGsXgjzNXWSvb23KDtvGq/W2SpWdFdwEHC9Y8NymoAPYRSW8MUQoiJ7NaQ1eZQuyx6/orjHpIgqiAuHrOSaMagzpKVG6Gtb87qDgov65ZOasyex1OtPQdfFtZBX67B6IVXkRPV+IA/+UX=

An example travis.yml section using slacktee may look like:

after_failure:
- ls /path/to/build | ./slacktee.sh -t "$TRAVIS_REPO_SLUG $TRAVIS_JOB_NUMBER build directory"
- cat /path/to/some.log | ./slacktee.sh -t "$TRAVIS_REPO_SLUG $TRAVIS_JOB_NUMBER some.log"

About

slacktee is a bash script that works like tee command. Instead of writing the standard input to files, slacktee posts it to Slack.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Shell 100.0%