]> git.giacomolli.uk - onechat/blobdiff - whatsapp_inbox.go
Receive voice notes from both chat services
[onechat] / whatsapp_inbox.go
index 9d2183e7d5e282a96442f35ba13d0a7cf58ec1a1..7d4e9e4c7b3920a303d35628b290a60676df9e7a 100644 (file)
@@ -2,9 +2,9 @@ package main
 
 import (
        "context"
-       "crypto/sha256"
        "fmt"
        "log/slog"
+       "path/filepath"
        "slices"
        "strings"
        "sync"
@@ -41,6 +41,8 @@ func newWhatsAppInbox(state *WhatsAppState, clock func() time.Time, groups *What
                clock:    clock,
                groups:   groups,
                images:   images,
+               voices:   &VoiceStore{dir: filepath.Join(filepath.Dir(filepath.Dir(state.path)), voiceDirName)},
+               voice:    &VoiceTranscriber{},
                logger:   logger,
                wake:     make(chan struct{}, 1),
                failures: make(chan error, 1),
@@ -74,6 +76,8 @@ type WhatsAppInbox struct {
        clock    func() time.Time
        groups   *WhatsAppGroups
        images   *ImageStore
+       voices   *VoiceStore
+       voice    *VoiceTranscriber
        logger   *slog.Logger
        wake     chan struct{}
        failures chan error
@@ -244,6 +248,8 @@ func (i *WhatsAppInbox) receive(ctx context.Context, link WhatsAppLink, event *e
                defer cancel()
 
                message.Text = i.imageText(ctx, link, event, message.Text)
+       } else if event.Message.GetAudioMessage().GetPTT() {
+               message.Text = whatsappRedact(link.own, whatsappVoice(ctx, link.client, i.voices, i.voice, event.Message.GetAudioMessage()))
        }
 
        _, err := i.state.Add(chat, message, time.Now())
@@ -271,9 +277,8 @@ func (i *WhatsAppInbox) imageText(ctx context.Context, link WhatsAppLink, event
        name, err := whatsappImage(ctx, link.client, i.images, event.Message.GetImageMessage())
        if err != nil {
                chat := event.Info.Chat.ToNonAD()
-               digest := sha256.Sum256([]byte(chat.String()))
                redact := append(slices.Clone(link.own), chat.User)
-               i.logger.Error("image receive failed", "chat", fmt.Sprintf("%x", digest[:6]), "err", whatsappRedact(redact, err.Error()))
+               i.logger.Error("image receive failed", "chat", whatsappChatHash(chat), "err", whatsappRedact(redact, err.Error()))
 
                text := "(image not downloaded)"
                if caption != "" {
@@ -358,7 +363,7 @@ func whatsappMessage(ctx context.Context, device *store.Device, own []string, ev
        chatJID := info.Chat.ToNonAD()
        switch {
        case device == nil:
-       case event.IsEdit, info.Edit != "", event.IsEphemeral, event.IsViewOnce, event.Message.GetImageMessage().GetViewOnce(), event.Message.GetVideoMessage().GetViewOnce(), event.SourceWebMsg != nil:
+       case event.IsEdit, info.Edit != "", event.IsEphemeral, event.IsViewOnce, event.Message.GetImageMessage().GetViewOnce(), event.Message.GetVideoMessage().GetViewOnce(), event.Message.GetAudioMessage().GetViewOnce(), event.SourceWebMsg != nil:
        case chatJID.Server != types.DefaultUserServer && chatJID.Server != types.HiddenUserServer && chatJID.Server != types.GroupServer:
        case slices.Contains(own, chatJID.User):
        default:
@@ -380,6 +385,9 @@ func whatsappText(ctx context.Context, device *store.Device, own []string, event
 
        contextInfo := extended.GetContextInfo()
        switch {
+       case event.Message.GetAudioMessage().GetPTT():
+               text = "(voice not downloaded)"
+               contextInfo = event.Message.GetAudioMessage().GetContextInfo()
        case event.Message.GetImageMessage() != nil:
                image := event.Message.GetImageMessage()
                text = image.GetCaption()