diff options
author | Alexander Kiryukhin <a.kiryukhin@mail.ru> | 2019-08-09 02:02:05 +0300 |
---|---|---|
committer | Alexander Kiryukhin <a.kiryukhin@mail.ru> | 2019-08-09 02:02:05 +0300 |
commit | 94c96cef29cd83bc60b2eabb8bb89c9fd8c7059b (patch) | |
tree | 676b3e2b6ffb171e102931e5fac01f7a4621c20a /messages.go | |
parent | d13acd7da9bd1c08de33b91120a479df88d21db2 (diff) |
0.1.8 Release
Diffstat (limited to 'messages.go')
-rw-r--r-- | messages.go | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/messages.go b/messages.go new file mode 100644 index 0000000..c7cad45 --- /dev/null +++ b/messages.go @@ -0,0 +1,126 @@ +// Package tamtam implements TamTam Bot API +// Copyright (c) 2019 Alexander Kiryukhin <a.kiryukhin@mail.ru> +package tamtam + +import ( + "encoding/json" + "log" + "net/http" + "net/url" + "strconv" +) + +type messages struct { + client *client +} + +func newMessages(client *client) *messages { + return &messages{client: client} +} + +func (a *messages) GetMessages(chatID int, messageIDs []string, from int, to int, count int) (*MessageList, error) { + result := new(MessageList) + values := url.Values{} + if chatID != 0 { + values.Set("chat_id", strconv.Itoa(int(chatID))) + } + if len(messageIDs) > 0 { + for _, mid := range messageIDs { + values.Add("message_ids", mid) + } + } + if from != 0 { + values.Set("from", strconv.Itoa(int(from))) + } + if to != 0 { + values.Set("to", strconv.Itoa(int(to))) + } + if count > 0 { + values.Set("count", strconv.Itoa(int(count))) + } + body, err := a.client.request(http.MethodGet, "messages", 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) +} + +func (a *messages) SendMessage(chatID int, userID int, message *NewMessageBody) (*Message, error) { + result := new(Message) + values := url.Values{} + if chatID != 0 { + values.Set("chat_id", strconv.Itoa(int(chatID))) + } + if userID != 0 { + values.Set("user_id", strconv.Itoa(int(userID))) + } + body, err := a.client.request(http.MethodPost, "messages", values, message) + if err != nil { + return result, err + } + defer func() { + if err := body.Close(); err != nil { + log.Println(err) + } + }() + return result, json.NewDecoder(body).Decode(result) +} + +func (a *messages) EditMessage(messageID int, message *NewMessageBody) (*SimpleQueryResult, error) { + result := new(SimpleQueryResult) + values := url.Values{} + values.Set("message_id", strconv.Itoa(int(messageID))) + body, err := a.client.request(http.MethodPut, "messages", values, message) + if err != nil { + return result, err + } + defer func() { + if err := body.Close(); err != nil { + log.Println(err) + } + }() + return result, json.NewDecoder(body).Decode(result) +} + +func (a *messages) DeleteMessage(messageID int) (*SimpleQueryResult, error) { + result := new(SimpleQueryResult) + values := url.Values{} + values.Set("message_id", strconv.Itoa(int(messageID))) + body, err := a.client.request(http.MethodDelete, "messages", 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) +} + +func (a *messages) AnswerOnCallback(callbackID int, callback *CallbackAnswer) (*SimpleQueryResult, error) { + result := new(SimpleQueryResult) + values := url.Values{} + values.Set("callback_id", strconv.Itoa(int(callbackID))) + body, err := a.client.request(http.MethodPost, "answers", values, callback) + if err != nil { + return result, err + } + defer func() { + if err := body.Close(); err != nil { + log.Println(err) + } + }() + return result, json.NewDecoder(body).Decode(result) +} + +func (a *messages) NewKeyboardBuilder() *KeyboardBuilder { + return &KeyboardBuilder{ + rows: make([]*KeyboardRow, 0), + } +} |