Skip to content

Commit 947e14a

Browse files
author
George Gabolaev
committed
Voice messages support
1 parent 42cdf67 commit 947e14a

File tree

3 files changed

+123
-28
lines changed

3 files changed

+123
-28
lines changed

bot.go

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,59 @@ 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},
52+
client: b.client,
53+
Chat: Chat{ID: chatID},
54+
ContentType: Text,
55+
}
56+
}
57+
58+
// NewTextMessage returns new text message
59+
func (b *Bot) NewTextMessage(chatID, text string) *Message {
60+
return &Message{
61+
client: b.client,
62+
Chat: Chat{ID: chatID},
63+
Text: text,
64+
ContentType: Text,
5465
}
5566
}
5667

5768
// NewFileMessage returns new file message
5869
func (b *Bot) NewFileMessage(chatID string, file *os.File) *Message {
5970
return &Message{
60-
client: b.client,
61-
Chat: Chat{ID: chatID},
62-
File: file,
71+
client: b.client,
72+
Chat: Chat{ID: chatID},
73+
File: file,
74+
ContentType: OtherFile,
6375
}
6476
}
6577

6678
// NewFileMessageByFileID returns new message with previously uploaded file id
6779
func (b *Bot) NewFileMessageByFileID(chatID, fileID string) *Message {
6880
return &Message{
69-
client: b.client,
70-
Chat: Chat{ID: chatID},
71-
FileID: fileID,
81+
client: b.client,
82+
Chat: Chat{ID: chatID},
83+
FileID: fileID,
84+
ContentType: OtherFile,
85+
}
86+
}
87+
88+
// NewFileMessage returns new voice message
89+
func (b *Bot) NewVoiceMessage(chatID string, file *os.File) *Message {
90+
return &Message{
91+
client: b.client,
92+
Chat: Chat{ID: chatID},
93+
File: file,
94+
ContentType: Voice,
95+
}
96+
}
97+
98+
// NewVoiceMessageByFileID returns new message with previously uploaded voice file id
99+
func (b *Bot) NewVoiceMessageByFileID(chatID, fileID string) *Message {
100+
return &Message{
101+
client: b.client,
102+
Chat: Chat{ID: chatID},
103+
FileID: fileID,
104+
ContentType: Voice,
72105
}
73106
}
74107

@@ -83,15 +116,6 @@ func (b *Bot) NewMessageFromPart(message PartMessage) *Message {
83116
}
84117
}
85118

86-
// NewTextMessage returns new text message
87-
func (b *Bot) NewTextMessage(chatID, text string) *Message {
88-
return &Message{
89-
client: b.client,
90-
Chat: Chat{ID: chatID},
91-
Text: text,
92-
}
93-
}
94-
95119
func (b *Bot) NewChat(id string) *Chat {
96120
return &Chat{
97121
client: b.client,

client.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,11 @@ func (c *Client) GetFileInfo(fileID string) (*File, error) {
165165
return file, nil
166166
}
167167

168-
func (c *Client) SendMessage(message *Message) error {
168+
func (c *Client) GetVoiceInfo(fileID string) (*File, error) {
169+
return c.GetFileInfo(fileID)
170+
}
171+
172+
func (c *Client) SendTextMessage(message *Message) error {
169173
params := url.Values{
170174
"chatId": []string{message.Chat.ID},
171175
"text": []string{message.Text},
@@ -242,6 +246,25 @@ func (c *Client) SendFile(message *Message) error {
242246
return nil
243247
}
244248

249+
func (c *Client) SendVoice(message *Message) error {
250+
params := url.Values{
251+
"chatId": {message.Chat.ID},
252+
"caption": {message.Text},
253+
"fileId": {message.FileID},
254+
}
255+
256+
response, err := c.Do("/messages/sendVoice", params, nil)
257+
if err != nil {
258+
return fmt.Errorf("error while making request: %s", err)
259+
}
260+
261+
if err := json.Unmarshal(response, message); err != nil {
262+
return fmt.Errorf("cannot unmarshal response: %s", err)
263+
}
264+
265+
return nil
266+
}
267+
245268
func (c *Client) UploadFile(message *Message) error {
246269
params := url.Values{
247270
"chatId": {message.Chat.ID},
@@ -260,6 +283,24 @@ func (c *Client) UploadFile(message *Message) error {
260283
return nil
261284
}
262285

286+
func (c *Client) UploadVoice(message *Message) error {
287+
params := url.Values{
288+
"chatId": {message.Chat.ID},
289+
"caption": {message.Text},
290+
}
291+
292+
response, err := c.Do("/messages/sendVoice", params, message.File)
293+
if err != nil {
294+
return fmt.Errorf("error while making request: %s", err)
295+
}
296+
297+
if err := json.Unmarshal(response, message); err != nil {
298+
return fmt.Errorf("cannot unmarshal response: %s", err)
299+
}
300+
301+
return nil
302+
}
303+
263304
func (c *Client) GetEvents(lastEventID int, pollTime int) ([]*Event, error) {
264305
params := url.Values{
265306
"lastEventId": {strconv.Itoa(lastEventID)},

message.go

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,18 @@ import (
77

88
//go:generate easyjson -all message.go
99

10+
type MessageContentType uint8
11+
12+
const (
13+
Text MessageContentType = iota
14+
OtherFile
15+
Voice
16+
)
17+
1018
// Message represents a text message in ICQ
1119
type Message struct {
12-
client *Client
20+
client *Client
21+
ContentType MessageContentType
1322

1423
// Id of the message (for editing)
1524
ID string `json:"msgId"`
@@ -44,10 +53,22 @@ type Message struct {
4453

4554
func (m *Message) AttachNewFile(file *os.File) {
4655
m.File = file
56+
m.ContentType = OtherFile
4757
}
4858

4959
func (m *Message) AttachExistingFile(fileID string) {
5060
m.FileID = fileID
61+
m.ContentType = OtherFile
62+
}
63+
64+
func (m *Message) AttachNewVoice(file *os.File) {
65+
m.File = file
66+
m.ContentType = Voice
67+
}
68+
69+
func (m *Message) AttachExistingVoice(fileID string) {
70+
m.FileID = fileID
71+
m.ContentType = Voice
5172
}
5273

5374
// Send method sends your message.
@@ -61,15 +82,24 @@ func (m *Message) Send() error {
6182
return fmt.Errorf("message should have chat id")
6283
}
6384

64-
if m.FileID != "" {
65-
return m.client.SendFile(m)
66-
}
67-
68-
if m.File != nil {
69-
return m.client.UploadFile(m)
70-
}
71-
72-
if m.Text != "" {
85+
switch m.ContentType {
86+
case Voice:
87+
if m.FileID != "" {
88+
return m.client.SendVoice(m)
89+
}
90+
91+
if m.File != nil {
92+
return m.client.UploadVoice(m)
93+
}
94+
case OtherFile:
95+
if m.FileID != "" {
96+
return m.client.SendFile(m)
97+
}
98+
99+
if m.File != nil {
100+
return m.client.UploadFile(m)
101+
}
102+
case Text:
73103
return m.client.SendMessage(m)
74104
}
75105

0 commit comments

Comments
 (0)