Skip to content

Commit 42cdf67

Browse files
Merge pull request #2 from gabolaev/master
GetFileInfo, send uploaded file, chat ej, tests fix, tiny fixes
2 parents 85dc8eb + 87391e1 commit 42cdf67

File tree

11 files changed

+380
-178
lines changed

11 files changed

+380
-178
lines changed

bot.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import (
1414
)
1515

1616
const (
17-
apiURL = "https://api.icq.net/bot/v1"
17+
defaultAPIURL = "https://api.icq.net/bot/v1"
18+
defaultDebug = false
1819
)
1920

2021
// Bot is the main structure for interaction with ICQ API.
@@ -29,10 +30,22 @@ type Bot struct {
2930

3031
// GetInfo returns information about bot:
3132
// id, name, about, avatar
32-
func (b *Bot) GetChatInfo(chatID string) (*BotInfo, error) {
33+
func (b *Bot) GetInfo() (*BotInfo, error) {
3334
return b.client.GetInfo()
3435
}
3536

37+
// GetChatInfo returns information about chat:
38+
// id, type, title, public, group, inviteLink, admins
39+
func (b *Bot) GetChatInfo(chatID string) (*Chat, error) {
40+
return b.client.GetChatInfo(chatID)
41+
}
42+
43+
// GetFileInfo returns information about file:
44+
// id, type, size, filename, url
45+
func (b *Bot) GetFileInfo(fileID string) (*File, error) {
46+
return b.client.GetFileInfo(fileID)
47+
}
48+
3649
// NewMessage returns new message
3750
func (b *Bot) NewMessage(chatID string) *Message {
3851
return &Message{
@@ -50,6 +63,15 @@ func (b *Bot) NewFileMessage(chatID string, file *os.File) *Message {
5063
}
5164
}
5265

66+
// NewFileMessageByFileID returns new message with previously uploaded file id
67+
func (b *Bot) NewFileMessageByFileID(chatID, fileID string) *Message {
68+
return &Message{
69+
client: b.client,
70+
Chat: Chat{ID: chatID},
71+
FileID: fileID,
72+
}
73+
}
74+
5375
// NewMessageFromPart returns new message based on part message
5476
func (b *Bot) NewMessageFromPart(message PartMessage) *Message {
5577
return &Message{
@@ -62,7 +84,7 @@ func (b *Bot) NewMessageFromPart(message PartMessage) *Message {
6284
}
6385

6486
// NewTextMessage returns new text message
65-
func (b *Bot) NewTextMessage(chatID string, text string) *Message {
87+
func (b *Bot) NewTextMessage(chatID, text string) *Message {
6688
return &Message{
6789
client: b.client,
6890
Chat: Chat{ID: chatID},
@@ -103,14 +125,13 @@ func (b *Bot) GetUpdatesChannel(ctx context.Context) <-chan Event {
103125
// All communications with ICQ bot API must go through Bot struct.
104126
// In general you don't need to configure this bot, therefore all options are optional arguments.
105127
func NewBot(token string, opts ...BotOption) (*Bot, error) {
106-
debug := false
107-
apiURL := "https://api.icq.net/bot/v1"
108128
logger := logrus.New()
109129
logger.SetFormatter(&logrus.TextFormatter{
110130
FullTimestamp: true,
111131
TimestampFormat: "2006-01-02 15:04:05",
112132
})
113133

134+
apiURL, debug := defaultAPIURL, defaultDebug
114135
for _, option := range opts {
115136
switch option.Type() {
116137
case "api_url":

chat.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package goicqbot
22

3+
//go:generate easyjson -all chat.go
34

45
type Chat struct {
56
client *Client
@@ -22,4 +23,3 @@ type Chat struct {
2223

2324
Admins []Contact `json:"admins"`
2425
}
25-

chat_easyjson.go

Lines changed: 159 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ func (c *Client) GetInfo() (*BotInfo, error) {
118118
return info, nil
119119
}
120120

121-
func (c *Client) GetChatInfo(chatId string) (*Chat, error) {
121+
func (c *Client) GetChatInfo(chatID string) (*Chat, error) {
122122
params := url.Values{
123-
"chatId": {chatId},
123+
"chatId": {chatID},
124124
}
125125
response, err := c.Do("/chats/getInfo", params, nil)
126126
if err != nil {
@@ -148,6 +148,23 @@ func (c *Client) GetChatInfo(chatId string) (*Chat, error) {
148148
return chat, nil
149149
}
150150

151+
func (c *Client) GetFileInfo(fileID string) (*File, error) {
152+
params := url.Values{
153+
"fileId": {fileID},
154+
}
155+
response, err := c.Do("/files/getInfo", params, nil)
156+
if err != nil {
157+
return nil, fmt.Errorf("error while receiving information: %s", err)
158+
}
159+
160+
file := &File{}
161+
if err := json.Unmarshal(response, file); err != nil {
162+
return nil, fmt.Errorf("error while unmarshalling information: %s", err)
163+
}
164+
165+
return file, nil
166+
}
167+
151168
func (c *Client) SendMessage(message *Message) error {
152169
params := url.Values{
153170
"chatId": []string{message.Chat.ID},

client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func TestClient_GetEvents_OK(t *testing.T) {
104104
{
105105
Type: FORWARD,
106106
Payload: PartPayload{
107-
Message: PartMessage{
107+
PartMessage: PartMessage{
108108
MsgID: "12354",
109109
Text: "test1",
110110
},
@@ -113,7 +113,7 @@ func TestClient_GetEvents_OK(t *testing.T) {
113113
{
114114
Type: REPLY,
115115
Payload: PartPayload{
116-
Message: PartMessage{
116+
PartMessage: PartMessage{
117117
MsgID: "12354",
118118
Text: "test",
119119
},

example/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ package main
33
import (
44
"context"
55
"fmt"
6-
"github.com/DmitryDorofeev/goicqbot"
76
"log"
87
"os"
98
"time"
9+
10+
"github.com/DmitryDorofeev/goicqbot"
1011
)
1112

1213
func main() {
@@ -35,7 +36,7 @@ func main() {
3536
fileMessage.Delete()
3637

3738
// Simple 30-seconds echo bot
38-
ctx, _ := context.WithTimeout(context.Background(), 30 * time.Second)
39+
ctx, _ := context.WithTimeout(context.Background(), 30*time.Second)
3940
updates := bot.GetUpdatesChannel(ctx)
4041
for update := range updates {
4142
fmt.Println(update.Type, update.Payload)

file.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package goicqbot
2+
3+
//go:generate easyjson -all file.go
4+
5+
type File struct {
6+
// Id of the file
7+
ID string `json:"fileId"`
8+
9+
// Type of the file
10+
Type string `json:"type"`
11+
12+
// Size in bytes
13+
Size uint64 `json:"size"`
14+
15+
// Name of file
16+
Name bool `json:"filename"`
17+
18+
// URL to the file
19+
URL string `json:"url"`
20+
}

0 commit comments

Comments
 (0)