tamtam/chats.go

178 lines
4.9 KiB
Go
Raw Normal View History

2019-08-09 02:02:05 +03:00
package tamtam
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"strconv"
2019-09-05 22:43:32 +03:00
"github.com/neonxp/tamtam/schemes"
2019-08-09 02:02:05 +03:00
)
type chats struct {
client *client
}
func newChats(client *client) *chats {
return &chats{client: client}
}
2019-08-09 02:16:08 +03:00
//GetChats returns information about chats that bot participated in: a result list and marker points to the next page
2019-11-20 15:14:54 +03:00
func (a *chats) GetChats(count, marker int64) (*schemes.ChatList, error) {
2019-09-05 22:43:32 +03:00
result := new(schemes.ChatList)
2019-08-09 02:02:05 +03:00
values := url.Values{}
if count > 0 {
values.Set("count", strconv.Itoa(int(count)))
}
if marker > 0 {
values.Set("marker", strconv.Itoa(int(marker)))
}
body, err := a.client.request(http.MethodGet, "chats", values, nil)
if err != nil {
return result, err
}
defer func() {
if err := body.Close(); err != nil {
log.Println(err)
}
}()
return result, json.NewDecoder(body).Decode(result)
}
2019-08-09 02:16:08 +03:00
//GetChat returns info about chat
2019-11-20 15:14:54 +03:00
func (a *chats) GetChat(chatID int64) (*schemes.Chat, error) {
2019-09-05 22:43:32 +03:00
result := new(schemes.Chat)
2019-08-09 02:02:05 +03:00
values := url.Values{}
body, err := a.client.request(http.MethodGet, fmt.Sprintf("chats/%d", chatID), values, nil)
if err != nil {
return result, err
}
defer func() {
if err := body.Close(); err != nil {
log.Println(err)
}
}()
return result, json.NewDecoder(body).Decode(result)
}
2019-08-09 02:16:08 +03:00
//GetChatMembership returns chat membership info for current bot
2019-11-20 15:14:54 +03:00
func (a *chats) GetChatMembership(chatID int64) (*schemes.ChatMember, error) {
2019-09-05 22:43:32 +03:00
result := new(schemes.ChatMember)
2019-08-09 02:02:05 +03:00
values := url.Values{}
body, err := a.client.request(http.MethodGet, fmt.Sprintf("chats/%d/members/me", chatID), values, nil)
if err != nil {
return result, err
}
defer func() {
if err := body.Close(); err != nil {
log.Println(err)
}
}()
return result, json.NewDecoder(body).Decode(result)
}
2019-08-09 02:16:08 +03:00
//GetChatMembers returns users participated in chat
2019-11-20 15:14:54 +03:00
func (a *chats) GetChatMembers(chatID, count, marker int64) (*schemes.ChatMembersList, error) {
2019-09-05 22:43:32 +03:00
result := new(schemes.ChatMembersList)
2019-08-09 02:02:05 +03:00
values := url.Values{}
if count > 0 {
values.Set("count", strconv.Itoa(int(count)))
}
if marker > 0 {
values.Set("marker", strconv.Itoa(int(marker)))
}
body, err := a.client.request(http.MethodGet, fmt.Sprintf("chats/%d/members", chatID), values, nil)
if err != nil {
return result, err
}
defer func() {
if err := body.Close(); err != nil {
log.Println(err)
}
}()
return result, json.NewDecoder(body).Decode(result)
}
2019-08-09 02:16:08 +03:00
//LeaveChat removes bot from chat members
2019-11-20 15:14:54 +03:00
func (a *chats) LeaveChat(chatID int64) (*schemes.SimpleQueryResult, error) {
2019-09-05 22:43:32 +03:00
result := new(schemes.SimpleQueryResult)
2019-08-09 02:02:05 +03:00
values := url.Values{}
body, err := a.client.request(http.MethodDelete, fmt.Sprintf("chats/%d/members/me", chatID), values, nil)
if err != nil {
return result, err
}
defer func() {
if err := body.Close(); err != nil {
log.Println(err)
}
}()
return result, json.NewDecoder(body).Decode(result)
}
2019-08-09 02:16:08 +03:00
//EditChat edits chat info: title, icon, etc…
2019-11-20 15:14:54 +03:00
func (a *chats) EditChat(chatID int64, update *schemes.ChatPatch) (*schemes.Chat, error) {
2019-09-05 22:43:32 +03:00
result := new(schemes.Chat)
2019-08-09 02:02:05 +03:00
values := url.Values{}
body, err := a.client.request(http.MethodPatch, fmt.Sprintf("chats/%d", chatID), values, update)
if err != nil {
return result, err
}
defer func() {
if err := body.Close(); err != nil {
log.Println(err)
}
}()
return result, json.NewDecoder(body).Decode(result)
}
2019-08-09 02:16:08 +03:00
//AddMember adds members to chat. Additional permissions may require.
2019-11-20 15:14:54 +03:00
func (a *chats) AddMember(chatID int64, users schemes.UserIdsList) (*schemes.SimpleQueryResult, error) {
2019-09-05 22:43:32 +03:00
result := new(schemes.SimpleQueryResult)
2019-08-09 02:02:05 +03:00
values := url.Values{}
body, err := a.client.request(http.MethodPost, fmt.Sprintf("chats/%d/members", chatID), values, users)
if err != nil {
return result, err
}
defer func() {
if err := body.Close(); err != nil {
log.Println(err)
}
}()
return result, json.NewDecoder(body).Decode(result)
}
2019-08-09 02:16:08 +03:00
//RemoveMember removes member from chat. Additional permissions may require.
2019-11-20 15:14:54 +03:00
func (a *chats) RemoveMember(chatID int64, userID int64) (*schemes.SimpleQueryResult, error) {
2019-09-05 22:43:32 +03:00
result := new(schemes.SimpleQueryResult)
2019-08-09 02:02:05 +03:00
values := url.Values{}
values.Set("user_id", strconv.Itoa(int(userID)))
body, err := a.client.request(http.MethodDelete, fmt.Sprintf("chats/%d/members", chatID), values, nil)
if err != nil {
return result, err
}
defer func() {
if err := body.Close(); err != nil {
log.Println(err)
}
}()
return result, json.NewDecoder(body).Decode(result)
}
2019-08-09 02:16:08 +03:00
//SendAction send bot action to chat
2019-11-20 15:14:54 +03:00
func (a *chats) SendAction(chatID int64, action schemes.SenderAction) (*schemes.SimpleQueryResult, error) {
2019-09-05 22:43:32 +03:00
result := new(schemes.SimpleQueryResult)
2019-08-09 02:02:05 +03:00
values := url.Values{}
2019-09-05 22:43:32 +03:00
body, err := a.client.request(http.MethodPost, fmt.Sprintf("chats/%d/actions", chatID), values, schemes.ActionRequestBody{Action: action})
2019-08-09 02:02:05 +03:00
if err != nil {
return result, err
}
defer func() {
if err := body.Close(); err != nil {
log.Println(err)
}
}()
return result, json.NewDecoder(body).Decode(result)
}