X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/b1be0ee8530df5db8973bfe355f2f6d3149383a0..4a42f7f208cbfead335eba9ebe615b35b1742130:/whatsapp_send.go?ds=inline diff --git a/whatsapp_send.go b/whatsapp_send.go index b95a82e..dbbf862 100644 --- a/whatsapp_send.go +++ b/whatsapp_send.go @@ -6,6 +6,7 @@ import ( "encoding/hex" "errors" "fmt" + "log/slog" "slices" "strings" "time" @@ -39,18 +40,18 @@ const ( // during the send gives an answer that the result is unknown, and not a second // send. A disconnected WhatsApp sends nothing and makes no record. link gives // the device, the client, and the own IDs, groups the names of the groups, and -// state the send records. The clock gives the times of the records and the time -// zone of the answer. Image names are checked before the record is written, and -// uploads start only after the record is durable. Text-only records keep their -// text digest. -func whatsappSend(ctx context.Context, link WhatsAppLink, groups *WhatsAppGroups, state *WhatsAppState, images *ImageStore, clock func() time.Time, id, chat, text, image string) (string, error) { +// state the send records, and logger gets one line for each send. The clock +// gives the times of the records and the time zone of the answer. Image names +// are checked before the record is written, and uploads start only after the +// record is durable. Text-only records keep their text digest. +func whatsappSend(ctx context.Context, link WhatsAppLink, groups *WhatsAppGroups, state *WhatsAppState, images *ImageStore, logger *slog.Logger, clock func() time.Time, id, chat, text, image string) (string, error) { if !link.linked { return whatsappNotLinked, nil } jid, err := whatsappSendCheck(id, chat, text, image, link.own) if err != nil { - return "", err + return "", fmt.Errorf("check WhatsApp send: %w", err) } digest := sha256.Sum256([]byte(text)) @@ -87,7 +88,7 @@ func whatsappSend(ctx context.Context, link WhatsAppLink, groups *WhatsAppGroups record.MessageID = link.client.GenerateMessageID() err = state.PutSend(record) if err != nil { - return "", err + return "", fmt.Errorf("save pending WhatsApp send: %w", err) } message := &waE2E.Message{Conversation: new(text)} @@ -113,7 +114,10 @@ func whatsappSend(ctx context.Context, link WhatsAppLink, groups *WhatsAppGroups if err != nil { record.State = whatsappFailed record.Answer = "The send failed: " + whatsappRedact(link.own, err.Error()) + "." + logger.Warn("message send failed", "chat", whatsappChatHash(jid), "image", image != "", "err", whatsappRedact(append(slices.Clone(link.own), jid.User), err.Error())) } else { + logger.Info("message sent", "chat", whatsappChatHash(jid), "image", image != "") + name := groups.name(ctx, link, jid.String()) if jid.Server != types.GroupServer { name = whatsappContactName(ctx, link.device, jid, "") @@ -124,7 +128,7 @@ func whatsappSend(ctx context.Context, link WhatsAppLink, groups *WhatsAppGroups err = state.PutSend(record) if err != nil { - return "", err + return "", fmt.Errorf("save completed WhatsApp send: %w", err) } return record.Answer, nil