Skip to content

Commit

Permalink
Merge pull request #5 from gabolaev/master
Browse files Browse the repository at this point in the history
Message content autodetection
  • Loading branch information
DmitryDorofeev authored Aug 26, 2019
2 parents db3d3da + 6c0fcba commit 7b1432c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ bot := goicqbot.NewBot(BOT_TOKEN, goicqbot.BotDebug(true))

- [x] Delete message

- [ ] Chat info
- [x] Chat info

- [ ] Send voice
- [x] Send voice

- [ ] File info
- [x] File info

8 changes: 4 additions & 4 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ func (b *Bot) GetFileInfo(fileID string) (*File, error) {
// NewMessage returns new message
func (b *Bot) NewMessage(chatID string) *Message {
return &Message{
client: b.client,
Chat: Chat{ID: chatID},
ContentType: Text,
client: b.client,
Chat: Chat{ID: chatID},
}
}

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

// EditMessage edit a message passed as an argument.
Expand Down
24 changes: 23 additions & 1 deletion message.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ package goicqbot
import (
"fmt"
"os"
"path/filepath"
)

//go:generate easyjson -all message.go

type MessageContentType uint8

const (
Text MessageContentType = iota
Unknown MessageContentType = iota
Text
OtherFile
Voice
)
Expand Down Expand Up @@ -101,6 +103,26 @@ func (m *Message) Send() error {
}
case Text:
return m.client.SendTextMessage(m)
case Unknown:
// need to autodetect
if m.FileID != "" {
// voice message's fileID always starts with 'I'
if m.FileID[0] == voiceMessageLeadingRune {
return m.client.SendVoiceMessage(m)
}
return m.client.SendFileMessage(m)
}

if m.File != nil {
if voiceMessageSupportedExtensions[filepath.Ext(m.File.Name())] {
return m.client.UploadVoice(m)
}
return m.client.UploadFile(m)
}

if m.Text != "" {
return m.client.SendTextMessage(m)
}
}

return fmt.Errorf("cannot send message or file without data")
Expand Down
13 changes: 13 additions & 0 deletions voice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package goicqbot

var (
voiceMessageSupportedExtensions = map[string]bool{
".aac": true,
".ogg": true,
".m4a": true,
}
)

const (
voiceMessageLeadingRune = 'I'
)

0 comments on commit 7b1432c

Please sign in to comment.