diff --git a/cmd/tg/main.go b/cmd/tg/main.go new file mode 100644 index 0000000..055a5f4 --- /dev/null +++ b/cmd/tg/main.go @@ -0,0 +1,100 @@ +package main + +import ( + "context" + "errors" + "flag" + "fmt" + "io" + "os" + + "github.com/a-kataev/tg" + "golang.org/x/exp/slog" +) + +func main() { + fset := flag.NewFlagSet("", flag.ContinueOnError) + fset.SetOutput(io.Discard) + + token := fset.String("token", "", "token") + chatID := fset.Int64("chat_id", 0, "chat id") + text := fset.String("text", "", "text") + parseMode := fset.String("parse_mode", "Markdown", "parse mode") + + log := slog.New(slog.NewJSONHandler(os.Stdout)) + + logFatal := func(msg string) { + log.Error(msg) + + os.Exit(1) + } + + err := fset.Parse(os.Args[1:]) + if err != nil { + if errors.Is(flag.ErrHelp, err) { + fmt.Fprintln(os.Stderr, "tg - telegram bot 🤖 send message ✉️") + fset.SetOutput(os.Stderr) + fset.Usage() + + os.Exit(1) + } + + logFatal(err.Error()) + } + + if *token == "" { + *token = os.Getenv("TOKEN") + } + + if *token == "" { + logFatal("empty token") + } + + if *chatID < 1 { + logFatal("invalid chat id") + } + + if *text == "" { + logFatal("empty text") + } + + if *text == "-" { + const maxTextSize int64 = 4096 + + stdin, err := io.ReadAll(io.LimitReader(os.Stdin, maxTextSize)) + if err != nil { + if !errors.Is(err, io.EOF) { + logFatal(err.Error()) + } + } + + *text = string(stdin) + } + + tg, err := tg.NewTG( + *token, + tg.MessageParseMode(tg.ParseMode(*parseMode)), + ) + if err != nil { + logFatal(err.Error()) + } + + ctx := context.Background() + + bot, err := tg.GetMe(ctx) + if err != nil { + logFatal(err.Error()) + } + + log = log.With(slog.String("bot_name", bot.UserName)) + + msg, err := tg.SendMessage(ctx, *chatID, *text) + if err != nil { + logFatal(err.Error()) + } + + log.Info("Success send message", + slog.Int64("chat_id", *chatID), + slog.Int("message_id", msg.MessageID), + ) +} diff --git a/go.mod b/go.mod index caa87fd..95eb08c 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,10 @@ module github.com/a-kataev/tg go 1.20 -require github.com/stretchr/testify v1.8.2 +require ( + github.com/stretchr/testify v1.8.2 + golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 +) require ( github.com/davecgh/go-spew v1.1.1 // indirect diff --git a/go.sum b/go.sum index 74f5036..c7fd8ea 100644 --- a/go.sum +++ b/go.sum @@ -11,6 +11,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 h1:5llv2sWeaMSnA3w2kS57ouQQ4pudlXrR0dCgw51QK9o= +golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=