Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import (
)

const (
apiURL = "https://api.icq.net/bot/v1"
defaultAPIURL = "https://api.icq.net/bot/v1"
defaultDebug = false
)

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

// GetInfo returns information about bot:
// id, name, about, avatar
func (b *Bot) GetChatInfo(chatID string) (*BotInfo, error) {
func (b *Bot) GetInfo() (*BotInfo, error) {
return b.client.GetInfo()
}

// GetChatInfo returns information about chat:
// id, type, title, public, group, inviteLink, admins
func (b *Bot) GetChatInfo(chatID string) (*Chat, error) {
return b.client.GetChatInfo(chatID)
}

// GetFileInfo returns information about file:
// id, type, size, filename, url
func (b *Bot) GetFileInfo(fileID string) (*File, error) {
return b.client.GetFileInfo(fileID)
}

// NewMessage returns new message
func (b *Bot) NewMessage(chatID string) *Message {
return &Message{
Expand All @@ -50,6 +63,15 @@ func (b *Bot) NewFileMessage(chatID string, file *os.File) *Message {
}
}

// NewFileMessageByFileID returns new message with previously uploaded file id
func (b *Bot) NewFileMessageByFileID(chatID, fileID string) *Message {
return &Message{
client: b.client,
Chat: Chat{ID: chatID},
FileID: fileID,
}
}

// NewMessageFromPart returns new message based on part message
func (b *Bot) NewMessageFromPart(message PartMessage) *Message {
return &Message{
Expand All @@ -62,7 +84,7 @@ func (b *Bot) NewMessageFromPart(message PartMessage) *Message {
}

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

apiURL, debug := defaultAPIURL, defaultDebug
for _, option := range opts {
switch option.Type() {
case "api_url":
Expand Down
2 changes: 1 addition & 1 deletion chat.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package goicqbot

//go:generate easyjson -all chat.go

type Chat struct {
client *Client
Expand All @@ -22,4 +23,3 @@ type Chat struct {

Admins []Contact `json:"admins"`
}

159 changes: 159 additions & 0 deletions chat_easyjson.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 19 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ func (c *Client) GetInfo() (*BotInfo, error) {
return info, nil
}

func (c *Client) GetChatInfo(chatId string) (*Chat, error) {
func (c *Client) GetChatInfo(chatID string) (*Chat, error) {
params := url.Values{
"chatId": {chatId},
"chatId": {chatID},
}
response, err := c.Do("/chats/getInfo", params, nil)
if err != nil {
Expand Down Expand Up @@ -148,6 +148,23 @@ func (c *Client) GetChatInfo(chatId string) (*Chat, error) {
return chat, nil
}

func (c *Client) GetFileInfo(fileID string) (*File, error) {
params := url.Values{
"fileId": {fileID},
}
response, err := c.Do("/files/getInfo", params, nil)
if err != nil {
return nil, fmt.Errorf("error while receiving information: %s", err)
}

file := &File{}
if err := json.Unmarshal(response, file); err != nil {
return nil, fmt.Errorf("error while unmarshalling information: %s", err)
}

return file, nil
}

func (c *Client) SendMessage(message *Message) error {
params := url.Values{
"chatId": []string{message.Chat.ID},
Expand Down
4 changes: 2 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestClient_GetEvents_OK(t *testing.T) {
{
Type: FORWARD,
Payload: PartPayload{
Message: PartMessage{
PartMessage: PartMessage{
MsgID: "12354",
Text: "test1",
},
Expand All @@ -113,7 +113,7 @@ func TestClient_GetEvents_OK(t *testing.T) {
{
Type: REPLY,
Payload: PartPayload{
Message: PartMessage{
PartMessage: PartMessage{
MsgID: "12354",
Text: "test",
},
Expand Down
5 changes: 3 additions & 2 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package main
import (
"context"
"fmt"
"github.com/DmitryDorofeev/goicqbot"
"log"
"os"
"time"

"github.com/DmitryDorofeev/goicqbot"
)

func main() {
Expand Down Expand Up @@ -35,7 +36,7 @@ func main() {
fileMessage.Delete()

// Simple 30-seconds echo bot
ctx, _ := context.WithTimeout(context.Background(), 30 * time.Second)
ctx, _ := context.WithTimeout(context.Background(), 30*time.Second)
updates := bot.GetUpdatesChannel(ctx)
for update := range updates {
fmt.Println(update.Type, update.Payload)
Expand Down
20 changes: 20 additions & 0 deletions file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package goicqbot

//go:generate easyjson -all file.go

type File struct {
// Id of the file
ID string `json:"fileId"`

// Type of the file
Type string `json:"type"`

// Size in bytes
Size uint64 `json:"size"`

// Name of file
Name bool `json:"filename"`

// URL to the file
URL string `json:"url"`
}
Loading