Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
icon_url, icon_emoji, 1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Salzmann committed Mar 24, 2018
1 parent fdf0e46 commit af72dbc
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 17 deletions.
40 changes: 31 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ echo "Hello World" | 2slack
usage: 2slack [<flags>] [<message>]

Flags:
--help Show context-sensitive help (also try --help-long and --help-man).
-c, --channel=CHANNEL ... Slack Channel Name or ID
-t, --token=TOKEN Slack token
--title=TITLE Message title
--footer=FOOTER Footer to use
--color=COLOR Message color
--username=USERNAME Username to use
--version Show application version.
--help Show context-sensitive help (also try --help-long and --help-man).
-c, --channel=CHANNEL ... Slack Channel Name or ID
-t, --token=TOKEN Slack token
--title=TITLE Message title
--footer=FOOTER Footer to use
--color=COLOR Message color
--username=USERNAME Username to use
--icon_emoji=ICON_EMOJI Emoji to use as the icon
--icon_url=ICON_URL URL to an image to use as the icon
--version Show application version.

Args:
[<message>] Message text
[<message>] Text of the message to send

```

# Installation
Expand All @@ -48,7 +51,26 @@ export SLACK_CHANNEL=Channel1
export SLACK_TOKEN=MySlackToken
export SLACK_TITLE=Date
export SLACK_COLOR=green
export SLACK_USERNAME=2slack

date | 2slack
2slack "Hello World!"
```

## List of environment variables
|--------------------|------------------------|
| Name | Commandline Equivalent |
|--------------------|------------------------|
| `SLACK_CHANNEL` | `channel` |
| `SLACK_TOKEN` | `token` |
| `SLACK_TITLE` | `title` |
| `SLACK_FOOTER` | `footer` |
| `SLACK_COLOR` | `color` |
| `SLACK_USERNAME` | `username` |
| `SLACK_ICON_EMOJI` | `icon_emoji` |
| `SLACK_ICON_URL` | `icon_url` |


# Changelog
1.1.0: Added icon_emoji & icon_url
1.0.0: Initial release
46 changes: 38 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"io"
"net/url"
"os"
"strings"

Expand All @@ -11,17 +12,19 @@ import (
)

var (
channelFlag = kingpin.Flag("channel", "Slack Channel Name or ID").Short('c').Strings()
tokenFlag = kingpin.Flag("token", "Slack token").Short('t').String()
titleFlag = kingpin.Flag("title", "Message title").String()
footerFlag = kingpin.Flag("footer", "Footer to use").String()
colorFlag = kingpin.Flag("color", "Message color").String()
usernameFlag = kingpin.Flag("username", "Username to use").String()
messageArg = kingpin.Arg("message", "Message text").String()
channelFlag = kingpin.Flag("channel", "Slack Channel Name or ID").Short('c').Strings()
tokenFlag = kingpin.Flag("token", "Slack token").Short('t').String()
titleFlag = kingpin.Flag("title", "Message title").String()
footerFlag = kingpin.Flag("footer", "Footer to use").String()
colorFlag = kingpin.Flag("color", "Message color").String()
usernameFlag = kingpin.Flag("username", "Username to use").String()
iconEmojiFlag = kingpin.Flag("icon_emoji", "Emoji to use as the icon").String()
iconURLFlag = kingpin.Flag("icon_url", "URL to an image to use as the icon").URL()
messageArg = kingpin.Arg("message", "Text of the message to send").String()
)

func main() {
kingpin.Version("1.0.0")
kingpin.Version("1.1.ß")
kingpin.Parse()

if channelFlag == nil || len(*channelFlag) <= 0 {
Expand Down Expand Up @@ -90,6 +93,25 @@ func main() {
}
*usernameFlag = strings.TrimSpace(*usernameFlag)

if iconEmojiFlag == nil || len(*iconEmojiFlag) <= 0 {
iconEmojiFlag = new(string)
if env := os.Getenv("SLACK_ICON_EMOJI"); len(env) > 0 {
*iconEmojiFlag = env
} else {
*iconEmojiFlag = ""
}
}
*iconEmojiFlag = strings.TrimSpace(*iconEmojiFlag)

if iconURLFlag == nil || *iconURLFlag == nil || len((*iconURLFlag).String()) <= 0 {
if env := os.Getenv("SLACK_ICON_URL"); len(env) > 0 {
parsedUrl, err := url.Parse(strings.TrimSpace(env))
if err == nil && parsedUrl != nil {
iconURLFlag = &parsedUrl
}
}
}

// read message
if messageArg == nil || len(*messageArg) <= 0 {
var builder strings.Builder
Expand Down Expand Up @@ -174,6 +196,14 @@ func main() {
parameters.Username = *usernameFlag
}

if len(*iconEmojiFlag) > 0 {
parameters.IconEmoji = *iconEmojiFlag
}

if iconURLFlag != nil && *iconURLFlag != nil && len((*iconURLFlag).String()) > 0 {
parameters.IconURL = (*iconURLFlag).String()
}

for i := 0; i < len(channelIDs); i++ {
_, _, err := api.PostMessage(channelIDs[i], text, parameters)
if err != nil {
Expand Down

0 comments on commit af72dbc

Please sign in to comment.