From 9ce24ee41c5649ca5a03edf60bef1606dca6a8d3 Mon Sep 17 00:00:00 2001 From: Alexander Kiryukhin Date: Sat, 30 Mar 2019 22:12:37 +0300 Subject: Keyboard builder Logger --- api.go | 16 +++++++++++++- models.go | 23 +++++++++++++------ utils.go | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 8 deletions(-) create mode 100644 utils.go diff --git a/api.go b/api.go index 3d727fd..ec45e73 100644 --- a/api.go +++ b/api.go @@ -10,6 +10,7 @@ import ( "fmt" "io" "io/ioutil" + "log" "net/http" "net/url" "strconv" @@ -22,6 +23,7 @@ type Api struct { url *url.URL timeout int pause int + logging bool } // New TamTam Api object @@ -33,9 +35,14 @@ func New(key string) *Api { version: "1.0.3", timeout: 30, pause: 1, + logging: false, } } +func (a *Api) EnableLogging() { + a.logging = true +} + // region Misc methods func (a *Api) GetMe() (*UserWithPhoto, error) { @@ -464,7 +471,11 @@ func (a *Api) getUpdates(limit int, timeout int, marker int64, types []string) ( return result, err } defer body.Close() - return result, json.NewDecoder(body).Decode(result) + jb, _ := ioutil.ReadAll(body) + if a.logging { + log.Printf("Received: %s", string(jb)) + } + return result, json.Unmarshal(jb, result) } func (a *Api) request(method, path string, query url.Values, body interface{}) (io.ReadCloser, error) { @@ -478,6 +489,9 @@ func (a *Api) request(method, path string, query url.Values, body interface{}) ( if err != nil { return nil, err } + if a.logging { + log.Printf("Sent: [%s %s] Query: %#v Body: %s", method, path, query, string(j)) + } req, err := http.NewRequest(method, u.String(), bytes.NewReader(j)) if err != nil { return nil, err diff --git a/models.go b/models.go index 310baf6..f2f13ea 100644 --- a/models.go +++ b/models.go @@ -113,7 +113,7 @@ type CallbackAnswer struct { // After pressing this type of button client sends to server payload it contains type CallbackButton struct { - Type string `json:"type,omitempty"` + Type ButtonType `json:"type,omitempty"` // Visible text of button Text string `json:"text"` // Intent of button. Affects clients representation. @@ -301,8 +301,8 @@ type Image struct { type InlineKeyboardAttachment struct { Type string `json:"type,omitempty"` // Unique identifier of keyboard - CallbackId string `json:"callback_id"` - Payload Keyboard `json:"payload"` + //CallbackId string `json:"callback_id"` + Payload Keyboard `json:"payload"` } // Request to attach keyboard to message @@ -316,6 +316,15 @@ type InlineKeyboardAttachmentRequestPayload struct { Buttons [][]Button `json:"buttons"` } +type ButtonType string + +const ( + LINK ButtonType = "link" + CALLBACK ButtonType = "callback" + CONTACT ButtonType = "request_contact" + GEOLOCATION ButtonType = "request_geo_location" +) + // Intent : Intent of button type Intent string @@ -328,12 +337,12 @@ const ( // Keyboard is two-dimension array of buttons type Keyboard struct { - Buttons [][]Button `json:"buttons"` + Buttons interface{} `json:"buttons"` } // After pressing this type of button user follows the link it contains type LinkButton struct { - Type string `json:"type,omitempty"` + Type ButtonType `json:"type,omitempty"` // Visible text of button Text string `json:"text"` // Intent of button. Affects clients representation. @@ -516,7 +525,7 @@ type Recipient struct { // After pressing this type of button client sends new message with attachment of curent user contact type RequestContactButton struct { - Type string `json:"type,omitempty"` + Type ButtonType `json:"type,omitempty"` // Visible text of button Text string `json:"text"` // Intent of button. Affects clients representation. @@ -525,7 +534,7 @@ type RequestContactButton struct { // After pressing this type of button client sends new message with attachment of current user geo location type RequestGeoLocationButton struct { - Type string `json:"type,omitempty"` + Type ButtonType `json:"type,omitempty"` // Visible text of button Text string `json:"text"` // Intent of button. Affects clients representation. diff --git a/utils.go b/utils.go new file mode 100644 index 0000000..0ae24c4 --- /dev/null +++ b/utils.go @@ -0,0 +1,76 @@ +package tamtam + +type KeyboardBuilder struct { + rows []*KeyboardRow +} + +func NewKeyboardBuilder() *KeyboardBuilder { + return &KeyboardBuilder{ + rows: make([]*KeyboardRow, 0), + } +} + +func (k *KeyboardBuilder) AddRow() *KeyboardRow { + kr := &KeyboardRow{} + k.rows = append(k.rows, kr) + return kr +} + +func (k *KeyboardBuilder) Build() Keyboard { + buttons := make([][]interface{}, 0, len(k.rows)) + for _, r := range k.rows { + buttons = append(buttons, r.Build()) + } + return Keyboard{Buttons: buttons} +} + +type KeyboardRow struct { + cols []interface{} +} + +func (k *KeyboardRow) Build() []interface{} { + return k.cols +} + +func (k *KeyboardRow) AddLink(text string, intent Intent, url string) *KeyboardRow { + b := LinkButton{ + Text: text, + Url: url, + Intent: intent, + Type: LINK, + } + k.cols = append(k.cols, b) + return k +} + +func (k *KeyboardRow) AddCallback(text string, intent Intent, payload string) *KeyboardRow { + b := CallbackButton{ + Text: text, + Payload: payload, + Intent: intent, + Type: CALLBACK, + } + k.cols = append(k.cols, b) + return k +} + +func (k *KeyboardRow) AddContact(text string, intent Intent, url string) *KeyboardRow { + b := RequestContactButton{ + Text: text, + Intent: intent, + Type: CONTACT, + } + k.cols = append(k.cols, b) + return k +} + +func (k *KeyboardRow) AddGeolocation(text string, intent Intent, quick bool) *KeyboardRow { + b := RequestGeoLocationButton{ + Text: text, + Quick: quick, + Intent: intent, + Type: GEOLOCATION, + } + k.cols = append(k.cols, b) + return k +} -- cgit v1.2.3