Skip to content

Commit

Permalink
add cmd/tg
Browse files Browse the repository at this point in the history
  • Loading branch information
a-kataev committed Apr 25, 2023
1 parent 9d7b151 commit 7102079
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
100 changes: 100 additions & 0 deletions cmd/tg/main.go
Original file line number Diff line number Diff line change
@@ -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),
)
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 7102079

Please sign in to comment.