fix: preserve QQ attachment filenames for local uploads
This commit is contained in:
parent
a752b00247
commit
7fa37f23df
3 changed files with 15 additions and 1 deletions
|
|
@ -354,7 +354,7 @@ func (c *QQChannel) SendMedia(ctx context.Context, msg bus.OutboundMediaMessage)
|
|||
return fmt.Errorf("qq send media: resolve local media ref %q: %w", part.Ref, err)
|
||||
}
|
||||
|
||||
fileInfo, err := c.uploadLocalMedia(ctx, chatKind, msg.ChatID, part.Type, resolved)
|
||||
fileInfo, err := c.uploadLocalMedia(ctx, chatKind, msg.ChatID, part.Type, part.Filename, resolved)
|
||||
if err != nil {
|
||||
logger.ErrorCF("qq", "Failed to upload local media", map[string]any{
|
||||
"type": part.Type,
|
||||
|
|
|
|||
|
|
@ -166,6 +166,9 @@ func TestSendMedia_LocalFileUploadsThenSendsRichMediaMessage(t *testing.T) {
|
|||
if fileData == "" {
|
||||
t.Fatal("file_data is empty, want base64-encoded local file contents")
|
||||
}
|
||||
if payload["file_name"] != "report.pdf" {
|
||||
t.Fatalf("file_name = %v, want %q", payload["file_name"], "report.pdf")
|
||||
}
|
||||
|
||||
if len(api.groupMessages) != 1 {
|
||||
t.Fatalf("group message count = %d, want 1", len(api.groupMessages))
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@ import (
|
|||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/tencent-connect/botgo/constant"
|
||||
"github.com/tencent-connect/botgo/dto"
|
||||
|
|
@ -33,6 +35,7 @@ func (c *QQChannel) uploadLocalMedia(
|
|||
chatKind string,
|
||||
chatID string,
|
||||
partType string,
|
||||
filename string,
|
||||
localPath string,
|
||||
) ([]byte, error) {
|
||||
content, err := os.ReadFile(localPath)
|
||||
|
|
@ -40,8 +43,16 @@ func (c *QQChannel) uploadLocalMedia(
|
|||
return nil, fmt.Errorf("read local media: %w", err)
|
||||
}
|
||||
|
||||
filename = strings.TrimSpace(filename)
|
||||
if filename == "" {
|
||||
filename = filepath.Base(localPath)
|
||||
} else {
|
||||
filename = filepath.Base(filename)
|
||||
}
|
||||
|
||||
payload := map[string]any{
|
||||
"file_type": qqFileType(partType),
|
||||
"file_name": filename,
|
||||
"srv_send_msg": false,
|
||||
"file_data": base64.StdEncoding.EncodeToString(content),
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue