aboutsummaryrefslogtreecommitdiff
path: root/messages.go
diff options
context:
space:
mode:
Diffstat (limited to 'messages.go')
-rw-r--r--messages.go100
1 files changed, 60 insertions, 40 deletions
diff --git a/messages.go b/messages.go
index 5131479..7c20e05 100644
--- a/messages.go
+++ b/messages.go
@@ -2,10 +2,13 @@ package tamtam
import (
"encoding/json"
+ "errors"
"log"
"net/http"
"net/url"
"strconv"
+
+ "github.com/neonxp/tamtam/schemes"
)
type messages struct {
@@ -17,8 +20,8 @@ func newMessages(client *client) *messages {
}
//GetMessages returns messages in chat: result page and marker referencing to the next page. Messages traversed in reverse direction so the latest message in chat will be first in result array. Therefore if you use from and to parameters, to must be less than from
-func (a *messages) GetMessages(chatID int, messageIDs []string, from int, to int, count int) (*MessageList, error) {
- result := new(MessageList)
+func (a *messages) GetMessages(chatID int, messageIDs []string, from int, to int, count int) (*schemes.MessageList, error) {
+ result := new(schemes.MessageList)
values := url.Values{}
if chatID != 0 {
values.Set("chat_id", strconv.Itoa(int(chatID)))
@@ -49,17 +52,24 @@ func (a *messages) GetMessages(chatID int, messageIDs []string, from int, to int
return result, json.NewDecoder(body).Decode(result)
}
-//SendMessage sends a message to a chat. As a result for this method new message identifier returns.
-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)))
+//EditMessage updates message by id
+func (a *messages) EditMessage(messageID int, message *Message) error {
+ s, err := a.editMessage(messageID, message.message)
+ if err != nil {
+ return err
}
- if userID != 0 {
- values.Set("user_id", strconv.Itoa(int(userID)))
+ if !s.Success {
+ return errors.New(s.Message)
}
- body, err := a.client.request(http.MethodPost, "messages", values, message)
+ return nil
+}
+
+//DeleteMessage deletes message by id
+func (a *messages) DeleteMessage(messageID int) (*schemes.SimpleQueryResult, error) {
+ result := new(schemes.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
}
@@ -71,12 +81,12 @@ func (a *messages) SendMessage(chatID int, userID int, message *NewMessageBody)
return result, json.NewDecoder(body).Decode(result)
}
-//EditMessage updates message by id
-func (a *messages) EditMessage(messageID int, message *NewMessageBody) (*SimpleQueryResult, error) {
- result := new(SimpleQueryResult)
+//AnswerOnCallback should be called to send an answer after a user has clicked the button. The answer may be an updated message or/and a one-time user notification.
+func (a *messages) AnswerOnCallback(callbackID string, callback *schemes.CallbackAnswer) (*schemes.SimpleQueryResult, error) {
+ result := new(schemes.SimpleQueryResult)
values := url.Values{}
- values.Set("message_id", strconv.Itoa(int(messageID)))
- body, err := a.client.request(http.MethodPut, "messages", values, message)
+ values.Set("callback_id", callbackID)
+ body, err := a.client.request(http.MethodPost, "answers", values, callback)
if err != nil {
return result, err
}
@@ -88,29 +98,46 @@ func (a *messages) EditMessage(messageID int, message *NewMessageBody) (*SimpleQ
return result, json.NewDecoder(body).Decode(result)
}
-//DeleteMessage deletes message by id
-func (a *messages) DeleteMessage(messageID int) (*SimpleQueryResult, error) {
- result := new(SimpleQueryResult)
+//NewKeyboardBuilder returns new keyboard builder helper
+func (a *messages) NewKeyboardBuilder() *Keyboard {
+ return &Keyboard{
+ rows: make([]*KeyboardRow, 0),
+ }
+}
+
+//Send sends a message to a chat. As a result for this method new message identifier returns.
+func (a *messages) Send(m *Message) error {
+ return a.sendMessage(m.chatID, m.userID, m.message)
+}
+
+func (a *messages) sendMessage(chatID int, userID int, message *schemes.NewMessageBody) error {
+ result := new(schemes.Error)
values := url.Values{}
- values.Set("message_id", strconv.Itoa(int(messageID)))
- body, err := a.client.request(http.MethodDelete, "messages", values, nil)
+ 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
+ return err
}
- defer func() {
- if err := body.Close(); err != nil {
- log.Println(err)
- }
- }()
- return result, json.NewDecoder(body).Decode(result)
+ defer body.Close()
+ if err := json.NewDecoder(body).Decode(result); err != nil {
+ return err
+ }
+ if result.Code == "" {
+ return nil
+ }
+ return result
}
-//AnswerOnCallback should be called to send an answer after a user has clicked the button. The answer may be an updated message or/and a one-time user notification.
-func (a *messages) AnswerOnCallback(callbackID string, callback *CallbackAnswer) (*SimpleQueryResult, error) {
- result := new(SimpleQueryResult)
+func (a *messages) editMessage(messageID int, message *schemes.NewMessageBody) (*schemes.SimpleQueryResult, error) {
+ result := new(schemes.SimpleQueryResult)
values := url.Values{}
- values.Set("callback_id", callbackID)
- body, err := a.client.request(http.MethodPost, "answers", values, callback)
+ values.Set("message_id", strconv.Itoa(int(messageID)))
+ body, err := a.client.request(http.MethodPut, "messages", values, message)
if err != nil {
return result, err
}
@@ -121,10 +148,3 @@ func (a *messages) AnswerOnCallback(callbackID string, callback *CallbackAnswer)
}()
return result, json.NewDecoder(body).Decode(result)
}
-
-//NewKeyboardBuilder returns new keyboard builder helper
-func (a *messages) NewKeyboardBuilder() *KeyboardBuilder {
- return &KeyboardBuilder{
- rows: make([]*KeyboardRow, 0),
- }
-}