You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.4 KiB

package main
import (
"fmt"
"log"
"net/http"
"os"
"strings"
"github.com/akamensky/argparse"
)
func main() {
// Create new parser object
parser := argparse.NewParser("messenger", "Generic messenger that notifies through multiple channels")
// Create webhook flag
webhookFlag := parser.Flag("", "webhook", &argparse.Options{Help: "Use this flag to enable webhook sending"})
webhookURL := parser.String("", "url", &argparse.Options{Help: "URL to be used for webhook"})
// Parse input
err := parser.Parse(os.Args)
if err != nil {
fmt.Print(parser.Usage(err))
return
}
// Check inputs are set
if *webhookFlag {
if webhookURL == nil || *webhookURL == "" {
fmt.Println("Webhook URL needs to be set if webhook is enabled.")
return
}
}
// Checks if at least one flag is enabled
if *webhookFlag || false {
handleStdIn(webhookFlag, webhookURL)
} else {
fmt.Print(parser.Usage(err))
}
}
func handleStdIn(webhookFlag *bool, webhookURL *string) {
for {
var currStr string
_, err := fmt.Scanf("%s", &currStr)
if err != nil {
return
}
// Handle string
if *webhookFlag {
err = sendWebhook(&currStr, webhookURL)
if err != nil {
log.Println(err)
}
}
}
}
func sendWebhook(currStr *string, webhookURL *string) error {
bodyReader := strings.NewReader(*currStr)
_, err := http.Post(*webhookURL, "text/plain", bodyReader)
return err
}