From a0f9a6347a8dce6a5d2176bb7eb00c8d65ef3bc2 Mon Sep 17 00:00:00 2001 From: nightnoryu Date: Sun, 28 Jul 2024 19:32:09 +0300 Subject: [PATCH] Add separate questions service and implement simple bot behavior --- cmd/anon3anon/main.go | 51 ++++++++++++++-------- go.mod | 5 ++- go.sum | 2 + pkg/app/anonymousmessagesservice.go | 66 +++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 18 deletions(-) create mode 100644 pkg/app/anonymousmessagesservice.go diff --git a/cmd/anon3anon/main.go b/cmd/anon3anon/main.go index 73d8288..447fbff 100644 --- a/cmd/anon3anon/main.go +++ b/cmd/anon3anon/main.go @@ -3,36 +3,53 @@ package main import ( "log" "os" + "strconv" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + "github.com/nightnoryu/anon3anon/pkg/app" ) -const telegramBotTokenEnvKey = "TELEGRAM_BOT_TOKEN" +const ( + telegramBotTokenEnvKey = "TELEGRAM_BOT_TOKEN" + ownerChatIDEnvKey = "OWNER_CHAT_ID" +) + +type config struct { + Token string + OwnerChatID int64 +} + +func getConfig() (config, error) { + token := os.Getenv(telegramBotTokenEnvKey) + ownerChatID, err := strconv.ParseInt(os.Getenv(ownerChatIDEnvKey), 10, 64) + if err != nil { + return config{}, err + } + + return config{ + Token: token, + OwnerChatID: ownerChatID, + }, nil +} func main() { - token := os.Getenv(telegramBotTokenEnvKey) - bot, err := tgbotapi.NewBotAPI(token) + config, err := getConfig() if err != nil { log.Panic(err) } - bot.Debug = true + bot, err := tgbotapi.NewBotAPI(config.Token) + if err != nil { + log.Panic(err) + } + + //bot.Debug = true log.Printf("Authorized on account %s", bot.Self.UserName) - u := tgbotapi.NewUpdate(0) - u.Timeout = 60 + service := app.NewAnonymousQuestionsService(bot, config.OwnerChatID) - updates := bot.GetUpdatesChan(u) - - for update := range updates { - if update.Message != nil { // If we got a message - log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text) - - msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text) - msg.ReplyToMessageID = update.Message.MessageID - - bot.Send(msg) - } + if err := service.ListenForMessages(); err != nil { + log.Panic(err) } } diff --git a/go.mod b/go.mod index 7573eed..5f7403d 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/nightnoryu/anon3anon go 1.22.5 -require github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 // indirect +require ( + github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 // indirect + github.com/pkg/errors v0.9.1 // indirect +) diff --git a/go.sum b/go.sum index db8e45c..ae23520 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,4 @@ github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc= github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= diff --git a/pkg/app/anonymousmessagesservice.go b/pkg/app/anonymousmessagesservice.go new file mode 100644 index 0000000..bb69d8a --- /dev/null +++ b/pkg/app/anonymousmessagesservice.go @@ -0,0 +1,66 @@ +package app + +import ( + "log" + + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" +) + +func NewAnonymousQuestionsService(bot *tgbotapi.BotAPI, ownerChatID int64) AnonymousMessagesService { + return &anonymousMessagesService{ + bot: bot, + ownerChatID: ownerChatID, + } +} + +type AnonymousMessagesService interface { + ListenForMessages() error +} + +type anonymousMessagesService struct { + bot *tgbotapi.BotAPI + ownerChatID int64 +} + +func (s *anonymousMessagesService) ListenForMessages() error { + u := tgbotapi.NewUpdate(0) + u.Timeout = 60 + + updates := s.bot.GetUpdatesChan(u) + for update := range updates { + if update.Message == nil { + continue + } + + log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text) + + if update.Message.Text == "/start" { + reply := tgbotapi.NewMessage(update.Message.Chat.ID, "Жду твоих вопросов!") + s.bot.Send(reply) + continue + } + + if len(update.Message.Photo) > 0 { + var photos []interface{} + photo := tgbotapi.NewInputMediaPhoto(tgbotapi.FileID(update.Message.Photo[0].FileID)) + photo.Caption = "*Новое анонимное сообщение!*" + photo.ParseMode = tgbotapi.ModeMarkdown + photos = append(photos, photo) + + mediaMsg := tgbotapi.NewMediaGroup(s.ownerChatID, photos) + + s.bot.Send(mediaMsg) + } else { + msg := tgbotapi.NewMessage(s.ownerChatID, "*Новое анонимное сообщение!*\n\n"+update.Message.Text) + msg.ParseMode = tgbotapi.ModeMarkdown + + s.bot.Send(msg) + } + + reply := tgbotapi.NewMessage(update.Message.Chat.ID, "Сообщение отправлено!") + + s.bot.Send(reply) + } + + return nil +}