From 548c5817da462d005f331b22bd58ff49ae209f2b Mon Sep 17 00:00:00 2001 From: nightnoryu Date: Sun, 4 Aug 2024 11:20:01 +0300 Subject: [PATCH] Add video support, extract command handler --- cmd/anon3anon/main.go | 7 ++++-- pkg/app/anonymousmessagesservice.go | 35 +++++++++++------------------ pkg/app/botapi.go | 14 ++++++++---- pkg/app/commandhandler.go | 31 +++++++++++++++++++++++++ pkg/infrastructure/botapi.go | 33 +++++++++++++++++++++++++++ 5 files changed, 92 insertions(+), 28 deletions(-) create mode 100644 pkg/app/commandhandler.go diff --git a/cmd/anon3anon/main.go b/cmd/anon3anon/main.go index 6a78327..086f190 100644 --- a/cmd/anon3anon/main.go +++ b/cmd/anon3anon/main.go @@ -20,9 +20,12 @@ func main() { } log.Printf("Authorized on account %s", bot.Self.UserName) - botAPI := infrastructure.NewBotAPI(bot, conf.OwnerChatID) errorsChan := make(chan error) - service := app.NewAnonymousQuestionsService(botAPI, errorsChan) + botAPI := infrastructure.NewBotAPI(bot, conf.OwnerChatID) + + commandHandler := app.NewCommandHandler(botAPI) + + service := app.NewAnonymousQuestionsService(errorsChan, commandHandler, botAPI) go func() { for err := range errorsChan { log.Println(err) diff --git a/pkg/app/anonymousmessagesservice.go b/pkg/app/anonymousmessagesservice.go index 26400eb..6ad0ccf 100644 --- a/pkg/app/anonymousmessagesservice.go +++ b/pkg/app/anonymousmessagesservice.go @@ -1,9 +1,14 @@ package app -func NewAnonymousQuestionsService(api BotAPI, errorsChan chan error) AnonymousMessagesService { +func NewAnonymousQuestionsService( + errorsChan chan error, + commandHandler CommandHandler, + api BotAPI, +) AnonymousMessagesService { return &anonymousMessagesService{ - api: api, - errorsChan: errorsChan, + errorsChan: errorsChan, + commandHandler: commandHandler, + api: api, } } @@ -17,14 +22,15 @@ type AnonymousMessagesService interface { } type anonymousMessagesService struct { - api BotAPI - errorsChan chan error + errorsChan chan error + commandHandler CommandHandler + api BotAPI } func (s *anonymousMessagesService) ServeMessages() error { return s.api.HandleUpdates(func(update MessageUpdate) { if update.Command != nil { - err := s.handleCommand(update) + err := s.commandHandler.HandleCommand(update) if err != nil { s.errorsChan <- err } @@ -43,22 +49,6 @@ func (s *anonymousMessagesService) ServeMessages() error { }) } -func (s *anonymousMessagesService) handleCommand(update MessageUpdate) error { - if update.Command == nil { - return nil - } - - var msgText string - switch *update.Command { - case StartCommand: - msgText = "Жду твоих вопросов!" - case UnknownCommand: - msgText = "Неизвестная команда!" - } - - return s.api.SendMessage(update.FromChatID, Message{Text: msgText}) -} - func (s *anonymousMessagesService) pingClient(chatID int64) error { return s.api.SendMessage(chatID, Message{Text: messageSentReply}) } @@ -68,5 +58,6 @@ func (s *anonymousMessagesService) handleMessage(message Message) error { return s.api.SendMessageToOwner(Message{ Text: msgText, Image: message.Image, + Video: message.Video, }) } diff --git a/pkg/app/botapi.go b/pkg/app/botapi.go index b2b4526..41826c9 100644 --- a/pkg/app/botapi.go +++ b/pkg/app/botapi.go @@ -18,6 +18,15 @@ type MessageUpdate struct { type Message struct { Text string Image *Image + Video *Video +} + +type Image struct { + FileID string +} + +type Video struct { + FileID string } type Command int @@ -25,8 +34,5 @@ type Command int const ( UnknownCommand Command = iota StartCommand + InfoCommand ) - -type Image struct { - FileID string -} diff --git a/pkg/app/commandhandler.go b/pkg/app/commandhandler.go new file mode 100644 index 0000000..04b8538 --- /dev/null +++ b/pkg/app/commandhandler.go @@ -0,0 +1,31 @@ +package app + +func NewCommandHandler(api BotAPI) CommandHandler { + return &commandHandler{ + api: api, + } +} + +type CommandHandler interface { + HandleCommand(update MessageUpdate) error +} + +type commandHandler struct { + api BotAPI +} + +func (h *commandHandler) HandleCommand(update MessageUpdate) error { + if update.Command == nil { + return nil + } + + var msgText string + switch *update.Command { + case StartCommand: + msgText = "Жду твоих вопросов!" + case UnknownCommand: + msgText = "Неизвестная команда!" + } + + return h.api.SendMessage(update.FromChatID, Message{Text: msgText}) +} diff --git a/pkg/infrastructure/botapi.go b/pkg/infrastructure/botapi.go index e24ca80..f5da364 100644 --- a/pkg/infrastructure/botapi.go +++ b/pkg/infrastructure/botapi.go @@ -14,6 +14,7 @@ const ( messageParseMode = tgbotapi.ModeMarkdown startCommand = "start" + infoCommand = "info" ) func NewBotAPI(bot *tgbotapi.BotAPI, ownerChatID int64) app.BotAPI { @@ -58,6 +59,10 @@ func (api *botAPI) SendMessage(chatID int64, message app.Message) error { return api.sendPhotoMessage(chatID, message) } + if message.Video != nil { + return api.sendVideoMessage(chatID, message) + } + return api.sendTextMessage(chatID, message) } @@ -74,6 +79,7 @@ func (api *botAPI) hydrateMessage(msg *tgbotapi.Message) app.Message { return app.Message{ Text: text, Image: api.hydrateImage(msg.Photo), + Video: api.hydrateVideo(msg.Video), } } @@ -96,6 +102,14 @@ func (api *botAPI) sendPhotoMessage(chatID int64, message app.Message) error { return errors.WithStack(err) } +func (api *botAPI) sendVideoMessage(chatID int64, message app.Message) error { + video := api.prepareVideo(message) + mediaMsg := tgbotapi.NewMediaGroup(chatID, video) + + _, err := api.bot.Send(mediaMsg) + return errors.WithStack(err) +} + func (api *botAPI) hydrateCommand(msg *tgbotapi.Message) *app.Command { if !msg.IsCommand() { return nil @@ -105,6 +119,8 @@ func (api *botAPI) hydrateCommand(msg *tgbotapi.Message) *app.Command { switch msg.Command() { case startCommand: cmd = app.StartCommand + case infoCommand: + cmd = app.InfoCommand default: cmd = app.UnknownCommand } @@ -131,6 +147,16 @@ func (api *botAPI) hydrateImage(photos []tgbotapi.PhotoSize) *app.Image { } } +func (api *botAPI) hydrateVideo(video *tgbotapi.Video) *app.Video { + if video == nil { + return nil + } + + return &app.Video{ + FileID: video.FileID, + } +} + func (api *botAPI) preparePhotos(message app.Message) []interface{} { photo := tgbotapi.NewInputMediaPhoto(tgbotapi.FileID(message.Image.FileID)) photo.Caption = message.Text @@ -141,3 +167,10 @@ func (api *botAPI) preparePhotos(message app.Message) []interface{} { return photos } + +func (api *botAPI) prepareVideo(message app.Message) []interface{} { + video := tgbotapi.NewInputMediaVideo(tgbotapi.FileID(message.Video.FileID)) + video.Caption = message.Text + video.ParseMode = messageParseMode + return []interface{}{video} +}