Add support for audio attachments and voice messages

This commit is contained in:
2024-08-07 14:47:39 +03:00
parent aabc012c6b
commit 0a71122ba1
3 changed files with 73 additions and 0 deletions
+2
View File
@@ -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
+10
View File
@@ -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 (
+61
View File
@@ -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