Skip to content

Commit

Permalink
add stdin support (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
Piitschy authored Sep 21, 2024
1 parent b0eba5d commit 8d2b6de
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"os"
Expand Down Expand Up @@ -58,7 +59,15 @@ func main() {
return
}

messageText := pflag.Arg(0) // Get the first non-flag command-line argument.
messageText := ""

// Get text from stdin and start messageText with it
stdin, err := readStdin()
if stdin != "" && err == nil {
messageText += stdin
}

messageText += pflag.Arg(0) // Get the first non-flag command-line argument.

bot, err := tgbotapi.NewBotAPI(config.BotKey)
if err != nil {
Expand Down Expand Up @@ -98,7 +107,7 @@ func runServerMode(config *AppConfig) {
for update := range updates {
if update.Message != nil {
displayDebugData(update)
os.Exit(0)
os.Exit(0)
}
}
}
Expand Down Expand Up @@ -158,3 +167,11 @@ func notifySuccess() {
style := lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
fmt.Println(style.Render("Message sent!"))
}

func readStdin() (string, error) {
stdin, err := io.ReadAll(os.Stdin)
if err != nil {
return "", err
}
return string(stdin), nil
}

0 comments on commit 8d2b6de

Please sign in to comment.