diff --git a/README.md b/README.md index 9353e57..8a46931 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/bot.go b/bot.go index e3d5dee..3db8688 100644 --- a/bot.go +++ b/bot.go @@ -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}, } } @@ -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. diff --git a/message.go b/message.go index 447ff83..b22559e 100644 --- a/message.go +++ b/message.go @@ -3,6 +3,7 @@ package goicqbot import ( "fmt" "os" + "path/filepath" ) //go:generate easyjson -all message.go @@ -10,7 +11,8 @@ import ( type MessageContentType uint8 const ( - Text MessageContentType = iota + Unknown MessageContentType = iota + Text OtherFile Voice ) @@ -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") diff --git a/voice.go b/voice.go new file mode 100644 index 0000000..949295f --- /dev/null +++ b/voice.go @@ -0,0 +1,13 @@ +package goicqbot + +var ( + voiceMessageSupportedExtensions = map[string]bool{ + ".aac": true, + ".ogg": true, + ".m4a": true, + } +) + +const ( + voiceMessageLeadingRune = 'I' +)