Skip to content

Commit 7b1432c

Browse files
Merge pull request #5 from gabolaev/master
Message content autodetection
2 parents db3d3da + 6c0fcba commit 7b1432c

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ bot := goicqbot.NewBot(BOT_TOKEN, goicqbot.BotDebug(true))
101101

102102
- [x] Delete message
103103

104-
- [ ] Chat info
104+
- [x] Chat info
105105

106-
- [ ] Send voice
106+
- [x] Send voice
107107

108-
- [ ] File info
108+
- [x] File info
109109

bot.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@ func (b *Bot) GetFileInfo(fileID string) (*File, error) {
4949
// NewMessage returns new message
5050
func (b *Bot) NewMessage(chatID string) *Message {
5151
return &Message{
52-
client: b.client,
53-
Chat: Chat{ID: chatID},
54-
ContentType: Text,
52+
client: b.client,
53+
Chat: Chat{ID: chatID},
5554
}
5655
}
5756

@@ -126,7 +125,8 @@ func (b *Bot) NewChat(id string) *Chat {
126125
// SendMessage sends a message, passed as an argument.
127126
// This method fills the argument with ID of sent message and returns an error if any.
128127
func (b *Bot) SendMessage(message *Message) error {
129-
return b.client.SendTextMessage(message)
128+
message.client = b.client
129+
return message.Send()
130130
}
131131

132132
// EditMessage edit a message passed as an argument.

message.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ package goicqbot
33
import (
44
"fmt"
55
"os"
6+
"path/filepath"
67
)
78

89
//go:generate easyjson -all message.go
910

1011
type MessageContentType uint8
1112

1213
const (
13-
Text MessageContentType = iota
14+
Unknown MessageContentType = iota
15+
Text
1416
OtherFile
1517
Voice
1618
)
@@ -101,6 +103,26 @@ func (m *Message) Send() error {
101103
}
102104
case Text:
103105
return m.client.SendTextMessage(m)
106+
case Unknown:
107+
// need to autodetect
108+
if m.FileID != "" {
109+
// voice message's fileID always starts with 'I'
110+
if m.FileID[0] == voiceMessageLeadingRune {
111+
return m.client.SendVoiceMessage(m)
112+
}
113+
return m.client.SendFileMessage(m)
114+
}
115+
116+
if m.File != nil {
117+
if voiceMessageSupportedExtensions[filepath.Ext(m.File.Name())] {
118+
return m.client.UploadVoice(m)
119+
}
120+
return m.client.UploadFile(m)
121+
}
122+
123+
if m.Text != "" {
124+
return m.client.SendTextMessage(m)
125+
}
104126
}
105127

106128
return fmt.Errorf("cannot send message or file without data")

voice.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package goicqbot
2+
3+
var (
4+
voiceMessageSupportedExtensions = map[string]bool{
5+
".aac": true,
6+
".ogg": true,
7+
".m4a": true,
8+
}
9+
)
10+
11+
const (
12+
voiceMessageLeadingRune = 'I'
13+
)

0 commit comments

Comments
 (0)