diff --git a/slackbot-console/README.md b/slackbot-console/README.md index 913f576..54bba81 100644 --- a/slackbot-console/README.md +++ b/slackbot-console/README.md @@ -14,5 +14,13 @@ The application uses the following environment variables: Call the application passing the channel and the message through command line params: ``` - slacbot-console.exe -channel="#random" -message="All your base are belong to us!" + slackbot-console.exe -channel="#random" -message="All your base are belong to us!" ``` + +### Reading the message from stdin + +You can also pass a message from the stding, example: + + type file.txt | slackbot-console.exe -channel="#random" -message="Title" + +In this case, the contents from the stdin are enclosed by ``` to be displayed as pre formatted text on slack. diff --git a/slackbot-console/main.go b/slackbot-console/main.go index 38492cd..ee07d53 100644 --- a/slackbot-console/main.go +++ b/slackbot-console/main.go @@ -1,28 +1,47 @@ package main import ( + "bytes" + "flag" + "fmt" "github.com/cadena-monde/slackbot" "os" - "flag" + "strings" ) var ( channel, message string - slackURL = os.Getenv("SLACKBOT_URL") - token = os.Getenv("SLACKBOT_TOKEN") + stdin bool + slackURL = os.Getenv("SLACKBOT_URL") + token = os.Getenv("SLACKBOT_TOKEN") ) func main() { parseFlags() - postMessage(); + if stdin { + readFromStdin() + } + postMessage() +} + +func readFromStdin() { + input := bytes.NewBuffer([]byte{}) + input.ReadFrom(os.Stdin) + + inputString := input.String() + if inputString != "" { + message = fmt.Sprintf("%s\n```%s```", message, input.String()) + message = strings.Replace(message, "\r", "", -1) + } } func parseFlags() { flag.StringVar(&channel, "channel", "", "Channel. Ex: #random") flag.StringVar(&message, "message", "", "Message to be sent to the channel") + flag.BoolVar(&stdin, "stdin", false, "Read message from stdin") flag.Parse() - if channel == "" || message == "" { + if channel == "" || (message == "" && !stdin) { flag.Usage() os.Exit(1) }