Skip to content

Commit

Permalink
feat: handle more message types (#13)
Browse files Browse the repository at this point in the history
* feat: handle more message types

* feat: rename processMessage

Co-authored-by: Michael <[email protected]>

* feat: rename processMessage

---------

Co-authored-by: Michael <[email protected]>
  • Loading branch information
lectral and Michael authored May 29, 2024
1 parent 7a63219 commit bc386b6
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions memogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (s *Service) handler(ctx context.Context, b *bot.Bot, m *models.Update) {
if message.Caption != "" {
content = message.Caption
}
hasResource := message.Document != nil || len(message.Photo) > 0
hasResource := message.Document != nil || len(message.Photo) > 0 || message.Voice != nil || message.Video != nil
if content == "" && !hasResource {
b.SendMessage(ctx, &bot.SendMessageParams{
ChatID: m.Message.Chat.ID,
Expand All @@ -119,31 +119,20 @@ func (s *Service) handler(ctx context.Context, b *bot.Bot, m *models.Update) {
}

if message.Document != nil {
file, err := b.GetFile(ctx, &bot.GetFileParams{FileID: message.Document.FileID})
if err != nil {
s.sendError(b, m.Message.Chat.ID, errors.Wrap(err, "failed to get file"))
return
}

_, err = s.saveResourceFromFile(ctx, file, memo)
if err != nil {
s.sendError(b, m.Message.Chat.ID, errors.Wrap(err, "failed to save resource"))
return
}
s.processFileMessage(ctx, b, m, message.Document.FileID, memo)
}

if message.Voice != nil {
s.processFileMessage(ctx, b, m, message.Voice.FileID, memo)
}

if message.Video != nil {
s.processFileMessage(ctx, b, m, message.Video.FileID, memo)
}

if len(message.Photo) > 0 {
photo := message.Photo[len(message.Photo)-1]
file, err := b.GetFile(ctx, &bot.GetFileParams{FileID: photo.FileID})
if err != nil {
s.sendError(b, m.Message.Chat.ID, errors.Wrap(err, "failed to get file"))
return
}

_, err = s.saveResourceFromFile(ctx, file, memo)
if err != nil {
s.sendError(b, m.Message.Chat.ID, errors.Wrap(err, "failed to save resource"))
return
}
s.processFileMessage(ctx, b, m, photo.FileID, memo)
}

b.SendMessage(ctx, &bot.SendMessageParams{
Expand Down Expand Up @@ -211,6 +200,21 @@ func (s *Service) saveResourceFromFile(ctx context.Context, file *models.File, m
return resource, nil
}

func (s *Service) processFileMessage(ctx context.Context, b *bot.Bot, m *models.Update, fileID string, memo *v1pb.Memo) {
file, err := b.GetFile(ctx, &bot.GetFileParams{FileID: fileID})
if err != nil {
s.sendError(b, m.Message.Chat.ID, errors.Wrap(err, "failed to get file"))
return
}

_, err = s.saveResourceFromFile(ctx, file, memo)
if err != nil {
s.sendError(b, m.Message.Chat.ID, errors.Wrap(err, "failed to save resource"))
return
}
}


func (s *Service) sendError(b *bot.Bot, chatID int64, err error) {
slog.Error("error", slog.Any("err", err))
b.SendMessage(context.Background(), &bot.SendMessageParams{
Expand Down

0 comments on commit bc386b6

Please sign in to comment.