Skip to content

Commit cce7d5a

Browse files
authored
feat(filebox): support send voice message (#143)
1 parent d6c4a33 commit cce7d5a

File tree

3 files changed

+59
-18
lines changed

3 files changed

+59
-18
lines changed

wechaty-puppet-service/puppet_service.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ func (p *PuppetService) messageSendFileNonStream(conversationID string, fileBox
736736
if err != nil {
737737
return "", err
738738
}
739-
jsonText, err = filebox.FromBase64(base64, filebox.WithName(fileBox.Name)).ToJSON()
739+
jsonText, err = filebox.FromBase64(base64, filebox.WithName(fileBox.Name), filebox.WithMetadata(fileBox.MetaData())).ToJSON()
740740
if err != nil {
741741
return "", err
742742
}

wechaty-puppet/filebox/file_box.go

+57-16
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"github.com/tuotoo/qrcode"
1010
"github.com/wechaty/go-wechaty/wechaty-puppet/helper"
11+
logger "github.com/wechaty/go-wechaty/wechaty-puppet/log"
1112
"io"
1213
"io/ioutil"
1314
"mime"
@@ -33,33 +34,66 @@ var (
3334
ErrNoUuid = errors.New("no uuid")
3435
)
3536

37+
var log = logger.L.WithField("module", "filebox")
38+
3639
type fileImplInterface interface {
3740
toJSONMap() (map[string]interface{}, error)
3841
toReader() (io.Reader, error)
3942
}
4043

4144
// FileBox struct
4245
type FileBox struct {
43-
fileImpl fileImplInterface
44-
Name string
45-
metadata map[string]interface{}
46-
boxType Type
47-
mimeType string
48-
size int64
49-
md5 string
46+
fileImpl fileImplInterface
47+
Name string
48+
metadata map[string]interface{}
49+
boxType Type
50+
mediaType string
51+
size int64
52+
md5 string
5053

5154
err error
5255
}
5356

5457
func newFileBox(boxType Type, fileImpl fileImplInterface, options Options) *FileBox {
55-
return &FileBox{
58+
fb := &FileBox{
5659
fileImpl: fileImpl,
5760
Name: options.Name,
5861
metadata: options.Metadata,
5962
boxType: boxType,
6063
size: options.Size,
6164
md5: options.Md5,
62-
mimeType: mime.TypeByExtension(filepath.Ext(options.Name)),
65+
}
66+
if fb.metadata == nil {
67+
fb.metadata = make(map[string]interface{})
68+
}
69+
fb.correctName()
70+
fb.guessMediaType()
71+
return fb
72+
}
73+
74+
func (fb *FileBox) correctName() {
75+
if strings.HasSuffix(fb.Name, ".silk") || strings.HasSuffix(fb.Name, ".slk") {
76+
log.Warn("detect that you want to send voice file which should be <name>.sil pattern. So we help you rename it.")
77+
if strings.HasSuffix(fb.Name, ".silk") {
78+
fb.Name = strings.ReplaceAll(fb.Name, ".silk", ".sil")
79+
}
80+
if strings.HasSuffix(fb.Name, ".slk") {
81+
fb.Name = strings.ReplaceAll(fb.Name, ".slk", ".sil")
82+
}
83+
}
84+
}
85+
86+
func (fb *FileBox) guessMediaType() {
87+
if strings.HasSuffix(fb.Name, ".sil") {
88+
fb.mediaType = "audio/silk"
89+
if _, ok := fb.metadata["voiceLength"]; !ok {
90+
log.Warn("detect that you want to send voice file, but no voiceLength setting, " +
91+
`so use the default setting: 1000,` +
92+
`you should set it manually: filebox.WithMetadata(map[string]interface{}{"voiceLength": 2000})`)
93+
fb.metadata["voiceLength"] = 1000
94+
}
95+
} else {
96+
fb.mediaType = mime.TypeByExtension(filepath.Ext(fb.Name))
6397
}
6498
}
6599

@@ -192,12 +226,13 @@ func (fb *FileBox) ToJSON() (string, error) {
192226
}
193227

194228
jsonMap := map[string]interface{}{
195-
"name": fb.Name,
196-
"metadata": fb.metadata,
197-
"type": fb.boxType,
198-
"boxType": fb.boxType, //Deprecated
199-
"size": fb.size,
200-
"md5": fb.md5,
229+
"name": fb.Name,
230+
"metadata": fb.metadata,
231+
"type": fb.boxType,
232+
"boxType": fb.boxType, //Deprecated
233+
"size": fb.size,
234+
"md5": fb.md5,
235+
"mediaType": fb.mediaType,
201236
}
202237

203238
switch fb.boxType {
@@ -291,7 +326,7 @@ func (fb *FileBox) ToDataURL() (string, error) {
291326
if err != nil {
292327
return "", nil
293328
}
294-
return fmt.Sprintf("data:%s;base64,%s", fb.mimeType, toBase64), nil
329+
return fmt.Sprintf("data:%s;base64,%s", fb.mediaType, toBase64), nil
295330
}
296331

297332
// ToQRCode to QRCode
@@ -352,6 +387,12 @@ func (fb *FileBox) Type() Type {
352387
return fb.boxType
353388
}
354389

390+
// MetaData get metadata
391+
func (fb *FileBox) MetaData() map[string]interface{} {
392+
// TODO deep copy?
393+
return fb.metadata
394+
}
395+
355396
// Error ret err
356397
func (fb *FileBox) Error() error {
357398
return fb.err

wechaty-puppet/filebox/file_box_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func TestFileBox_ToJSON(t *testing.T) {
108108
t.Run("ToJSON success", func(t *testing.T) {
109109
const base64Encode = "RmlsZUJveEJhc2U2NAo="
110110
const base64Filename = "test.txt"
111-
const want = `{"base64":"RmlsZUJveEJhc2U2NAo=","boxType":1,"md5":"","metadata":null,"name":"test.txt","size":14,"type":1}`
111+
const want = `{"base64":"RmlsZUJveEJhc2U2NAo=","boxType":1,"md5":"","mediaType":"text/plain; charset=utf-8","metadata":{},"name":"test.txt","size":14,"type":1}`
112112
jsonString, err := FromBase64(base64Encode, WithName(base64Filename)).ToJSON()
113113
if err != nil {
114114
t.Error(err)

0 commit comments

Comments
 (0)