Add proper logging, extract handlers in separate file
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
bin
|
||||
docker
|
||||
docker-compose.yml
|
||||
Makefile
|
||||
*.env
|
||||
|
||||
@@ -15,5 +15,5 @@ func parseEnv() (*config, error) {
|
||||
|
||||
type config struct {
|
||||
TelegramBotToken string `envconfig:"telegram_bot_token"`
|
||||
OwnerChatID int64 `envconfig:"owner_chat_id"`
|
||||
OwnerChatID int `envconfig:"owner_chat_id"`
|
||||
}
|
||||
|
||||
+17
-42
@@ -2,69 +2,44 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
|
||||
"github.com/nightnoryu/anon3anon/pkg/app"
|
||||
|
||||
"github.com/go-telegram/bot"
|
||||
"github.com/go-telegram/bot/models"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
|
||||
defer cancel()
|
||||
logger := initLogger()
|
||||
|
||||
conf, err := parseEnv()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
logger.Fatal(err)
|
||||
}
|
||||
|
||||
opts := []bot.Option{
|
||||
bot.WithDebug(),
|
||||
bot.WithMessageTextHandler("/start", bot.MatchTypeExact, getStartHandler()),
|
||||
bot.WithDefaultHandler(getDefaultHandler(conf)),
|
||||
bot.WithMessageTextHandler("/start", bot.MatchTypeExact, app.GetStartCommandHandler(logger)),
|
||||
bot.WithDefaultHandler(app.GetAnonymousMessagesHandler(logger, conf.OwnerChatID)),
|
||||
}
|
||||
|
||||
b, err := bot.New(conf.TelegramBotToken, opts...)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
logger.Fatal(err)
|
||||
}
|
||||
|
||||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
|
||||
defer cancel()
|
||||
|
||||
b.Start(ctx)
|
||||
}
|
||||
|
||||
func getStartHandler() bot.HandlerFunc {
|
||||
return func(ctx context.Context, b *bot.Bot, update *models.Update) {
|
||||
params := &bot.SendMessageParams{
|
||||
ChatID: update.Message.Chat.ID,
|
||||
Text: "Жду твоих сообщений!!\nОтветы будут в канале @meme_me_a_meme",
|
||||
}
|
||||
_, err := b.SendMessage(ctx, params)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func getDefaultHandler(conf *config) bot.HandlerFunc {
|
||||
return func(ctx context.Context, b *bot.Bot, update *models.Update) {
|
||||
params := &bot.CopyMessageParams{
|
||||
ChatID: conf.OwnerChatID,
|
||||
FromChatID: update.Message.Chat.ID,
|
||||
MessageID: update.Message.ID,
|
||||
}
|
||||
_, err := b.CopyMessage(ctx, params)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
}
|
||||
|
||||
sendParams := &bot.SendMessageParams{
|
||||
ChatID: update.Message.Chat.ID,
|
||||
Text: "Сообщение отправлено!",
|
||||
}
|
||||
_, err = b.SendMessage(ctx, sendParams)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
}
|
||||
}
|
||||
func initLogger() *logrus.Logger {
|
||||
logger := logrus.New()
|
||||
logger.SetOutput(os.Stdout)
|
||||
logger.SetFormatter(&logrus.JSONFormatter{})
|
||||
logger.SetLevel(logrus.WarnLevel)
|
||||
return logger
|
||||
}
|
||||
|
||||
@@ -7,4 +7,9 @@ require (
|
||||
github.com/pkg/errors v0.9.1
|
||||
)
|
||||
|
||||
require github.com/go-telegram/bot v1.13.3
|
||||
require (
|
||||
github.com/go-telegram/bot v1.13.3
|
||||
github.com/sirupsen/logrus v1.9.3
|
||||
)
|
||||
|
||||
require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
|
||||
|
||||
@@ -1,6 +1,21 @@
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/go-telegram/bot v1.13.3 h1:r2erpHI5rMQsR5TFWJ/XVqWHq9R228fcaejLFvXJsmM=
|
||||
github.com/go-telegram/bot v1.13.3/go.mod h1:i2TRs7fXWIeaceF3z7KzsMt/he0TwkVC680mvdTFYeM=
|
||||
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
|
||||
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
|
||||
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/go-telegram/bot"
|
||||
"github.com/go-telegram/bot/models"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func GetStartCommandHandler(logger *logrus.Logger) bot.HandlerFunc {
|
||||
return func(ctx context.Context, b *bot.Bot, update *models.Update) {
|
||||
params := &bot.SendMessageParams{
|
||||
ChatID: update.Message.Chat.ID,
|
||||
Text: "Жду твоих сообщений!!\nОтветы будут в канале @meme_me_a_meme",
|
||||
}
|
||||
_, err := b.SendMessage(ctx, params)
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func GetAnonymousMessagesHandler(logger *logrus.Logger, ownerChatId int) bot.HandlerFunc {
|
||||
return func(ctx context.Context, b *bot.Bot, update *models.Update) {
|
||||
params := &bot.CopyMessageParams{
|
||||
ChatID: ownerChatId,
|
||||
FromChatID: update.Message.Chat.ID,
|
||||
MessageID: update.Message.ID,
|
||||
}
|
||||
_, err := b.CopyMessage(ctx, params)
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
sendParams := &bot.SendMessageParams{
|
||||
ChatID: update.Message.Chat.ID,
|
||||
Text: "Сообщение отправлено!",
|
||||
}
|
||||
_, err = b.SendMessage(ctx, sendParams)
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user