Skip to content

Commit

Permalink
Merge pull request #10 from mail-ru-im/chats-new-methods
Browse files Browse the repository at this point in the history
05.03.2020 API update: chats new methods
  • Loading branch information
DmitryDorofeev authored Apr 17, 2020
2 parents e713f2b + 2b5e3bd commit a7da30e
Show file tree
Hide file tree
Showing 11 changed files with 1,231 additions and 374 deletions.
34 changes: 9 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
<img src="https://github.com/mail-ru-im/bot-python/blob/master/logo.png" width="100" height="100">

# Golang interface for bot API
# Golang interface for Mail.ru Instant Messengers bot API
![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)
[![CircleCI](https://circleci.com/gh/mail-ru-im/bot-golang.svg?style=svg)](https://circleci.com/gh/mail-ru-im/bot-golang)
[![go.dev reference](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white&style=flat)](https://pkg.go.dev/github.com/mail-ru-im/bot-golang)

- *Brand new Bot API!*

- *Zero-configuration library*

- *Simple and clear interface*

## API specification:
### [<img src="https://icq.com/cached/img/landing/icon_and_192.png" width="15"> ICQ New ](https://icq.com/botapi/)
### [<img src="https://is3-ssl.mzstatic.com/image/thumb/Purple123/v4/e8/4f/1b/e84f1b57-206f-7750-ac5a-27f93ff4a0d8/icons-bundle.png/460x0w.png" width="16"> Myteam ](https://myteam.mail.ru/botapi/)

### [<img src="https://agent.mail.ru/img/agent2014/common/2x/button_logo.png" width="16"> Agent Mail.ru](https://agent.mail.ru/botapi/)

## Install
```bash
go get github.com/mail-ru-im/bot-golang
Expand Down Expand Up @@ -82,27 +90,3 @@ And debug all api requests and responses:
```go
bot := botgolang.NewBot(BOT_TOKEN, botgolang.BotDebug(true))
```


## Roadmap

- [x] Send message

- [x] Events subscription

- [x] Tests

- [x] Godoc

- [x] Edit message

- [x] Send files

- [x] Delete message

- [x] Chat info

- [x] Send voice

- [x] File info

62 changes: 61 additions & 1 deletion bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,66 @@ func (b *Bot) GetChatInfo(chatID string) (*Chat, error) {
return b.client.GetChatInfo(chatID)
}

// SendChatActions sends an actions like "typing, looking"
func (b *Bot) SendChatActions(chatID string, actions ...ChatAction) error {
return b.client.SendChatActions(chatID, actions...)
}

// GetChatAdmins returns chat admins list with fields:
// userID, creator flag
func (b *Bot) GetChatAdmins(chatID string) ([]ChatMember, error) {
return b.client.GetChatAdmins(chatID)
}

// GetChatMem returns chat members list with fields:
// userID, creator flag, admin flag
func (b *Bot) GetChatMembers(chatID string) ([]ChatMember, error) {
return b.client.GetChatMembers(chatID)
}

// GetChatBlockedUsers returns chat blocked users list:
// userID
func (b *Bot) GetChatBlockedUsers(chatID string) ([]User, error) {
return b.client.GetChatBlockedUsers(chatID)
}

// GetChatPendingUsers returns chat join pending users list:
// userID
func (b *Bot) GetChatPendingUsers(chatID string) ([]User, error) {
return b.client.GetChatPendingUsers(chatID)
}

// BlockChatUser blocks user and removes him from chat.
// If deleteLastMessages is true, the messages written recently will be deleted
func (b *Bot) BlockChatUser(chatID, userID string, deleteLastMessages bool) error {
return b.client.BlockChatUser(chatID, userID, deleteLastMessages)
}

// UnblockChatUser unblocks user in chat
func (b *Bot) UnblockChatUser(chatID, userID string) error {
return b.client.UnblockChatUser(chatID, userID)
}

// ResolveChatJoinRequests sends a decision to accept/decline user join to chat
func (b *Bot) ResolveChatJoinRequests(chatID, userID string, accept, everyone bool) error {
return b.client.ResolveChatPending(chatID, userID, accept, everyone)
}

// SetChatTitle changes chat title
func (b *Bot) SetChatTitle(chatID, title string) error {
return b.client.SetChatTitle(chatID, title)
}

// SetChatAbout changes chat about
func (b *Bot) SetChatAbout(chatID, about string) error {
return b.client.SetChatAbout(chatID, about)
}

// SetChatRules changes chat rules
func (b *Bot) SetChatRules(chatID, rules string) error {
return b.client.SetChatRules(chatID, rules)
}

// GetFileInfo returns information about file:
// id, type, size, filename, url
func (b *Bot) GetFileInfo(fileID string) (*File, error) {
Expand Down Expand Up @@ -119,7 +179,7 @@ func (b *Bot) NewMessageFromPart(message PartMessage) *Message {
return &Message{
client: b.client,
ID: message.MsgID,
Chat: Chat{ID: message.From.UserID, Title: message.From.FirstName},
Chat: Chat{ID: message.From.User.ID, Title: message.From.FirstName},
Text: message.Text,
Timestamp: message.Timestamp,
}
Expand Down
24 changes: 12 additions & 12 deletions button_easyjson.go

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

114 changes: 110 additions & 4 deletions chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,130 @@ package botgolang

//go:generate easyjson -all chat.go

type ChatAction = string

const (
TypingAction ChatAction = "typing"
LookingAction ChatAction = "looking"
)

type ChatType = string

const (
Private ChatType = "private"
Group ChatType = "group"
Channel ChatType = "channel"
)

type Chat struct {
client *Client
// Id of the chat
ID string `json:"chatId"`

// Type of the chat: channel or group
Type string `json:"type"`
// Type of the chat: channel, group or private
Type ChatType `json:"type"`

// First name of the user
FirstName string `json:"firstName"`

// Last name of the user
LastName string `json:"lastName"`

// Nick of the user
Nick string `json:"nick"`

// User about or group/channel description
About string `json:"about"`

// Rules of the group/channel
Rules string `json:"rules"`

// Title of the chat
Title string `json:"title"`

// Flag that indicates that requested chat is the bot
IsBot bool `json:"isBot"`

// Is this chat public?
Public bool `json:"public"`

Group string `json:"group"`
// Is this chat has join moderation?
JoinModeration bool `json:"joinModeration"`

// You can send this link to all your friends
InviteLink string `json:"inviteLink"`
}

func (c *Chat) resolveID() string {
switch c.Type {
case Private:
return c.Nick
default:
return c.ID
}
}

// Send bot actions to the chat
//
// You can call this method every time you change the current actions,
// or every 10 seconds if the actions have not changed. After sending a
// request without active action, you should not re-notify of their absence.
func (c *Chat) SendActions(actions ...ChatAction) error {
return c.client.SendChatActions(c.resolveID(), actions...)
}

// Get chat administrators list
func (c *Chat) GetAdmins() ([]ChatMember, error) {
return c.client.GetChatAdmins(c.ID)
}

// Get chat members list
func (c *Chat) GetMembers() ([]ChatMember, error) {
return c.client.GetChatMembers(c.ID)
}

// Get chat blocked users list
func (c *Chat) GetBlockedUsers() ([]User, error) {
return c.client.GetChatBlockedUsers(c.ID)
}

// Get chat join pending users list
func (c *Chat) GetPendingUsers() ([]User, error) {
return c.client.GetChatPendingUsers(c.ID)
}

// Block user and remove him from chat.
// If deleteLastMessages is true, the messages written recently will be deleted
func (c *Chat) BlockUser(userID string, deleteLastMessages bool) error {
return c.client.BlockChatUser(c.ID, userID, deleteLastMessages)
}

// Unblock user in chat (but not add him back)
func (c *Chat) UnblockUser(userID string) error {
return c.client.UnblockChatUser(c.ID, userID)
}

// ResolveJoinRequest resolve specific user chat join request
func (c *Chat) ResolveJoinRequest(userID string, accept bool) error {
return c.client.ResolveChatPending(c.ID, userID, accept, false)
}

// ResolveAllJoinRequest resolve all chat join requests
func (c *Chat) ResolveAllJoinRequests(accept bool) error {
return c.client.ResolveChatPending(c.ID, "", accept, true)
}

// SetTitle changes chat title
func (c *Chat) SetTitle(title string) error {
return c.client.SetChatTitle(c.ID, title)
}

// SetAbout changes chat about
func (c *Chat) SetAbout(about string) error {
return c.client.SetChatAbout(c.ID, about)
}

Admins []Contact `json:"admins"`
// SetRules changes chat rules
func (c *Chat) SetRules(rules string) error {
return c.client.SetChatRules(c.ID, rules)
}
Loading

0 comments on commit a7da30e

Please sign in to comment.