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

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Eun committed Mar 20, 2018
1 parent 00445a5 commit 9c6246c
Show file tree
Hide file tree
Showing 125 changed files with 20,155 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Godeps/Readme

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2018 Tobias Salzmann

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# 2slack

Send a message to slack

```bash
echo "Hello World" | 2slack
```

## Usage
```json
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.

Args:
[<message>] Message text
```

# Installation
You find releases in [Github Releases](releases) section.

Or you can use `go get`:
```bash
go get github.com/Eun/2slack
```

You can find instructions for slack [here](slack/README.md)

# Examples

```bash
2slack --channel=Channel1 --token=MySlackToken --title=Title --color=green "Hello from 2slack"
date | 2slack --channel=Channel1 --token=MySlackToken --title=Title --color=ff0000
```

## Using environment variables
```bash

export SLACK_CHANNEL = Channel1
export SLACK_TOKEN = MySlackToken
export SLACK_TITLE = Date
export SLACK_COLOR = green

date | 2slack
2slack "Hello World!"
```
184 changes: 184 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
package main

import (
"fmt"
"io"
"os"
"strings"

"github.com/nlopes/slack"
"gopkg.in/alecthomas/kingpin.v2"
)

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()
)

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

if channelFlag == nil || len(*channelFlag) <= 0 {
if env := os.Getenv("SLACK_CHANNEL"); len(env) > 0 {
channelFlag = new([]string)
parts := strings.Split(env, ",")
for i := 0; i < len(parts); i++ {
if part := strings.TrimSpace(parts[i]); len(part) > 0 {
*channelFlag = append(*channelFlag, part)
}
}
} else {
fmt.Println("No environment variable `SLACK_CHANNEL' present")
os.Exit(1)
}
}

if tokenFlag == nil || len(*tokenFlag) <= 0 {
if env := os.Getenv("SLACK_TOKEN"); len(env) > 0 {
tokenFlag = new(string)
*tokenFlag = env
} else {
fmt.Println("No environment variable `SLACK_TOKEN' present")
os.Exit(1)
}
}
*tokenFlag = strings.TrimSpace(*tokenFlag)

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

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

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

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

// read message
if messageArg == nil || len(*messageArg) <= 0 {
var builder strings.Builder
_, err := io.Copy(&builder, os.Stdin)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
messageArg = new(string)
*messageArg = builder.String()
}

if len(strings.TrimSpace(*messageArg)) <= 0 {
return
}

// get channel ID
var channelIDs []string
api := slack.New(*tokenFlag)
channels, err := api.GetChannels(true)
if err != nil {
fmt.Println("Unable to receive channel list:")
fmt.Println(err)
os.Exit(1)
}

for _, channel := range channels {
for i := 0; i < len(*channelFlag); i++ {
if strings.EqualFold(channel.NameNormalized, (*channelFlag)[i]) || strings.EqualFold(channel.Name, (*channelFlag)[i]) || strings.EqualFold(channel.ID, (*channelFlag)[i]) {
channelIDs = append(channelIDs, channel.ID)
}
}
}

groups, err := api.GetGroups(true)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to receive (private) channel list:\n")
fmt.Fprintln(os.Stderr, err)
} else {
for _, channel := range groups {
for i := 0; i < len(*channelFlag); i++ {
if strings.EqualFold(channel.NameNormalized, (*channelFlag)[i]) || strings.EqualFold(channel.Name, (*channelFlag)[i]) || strings.EqualFold(channel.ID, (*channelFlag)[i]) {
channelIDs = append(channelIDs, channel.ID)
}
}
}
}

if len(channelIDs) <= 0 {
fmt.Println("No channels found")
os.Exit(1)
}

var text string

parameters := slack.PostMessageParameters{
AsUser: false,
}

if len(*titleFlag) > 0 || len(*colorFlag) > 0 || len(*footerFlag) > 0 || len(*messageArg) > 1024 {
var attachment slack.Attachment

if len(*titleFlag) > 0 {
attachment.Title = *titleFlag
}
if len(*footerFlag) > 0 {
attachment.Footer = *footerFlag
}
if len(*colorFlag) > 0 {
attachment.Color = *colorFlag
}
attachment.Text = *messageArg

parameters.Attachments = []slack.Attachment{
attachment,
}
} else {
text = *messageArg
}

if len(*usernameFlag) > 0 {
parameters.Username = *usernameFlag
}

for i := 0; i < len(channelIDs); i++ {
_, _, err := api.PostMessage(channelIDs[i], text, parameters)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
}
23 changes: 23 additions & 0 deletions slack/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## Slack Installation

To use **2slack** you need an Slack Token that has the needed permissions.

### Goto [slack API](https://api.slack.com/apps) and `Create New App`
![Step1](step1.png)
### Use a good name for the `App Name` (e.g. `2slack`)
### Select your Workspace in `Development Slack Workspace`
![Step2](step2.png)
### Navigate to your App `OAuth & Permissions` setting
![Step3](step3.png)
### Scroll to `Scopes` and add the Permissions
- Access information about user’s public channels (`channels:read`)
- Send messages as *App-Name* (`chat:write:bot`)
- Access information about user’s private channels (`groups:read`)


![Step4](step4.png)
### Install and Authorize the App
![Step5](step5.png)
![Step6](step6.png)
### You find the token in the App settings
![Step7](step7.png)
Binary file added slack/step1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added slack/step2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added slack/step3.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added slack/step4.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added slack/step5.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added slack/step6.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added slack/step7.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions vendor/github.com/alecthomas/template/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions vendor/github.com/alecthomas/template/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9c6246c

Please sign in to comment.