From 0a71122ba18b83b5f74cf949cec549980f007697 Mon Sep 17 00:00:00 2001 From: nightnoryu Date: Wed, 7 Aug 2024 14:47:39 +0300 Subject: [PATCH] Add support for audio attachments and voice messages --- pkg/app/anonymousmessagesservice.go | 2 + pkg/app/botapi.go | 10 +++++ pkg/infrastructure/botapi.go | 61 +++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+) diff --git a/pkg/app/anonymousmessagesservice.go b/pkg/app/anonymousmessagesservice.go index 885d69a..f841c2f 100644 --- a/pkg/app/anonymousmessagesservice.go +++ b/pkg/app/anonymousmessagesservice.go @@ -54,7 +54,9 @@ func (s *anonymousMessagesService) ServeMessages() error { err := s.api.SendMessageToOwner(Message{ Text: notificationText, Image: update.Message.Image, + Audio: update.Message.Audio, Video: update.Message.Video, + Voice: update.Message.Voice, }) if err != nil { s.errorsChan <- err diff --git a/pkg/app/botapi.go b/pkg/app/botapi.go index 7089ac7..87474f5 100644 --- a/pkg/app/botapi.go +++ b/pkg/app/botapi.go @@ -19,14 +19,20 @@ type Message struct { Text string UseMarkdown bool Image *Image + Audio *Audio Video *Video Sticker *Sticker + Voice *Voice } type Image struct { FileID string } +type Audio struct { + FileID string +} + type Video struct { FileID string } @@ -36,6 +42,10 @@ type Sticker struct { Emoji string } +type Voice struct { + FileID string +} + type Command int const ( diff --git a/pkg/infrastructure/botapi.go b/pkg/infrastructure/botapi.go index 7c0307c..a44ca21 100644 --- a/pkg/infrastructure/botapi.go +++ b/pkg/infrastructure/botapi.go @@ -58,10 +58,18 @@ func (api *botAPI) SendMessage(chatID int64, message app.Message) error { return api.sendPhotoMessage(chatID, message) } + if message.Audio != nil { + return api.sendAudioMessage(chatID, message) + } + if message.Video != nil { return api.sendVideoMessage(chatID, message) } + if message.Voice != nil { + return api.sendVoiceMessage(chatID, message) + } + return api.sendTextMessage(chatID, message) } @@ -78,8 +86,10 @@ func (api *botAPI) hydrateMessage(msg *tgbotapi.Message) app.Message { return app.Message{ Text: text, Image: api.hydrateImage(msg.Photo), + Audio: api.hydrateAudio(msg.Audio), Video: api.hydrateVideo(msg.Video), Sticker: api.hydrateSticker(msg.Sticker), + Voice: api.hydrateVoice(msg.Voice), } } @@ -105,6 +115,14 @@ func (api *botAPI) sendPhotoMessage(chatID int64, message app.Message) error { return errors.WithStack(err) } +func (api *botAPI) sendAudioMessage(chatID int64, message app.Message) error { + audios := api.prepareAudio(message) + mediaMsg := tgbotapi.NewMediaGroup(chatID, audios) + + _, err := api.bot.Send(mediaMsg) + return errors.WithStack(err) +} + func (api *botAPI) sendVideoMessage(chatID int64, message app.Message) error { video := api.prepareVideo(message) mediaMsg := tgbotapi.NewMediaGroup(chatID, video) @@ -113,6 +131,18 @@ func (api *botAPI) sendVideoMessage(chatID int64, message app.Message) error { return errors.WithStack(err) } +func (api *botAPI) sendVoiceMessage(chatID int64, message app.Message) error { + voiceMsg := tgbotapi.NewVoice(chatID, tgbotapi.FileID(message.Voice.FileID)) + + voiceMsg.Caption = message.Text + if message.UseMarkdown { + voiceMsg.ParseMode = tgbotapi.ModeMarkdown + } + + _, err := api.bot.Send(voiceMsg) + return errors.WithStack(err) +} + func (api *botAPI) hydrateCommand(msg *tgbotapi.Message) *app.Command { if !msg.IsCommand() { return nil @@ -150,6 +180,16 @@ func (api *botAPI) hydrateImage(photos []tgbotapi.PhotoSize) *app.Image { } } +func (api *botAPI) hydrateAudio(audio *tgbotapi.Audio) *app.Audio { + if audio == nil { + return nil + } + + return &app.Audio{ + FileID: audio.FileID, + } +} + func (api *botAPI) hydrateVideo(video *tgbotapi.Video) *app.Video { if video == nil { return nil @@ -171,6 +211,16 @@ func (api *botAPI) hydrateSticker(sticker *tgbotapi.Sticker) *app.Sticker { } } +func (api *botAPI) hydrateVoice(voice *tgbotapi.Voice) *app.Voice { + if voice == nil { + return nil + } + + return &app.Voice{ + FileID: voice.FileID, + } +} + func (api *botAPI) preparePhotos(message app.Message) []interface{} { photo := tgbotapi.NewInputMediaPhoto(tgbotapi.FileID(message.Image.FileID)) photo.Caption = message.Text @@ -185,6 +235,17 @@ func (api *botAPI) preparePhotos(message app.Message) []interface{} { return photos } +func (api *botAPI) prepareAudio(message app.Message) []interface{} { + audio := tgbotapi.NewInputMediaAudio(tgbotapi.FileID(message.Audio.FileID)) + audio.Caption = message.Text + + if message.UseMarkdown { + audio.ParseMode = tgbotapi.ModeMarkdown + } + + return []interface{}{audio} +} + func (api *botAPI) prepareVideo(message app.Message) []interface{} { video := tgbotapi.NewInputMediaVideo(tgbotapi.FileID(message.Video.FileID)) video.Caption = message.Text