From 47d3352ee54a3be2e7169f65ab4d834c74a24f01 Mon Sep 17 00:00:00 2001 From: bulletmys Date: Sun, 12 Apr 2020 14:59:41 +0300 Subject: [PATCH 1/6] fixed func doc --- bot.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot.go b/bot.go index 7d0aa9d..2636717 100644 --- a/bot.go +++ b/bot.go @@ -83,7 +83,7 @@ func (b *Bot) NewFileMessageByFileID(chatID, fileID string) *Message { } } -// NewFileMessage returns new voice message +// NewVoiceMessage returns new voice message func (b *Bot) NewVoiceMessage(chatID string, file *os.File) *Message { return &Message{ client: b.client, From 78ccc9cf571acee4c4b0cc86c07dcc6b5b727c36 Mon Sep 17 00:00:00 2001 From: bulletmys Date: Mon, 13 Apr 2020 01:15:10 +0300 Subject: [PATCH 2/6] [ADD] button functionality --- bot.go | 22 ++ button.go | 59 +++++ button_easyjson.go | 193 ++++++++++++++ client.go | 25 ++ message.go | 7 + types.go | 16 ++ types_easyjson.go | 630 +++++++++++++++++++++++---------------------- 7 files changed, 644 insertions(+), 308 deletions(-) create mode 100644 button.go create mode 100644 button_easyjson.go diff --git a/bot.go b/bot.go index 2636717..0a4cfe9 100644 --- a/bot.go +++ b/bot.go @@ -63,6 +63,17 @@ func (b *Bot) NewTextMessage(chatID, text string) *Message { } } +// NewInlineKeyboardMessage returns new text message with inline keyboard +func (b *Bot) NewInlineKeyboardMessage(chatID, text string, keyboard [][]Button) *Message { + return &Message{ + client: b.client, + Chat: Chat{ID: chatID}, + Text: text, + ContentType: Text, + InlineKeyboard: keyboard, + } +} + // NewFileMessage returns new file message func (b *Bot) NewFileMessage(chatID string, file *os.File) *Message { return &Message{ @@ -114,6 +125,17 @@ func (b *Bot) NewMessageFromPart(message PartMessage) *Message { } } +// NewButtonResponse returns new ButtonResponse +func (b *Bot) NewButtonResponse(queryID, url, text string, showAlert bool) *ButtonResponse { + return &ButtonResponse{ + client: b.client, + QueryID: queryID, + Text: text, + URL: url, + ShowAlert: showAlert, + } +} + func (b *Bot) NewChat(id string) *Chat { return &Chat{ client: b.client, diff --git a/button.go b/button.go new file mode 100644 index 0000000..a6a1a8b --- /dev/null +++ b/button.go @@ -0,0 +1,59 @@ +package botgolang + +//go:generate easyjson -all button.go + +// Button represents a button in inline keyboard +type Button struct { + // Button text + Text string `json:"text"` + + // URL to be opened + // You can't use it with CallbackData + URL string `json:"url,omitempty"` + + // Data that identify the button + // You can't use it with URL + CallbackData string `json:"callbackData,omitempty"` +} + +// NewURLButton returns new button with URL field +func NewURLButton(text string, url string) Button { + return Button{ + Text: text, + URL: url, + } +} + +// NewCallbackButton returns new button with CallbackData field +func NewCallbackButton(text string, callbackData string) Button { + return Button{ + Text: text, + CallbackData: callbackData, + } +} + +// ButtonResponse represents a data that is returned when a button is clicked +type ButtonResponse struct { + client *Client + + // Id of the query + QueryID string `json:"queryId"` + + // Text of the response message + Text string `json:"text"` + + // Display alert? + ShowAlert bool `json:"showAlert"` + + // URL to be opened + URL string `json:"url"` + + // CallbackData of the query (id of the pressed button). + CallbackData string `json:"callbackData"` +} + +// Send method sends your response message. +// Make sure you have QueryID in your ButtonResponse. +func (cl *ButtonResponse) Send() error { + return cl.client.SendAnswerCallbackQuery(cl) +} diff --git a/button_easyjson.go b/button_easyjson.go new file mode 100644 index 0000000..e0f5c7f --- /dev/null +++ b/button_easyjson.go @@ -0,0 +1,193 @@ +// Code generated by easyjson for marshaling/unmarshaling. DO NOT EDIT. + +package botgolang + +import ( + json "encoding/json" + easyjson "github.com/mailru/easyjson" + jlexer "github.com/mailru/easyjson/jlexer" + jwriter "github.com/mailru/easyjson/jwriter" +) + +// suppress unused package warning +var ( + _ *json.RawMessage + _ *jlexer.Lexer + _ *jwriter.Writer + _ easyjson.Marshaler +) + +func easyjsonF248ab8DecodeBotGolang(in *jlexer.Lexer, out *ButtonResponse) { + isTopLevel := in.IsStart() + if in.IsNull() { + if isTopLevel { + in.Consumed() + } + in.Skip() + return + } + in.Delim('{') + for !in.IsDelim('}') { + key := in.UnsafeString() + in.WantColon() + if in.IsNull() { + in.Skip() + in.WantComma() + continue + } + switch key { + case "queryId": + out.QueryID = string(in.String()) + case "text": + out.Text = string(in.String()) + case "showAlert": + out.ShowAlert = bool(in.Bool()) + case "url": + out.URL = string(in.String()) + case "callbackData": + out.CallbackData = string(in.String()) + default: + in.SkipRecursive() + } + in.WantComma() + } + in.Delim('}') + if isTopLevel { + in.Consumed() + } +} +func easyjsonF248ab8EncodeBotGolang(out *jwriter.Writer, in ButtonResponse) { + out.RawByte('{') + first := true + _ = first + { + const prefix string = ",\"queryId\":" + out.RawString(prefix[1:]) + out.String(string(in.QueryID)) + } + { + const prefix string = ",\"text\":" + out.RawString(prefix) + out.String(string(in.Text)) + } + { + const prefix string = ",\"showAlert\":" + out.RawString(prefix) + out.Bool(bool(in.ShowAlert)) + } + { + const prefix string = ",\"url\":" + out.RawString(prefix) + out.String(string(in.URL)) + } + { + const prefix string = ",\"callbackData\":" + out.RawString(prefix) + out.String(string(in.CallbackData)) + } + out.RawByte('}') +} + +// MarshalJSON supports json.Marshaler interface +func (v ButtonResponse) MarshalJSON() ([]byte, error) { + w := jwriter.Writer{} + easyjsonF248ab8EncodeBotGolang(&w, v) + return w.Buffer.BuildBytes(), w.Error +} + +// MarshalEasyJSON supports easyjson.Marshaler interface +func (v ButtonResponse) MarshalEasyJSON(w *jwriter.Writer) { + easyjsonF248ab8EncodeBotGolang(w, v) +} + +// UnmarshalJSON supports json.Unmarshaler interface +func (v *ButtonResponse) UnmarshalJSON(data []byte) error { + r := jlexer.Lexer{Data: data} + easyjsonF248ab8DecodeBotGolang(&r, v) + return r.Error() +} + +// UnmarshalEasyJSON supports easyjson.Unmarshaler interface +func (v *ButtonResponse) UnmarshalEasyJSON(l *jlexer.Lexer) { + easyjsonF248ab8DecodeBotGolang(l, v) +} +func easyjsonF248ab8DecodeBotGolang1(in *jlexer.Lexer, out *Button) { + isTopLevel := in.IsStart() + if in.IsNull() { + if isTopLevel { + in.Consumed() + } + in.Skip() + return + } + in.Delim('{') + for !in.IsDelim('}') { + key := in.UnsafeString() + in.WantColon() + if in.IsNull() { + in.Skip() + in.WantComma() + continue + } + switch key { + case "text": + out.Text = string(in.String()) + case "url": + out.URL = string(in.String()) + case "callbackData": + out.CallbackData = string(in.String()) + default: + in.SkipRecursive() + } + in.WantComma() + } + in.Delim('}') + if isTopLevel { + in.Consumed() + } +} +func easyjsonF248ab8EncodeBotGolang1(out *jwriter.Writer, in Button) { + out.RawByte('{') + first := true + _ = first + { + const prefix string = ",\"text\":" + out.RawString(prefix[1:]) + out.String(string(in.Text)) + } + if in.URL != "" { + const prefix string = ",\"url\":" + out.RawString(prefix) + out.String(string(in.URL)) + } + if in.CallbackData != "" { + const prefix string = ",\"callbackData\":" + out.RawString(prefix) + out.String(string(in.CallbackData)) + } + out.RawByte('}') +} + +// MarshalJSON supports json.Marshaler interface +func (v Button) MarshalJSON() ([]byte, error) { + w := jwriter.Writer{} + easyjsonF248ab8EncodeBotGolang1(&w, v) + return w.Buffer.BuildBytes(), w.Error +} + +// MarshalEasyJSON supports easyjson.Marshaler interface +func (v Button) MarshalEasyJSON(w *jwriter.Writer) { + easyjsonF248ab8EncodeBotGolang1(w, v) +} + +// UnmarshalJSON supports json.Unmarshaler interface +func (v *Button) UnmarshalJSON(data []byte) error { + r := jlexer.Lexer{Data: data} + easyjsonF248ab8DecodeBotGolang1(&r, v) + return r.Error() +} + +// UnmarshalEasyJSON supports easyjson.Unmarshaler interface +func (v *Button) UnmarshalEasyJSON(l *jlexer.Lexer) { + easyjsonF248ab8DecodeBotGolang1(l, v) +} diff --git a/client.go b/client.go index 8a28d06..2f41957 100644 --- a/client.go +++ b/client.go @@ -184,6 +184,15 @@ func (c *Client) SendTextMessage(message *Message) error { params.Set("forwardChatId", message.ForwardChatID) } + if message.InlineKeyboard != nil { + data, err := json.Marshal(message.InlineKeyboard) + if err != nil { + return fmt.Errorf("cannot marshal inline keyboard markup: %s", err) + } + + params.Set("inlineKeyboardMarkup", string(data)) + } + response, err := c.Do("/messages/sendText", params, nil) if err != nil { return fmt.Errorf("error while sending text: %s", err) @@ -364,6 +373,22 @@ func (c *Client) UnpinMessage(message *Message) error { return nil } +func (c *Client) SendAnswerCallbackQuery(answer *ButtonResponse) error { + params := url.Values{ + "queryId": []string{answer.QueryID}, + "text": []string{answer.Text}, + "url": []string{answer.URL}, + "showAlert": []string{strconv.FormatBool(answer.ShowAlert)}, + } + + _, err := c.Do("/messages/answerCallbackQuery", params, nil) + if err != nil { + return fmt.Errorf("error while making request: %s", err) + } + + return nil +} + func NewClient(baseURL string, token string, logger *logrus.Logger) *Client { return &Client{ token: token, diff --git a/message.go b/message.go index f78bc42..3d2be2e 100644 --- a/message.go +++ b/message.go @@ -51,6 +51,9 @@ type Message struct { ForwardChatID string `json:"forwardChatId"` Timestamp int `json:"timestamp"` + + // The markup for the inline keyboard + InlineKeyboard [][]Button `json:"inlineKeyboardMarkup"` } func (m *Message) AttachNewFile(file *os.File) { @@ -73,6 +76,10 @@ func (m *Message) AttachExistingVoice(fileID string) { m.ContentType = Voice } +func (m *Message) AttachInlineKeyboard(keyboard [][]Button) { + m.InlineKeyboard = keyboard +} + // Send method sends your message. // Make sure you have Text or FileID in your message. func (m *Message) Send() error { diff --git a/types.go b/types.go index 25445f9..d4ef3a3 100644 --- a/types.go +++ b/types.go @@ -14,6 +14,7 @@ const ( UNPINNED_MESSAGE EventType = "unpinnedMessage" NEW_CHAT_MEMBERS EventType = "newChatMembers" LEFT_CHAT_MEMBERS EventType = "leftChatMembers" + CALLBACK_QUERY EventType = "callbackQuery" STICKER PartType = "sticker" MENTION PartType = "mention" @@ -86,6 +87,14 @@ type EventPayload struct { // Timestamp of the event. Timestamp int `json:"timestamp"` + // Id of the query. + // Presented only in callbackQuery event. + QueryID string `json:"queryId"` + + // CallbackData of the query (id of button). + // Presented only in callbackQuery event. + CallbackData string `json:"callbackData"` + LeftMembers []Contact `json:"leftMembers"` NewMembers []Contact `json:"newMembers"` @@ -145,3 +154,10 @@ type Part struct { // Payload of the part Payload PartPayload `json:"payload"` } + +func (ep *EventPayload) CallbackQuery() *ButtonResponse { + return &ButtonResponse{ + QueryID: ep.QueryID, + CallbackData: ep.CallbackData, + } +} diff --git a/types_easyjson.go b/types_easyjson.go index 76bcaea..8c2bae5 100644 --- a/types_easyjson.go +++ b/types_easyjson.go @@ -17,7 +17,7 @@ var ( _ easyjson.Marshaler ) -func easyjson6601e8cdDecodeGithubComMailRuImBotGolang(in *jlexer.Lexer, out *eventsResponse) { +func easyjson6601e8cdDecodeBotGolang(in *jlexer.Lexer, out *Part) { isTopLevel := in.IsStart() if in.IsNull() { if isTopLevel { @@ -36,39 +36,10 @@ func easyjson6601e8cdDecodeGithubComMailRuImBotGolang(in *jlexer.Lexer, out *eve continue } switch key { - case "ok": - out.OK = bool(in.Bool()) - case "events": - if in.IsNull() { - in.Skip() - out.Events = nil - } else { - in.Delim('[') - if out.Events == nil { - if !in.IsDelim(']') { - out.Events = make([]*Event, 0, 8) - } else { - out.Events = []*Event{} - } - } else { - out.Events = (out.Events)[:0] - } - for !in.IsDelim(']') { - var v1 *Event - if in.IsNull() { - in.Skip() - v1 = nil - } else { - if v1 == nil { - v1 = new(Event) - } - (*v1).UnmarshalEasyJSON(in) - } - out.Events = append(out.Events, v1) - in.WantComma() - } - in.Delim(']') - } + case "type": + out.Type = PartType(in.String()) + case "payload": + (out.Payload).UnmarshalEasyJSON(in) default: in.SkipRecursive() } @@ -79,62 +50,47 @@ func easyjson6601e8cdDecodeGithubComMailRuImBotGolang(in *jlexer.Lexer, out *eve in.Consumed() } } -func easyjson6601e8cdEncodeGithubComMailRuImBotGolang(out *jwriter.Writer, in eventsResponse) { +func easyjson6601e8cdEncodeBotGolang(out *jwriter.Writer, in Part) { out.RawByte('{') first := true _ = first { - const prefix string = ",\"ok\":" + const prefix string = ",\"type\":" out.RawString(prefix[1:]) - out.Bool(bool(in.OK)) + out.String(string(in.Type)) } { - const prefix string = ",\"events\":" + const prefix string = ",\"payload\":" out.RawString(prefix) - if in.Events == nil && (out.Flags&jwriter.NilSliceAsEmpty) == 0 { - out.RawString("null") - } else { - out.RawByte('[') - for v2, v3 := range in.Events { - if v2 > 0 { - out.RawByte(',') - } - if v3 == nil { - out.RawString("null") - } else { - (*v3).MarshalEasyJSON(out) - } - } - out.RawByte(']') - } + (in.Payload).MarshalEasyJSON(out) } out.RawByte('}') } // MarshalJSON supports json.Marshaler interface -func (v eventsResponse) MarshalJSON() ([]byte, error) { +func (v Part) MarshalJSON() ([]byte, error) { w := jwriter.Writer{} - easyjson6601e8cdEncodeGithubComMailRuImBotGolang(&w, v) + easyjson6601e8cdEncodeBotGolang(&w, v) return w.Buffer.BuildBytes(), w.Error } // MarshalEasyJSON supports easyjson.Marshaler interface -func (v eventsResponse) MarshalEasyJSON(w *jwriter.Writer) { - easyjson6601e8cdEncodeGithubComMailRuImBotGolang(w, v) +func (v Part) MarshalEasyJSON(w *jwriter.Writer) { + easyjson6601e8cdEncodeBotGolang(w, v) } // UnmarshalJSON supports json.Unmarshaler interface -func (v *eventsResponse) UnmarshalJSON(data []byte) error { +func (v *Part) UnmarshalJSON(data []byte) error { r := jlexer.Lexer{Data: data} - easyjson6601e8cdDecodeGithubComMailRuImBotGolang(&r, v) + easyjson6601e8cdDecodeBotGolang(&r, v) return r.Error() } // UnmarshalEasyJSON supports easyjson.Unmarshaler interface -func (v *eventsResponse) UnmarshalEasyJSON(l *jlexer.Lexer) { - easyjson6601e8cdDecodeGithubComMailRuImBotGolang(l, v) +func (v *Part) UnmarshalEasyJSON(l *jlexer.Lexer) { + easyjson6601e8cdDecodeBotGolang(l, v) } -func easyjson6601e8cdDecodeGithubComMailRuImBotGolang1(in *jlexer.Lexer, out *Response) { +func easyjson6601e8cdDecodeBotGolang1(in *jlexer.Lexer, out *Event) { isTopLevel := in.IsStart() if in.IsNull() { if isTopLevel { @@ -153,10 +109,12 @@ func easyjson6601e8cdDecodeGithubComMailRuImBotGolang1(in *jlexer.Lexer, out *Re continue } switch key { - case "ok": - out.OK = bool(in.Bool()) - case "description": - out.Description = string(in.String()) + case "eventId": + out.EventID = int(in.Int()) + case "type": + out.Type = EventType(in.String()) + case "payload": + (out.Payload).UnmarshalEasyJSON(in) default: in.SkipRecursive() } @@ -167,113 +125,52 @@ func easyjson6601e8cdDecodeGithubComMailRuImBotGolang1(in *jlexer.Lexer, out *Re in.Consumed() } } -func easyjson6601e8cdEncodeGithubComMailRuImBotGolang1(out *jwriter.Writer, in Response) { +func easyjson6601e8cdEncodeBotGolang1(out *jwriter.Writer, in Event) { out.RawByte('{') first := true _ = first { - const prefix string = ",\"ok\":" + const prefix string = ",\"eventId\":" out.RawString(prefix[1:]) - out.Bool(bool(in.OK)) + out.Int(int(in.EventID)) } - if in.Description != "" { - const prefix string = ",\"description\":" + { + const prefix string = ",\"type\":" out.RawString(prefix) - out.String(string(in.Description)) - } - out.RawByte('}') -} - -// MarshalJSON supports json.Marshaler interface -func (v Response) MarshalJSON() ([]byte, error) { - w := jwriter.Writer{} - easyjson6601e8cdEncodeGithubComMailRuImBotGolang1(&w, v) - return w.Buffer.BuildBytes(), w.Error -} - -// MarshalEasyJSON supports easyjson.Marshaler interface -func (v Response) MarshalEasyJSON(w *jwriter.Writer) { - easyjson6601e8cdEncodeGithubComMailRuImBotGolang1(w, v) -} - -// UnmarshalJSON supports json.Unmarshaler interface -func (v *Response) UnmarshalJSON(data []byte) error { - r := jlexer.Lexer{Data: data} - easyjson6601e8cdDecodeGithubComMailRuImBotGolang1(&r, v) - return r.Error() -} - -// UnmarshalEasyJSON supports easyjson.Unmarshaler interface -func (v *Response) UnmarshalEasyJSON(l *jlexer.Lexer) { - easyjson6601e8cdDecodeGithubComMailRuImBotGolang1(l, v) -} -func easyjson6601e8cdDecodeGithubComMailRuImBotGolang2(in *jlexer.Lexer, out *Photo) { - isTopLevel := in.IsStart() - if in.IsNull() { - if isTopLevel { - in.Consumed() - } - in.Skip() - return - } - in.Delim('{') - for !in.IsDelim('}') { - key := in.UnsafeString() - in.WantColon() - if in.IsNull() { - in.Skip() - in.WantComma() - continue - } - switch key { - case "url": - out.URL = string(in.String()) - default: - in.SkipRecursive() - } - in.WantComma() - } - in.Delim('}') - if isTopLevel { - in.Consumed() + out.String(string(in.Type)) } -} -func easyjson6601e8cdEncodeGithubComMailRuImBotGolang2(out *jwriter.Writer, in Photo) { - out.RawByte('{') - first := true - _ = first { - const prefix string = ",\"url\":" - out.RawString(prefix[1:]) - out.String(string(in.URL)) + const prefix string = ",\"payload\":" + out.RawString(prefix) + (in.Payload).MarshalEasyJSON(out) } out.RawByte('}') } // MarshalJSON supports json.Marshaler interface -func (v Photo) MarshalJSON() ([]byte, error) { +func (v Event) MarshalJSON() ([]byte, error) { w := jwriter.Writer{} - easyjson6601e8cdEncodeGithubComMailRuImBotGolang2(&w, v) + easyjson6601e8cdEncodeBotGolang1(&w, v) return w.Buffer.BuildBytes(), w.Error } // MarshalEasyJSON supports easyjson.Marshaler interface -func (v Photo) MarshalEasyJSON(w *jwriter.Writer) { - easyjson6601e8cdEncodeGithubComMailRuImBotGolang2(w, v) +func (v Event) MarshalEasyJSON(w *jwriter.Writer) { + easyjson6601e8cdEncodeBotGolang1(w, v) } // UnmarshalJSON supports json.Unmarshaler interface -func (v *Photo) UnmarshalJSON(data []byte) error { +func (v *Event) UnmarshalJSON(data []byte) error { r := jlexer.Lexer{Data: data} - easyjson6601e8cdDecodeGithubComMailRuImBotGolang2(&r, v) + easyjson6601e8cdDecodeBotGolang1(&r, v) return r.Error() } // UnmarshalEasyJSON supports easyjson.Unmarshaler interface -func (v *Photo) UnmarshalEasyJSON(l *jlexer.Lexer) { - easyjson6601e8cdDecodeGithubComMailRuImBotGolang2(l, v) +func (v *Event) UnmarshalEasyJSON(l *jlexer.Lexer) { + easyjson6601e8cdDecodeBotGolang1(l, v) } -func easyjson6601e8cdDecodeGithubComMailRuImBotGolang3(in *jlexer.Lexer, out *PartPayload) { +func easyjson6601e8cdDecodeBotGolang2(in *jlexer.Lexer, out *PartPayload) { isTopLevel := in.IsStart() if in.IsNull() { if isTopLevel { @@ -316,7 +213,7 @@ func easyjson6601e8cdDecodeGithubComMailRuImBotGolang3(in *jlexer.Lexer, out *Pa in.Consumed() } } -func easyjson6601e8cdEncodeGithubComMailRuImBotGolang3(out *jwriter.Writer, in PartPayload) { +func easyjson6601e8cdEncodeBotGolang2(out *jwriter.Writer, in PartPayload) { out.RawByte('{') first := true _ = first @@ -361,27 +258,27 @@ func easyjson6601e8cdEncodeGithubComMailRuImBotGolang3(out *jwriter.Writer, in P // MarshalJSON supports json.Marshaler interface func (v PartPayload) MarshalJSON() ([]byte, error) { w := jwriter.Writer{} - easyjson6601e8cdEncodeGithubComMailRuImBotGolang3(&w, v) + easyjson6601e8cdEncodeBotGolang2(&w, v) return w.Buffer.BuildBytes(), w.Error } // MarshalEasyJSON supports easyjson.Marshaler interface func (v PartPayload) MarshalEasyJSON(w *jwriter.Writer) { - easyjson6601e8cdEncodeGithubComMailRuImBotGolang3(w, v) + easyjson6601e8cdEncodeBotGolang2(w, v) } // UnmarshalJSON supports json.Unmarshaler interface func (v *PartPayload) UnmarshalJSON(data []byte) error { r := jlexer.Lexer{Data: data} - easyjson6601e8cdDecodeGithubComMailRuImBotGolang3(&r, v) + easyjson6601e8cdDecodeBotGolang2(&r, v) return r.Error() } // UnmarshalEasyJSON supports easyjson.Unmarshaler interface func (v *PartPayload) UnmarshalEasyJSON(l *jlexer.Lexer) { - easyjson6601e8cdDecodeGithubComMailRuImBotGolang3(l, v) + easyjson6601e8cdDecodeBotGolang2(l, v) } -func easyjson6601e8cdDecodeGithubComMailRuImBotGolang4(in *jlexer.Lexer, out *PartMessage) { +func easyjson6601e8cdDecodeBotGolang3(in *jlexer.Lexer, out *PartMessage) { isTopLevel := in.IsStart() if in.IsNull() { if isTopLevel { @@ -418,7 +315,7 @@ func easyjson6601e8cdDecodeGithubComMailRuImBotGolang4(in *jlexer.Lexer, out *Pa in.Consumed() } } -func easyjson6601e8cdEncodeGithubComMailRuImBotGolang4(out *jwriter.Writer, in PartMessage) { +func easyjson6601e8cdEncodeBotGolang3(out *jwriter.Writer, in PartMessage) { out.RawByte('{') first := true _ = first @@ -448,100 +345,27 @@ func easyjson6601e8cdEncodeGithubComMailRuImBotGolang4(out *jwriter.Writer, in P // MarshalJSON supports json.Marshaler interface func (v PartMessage) MarshalJSON() ([]byte, error) { w := jwriter.Writer{} - easyjson6601e8cdEncodeGithubComMailRuImBotGolang4(&w, v) + easyjson6601e8cdEncodeBotGolang3(&w, v) return w.Buffer.BuildBytes(), w.Error } // MarshalEasyJSON supports easyjson.Marshaler interface func (v PartMessage) MarshalEasyJSON(w *jwriter.Writer) { - easyjson6601e8cdEncodeGithubComMailRuImBotGolang4(w, v) + easyjson6601e8cdEncodeBotGolang3(w, v) } // UnmarshalJSON supports json.Unmarshaler interface func (v *PartMessage) UnmarshalJSON(data []byte) error { r := jlexer.Lexer{Data: data} - easyjson6601e8cdDecodeGithubComMailRuImBotGolang4(&r, v) + easyjson6601e8cdDecodeBotGolang3(&r, v) return r.Error() } // UnmarshalEasyJSON supports easyjson.Unmarshaler interface func (v *PartMessage) UnmarshalEasyJSON(l *jlexer.Lexer) { - easyjson6601e8cdDecodeGithubComMailRuImBotGolang4(l, v) + easyjson6601e8cdDecodeBotGolang3(l, v) } -func easyjson6601e8cdDecodeGithubComMailRuImBotGolang5(in *jlexer.Lexer, out *Part) { - isTopLevel := in.IsStart() - if in.IsNull() { - if isTopLevel { - in.Consumed() - } - in.Skip() - return - } - in.Delim('{') - for !in.IsDelim('}') { - key := in.UnsafeString() - in.WantColon() - if in.IsNull() { - in.Skip() - in.WantComma() - continue - } - switch key { - case "type": - out.Type = PartType(in.String()) - case "payload": - (out.Payload).UnmarshalEasyJSON(in) - default: - in.SkipRecursive() - } - in.WantComma() - } - in.Delim('}') - if isTopLevel { - in.Consumed() - } -} -func easyjson6601e8cdEncodeGithubComMailRuImBotGolang5(out *jwriter.Writer, in Part) { - out.RawByte('{') - first := true - _ = first - { - const prefix string = ",\"type\":" - out.RawString(prefix[1:]) - out.String(string(in.Type)) - } - { - const prefix string = ",\"payload\":" - out.RawString(prefix) - (in.Payload).MarshalEasyJSON(out) - } - out.RawByte('}') -} - -// MarshalJSON supports json.Marshaler interface -func (v Part) MarshalJSON() ([]byte, error) { - w := jwriter.Writer{} - easyjson6601e8cdEncodeGithubComMailRuImBotGolang5(&w, v) - return w.Buffer.BuildBytes(), w.Error -} - -// MarshalEasyJSON supports easyjson.Marshaler interface -func (v Part) MarshalEasyJSON(w *jwriter.Writer) { - easyjson6601e8cdEncodeGithubComMailRuImBotGolang5(w, v) -} - -// UnmarshalJSON supports json.Unmarshaler interface -func (v *Part) UnmarshalJSON(data []byte) error { - r := jlexer.Lexer{Data: data} - easyjson6601e8cdDecodeGithubComMailRuImBotGolang5(&r, v) - return r.Error() -} - -// UnmarshalEasyJSON supports easyjson.Unmarshaler interface -func (v *Part) UnmarshalEasyJSON(l *jlexer.Lexer) { - easyjson6601e8cdDecodeGithubComMailRuImBotGolang5(l, v) -} -func easyjson6601e8cdDecodeGithubComMailRuImBotGolang6(in *jlexer.Lexer, out *EventPayload) { +func easyjson6601e8cdDecodeBotGolang4(in *jlexer.Lexer, out *EventPayload) { isTopLevel := in.IsStart() if in.IsNull() { if isTopLevel { @@ -576,7 +400,7 @@ func easyjson6601e8cdDecodeGithubComMailRuImBotGolang6(in *jlexer.Lexer, out *Ev in.Delim('[') if out.Parts == nil { if !in.IsDelim(']') { - out.Parts = make([]Part, 0, 1) + out.Parts = make([]Part, 0, 0) } else { out.Parts = []Part{} } @@ -584,9 +408,9 @@ func easyjson6601e8cdDecodeGithubComMailRuImBotGolang6(in *jlexer.Lexer, out *Ev out.Parts = (out.Parts)[:0] } for !in.IsDelim(']') { - var v4 Part - (v4).UnmarshalEasyJSON(in) - out.Parts = append(out.Parts, v4) + var v1 Part + (v1).UnmarshalEasyJSON(in) + out.Parts = append(out.Parts, v1) in.WantComma() } in.Delim(']') @@ -609,9 +433,9 @@ func easyjson6601e8cdDecodeGithubComMailRuImBotGolang6(in *jlexer.Lexer, out *Ev out.LeftMembers = (out.LeftMembers)[:0] } for !in.IsDelim(']') { - var v5 Contact - (v5).UnmarshalEasyJSON(in) - out.LeftMembers = append(out.LeftMembers, v5) + var v2 Contact + (v2).UnmarshalEasyJSON(in) + out.LeftMembers = append(out.LeftMembers, v2) in.WantComma() } in.Delim(']') @@ -632,9 +456,9 @@ func easyjson6601e8cdDecodeGithubComMailRuImBotGolang6(in *jlexer.Lexer, out *Ev out.NewMembers = (out.NewMembers)[:0] } for !in.IsDelim(']') { - var v6 Contact - (v6).UnmarshalEasyJSON(in) - out.NewMembers = append(out.NewMembers, v6) + var v3 Contact + (v3).UnmarshalEasyJSON(in) + out.NewMembers = append(out.NewMembers, v3) in.WantComma() } in.Delim(']') @@ -643,6 +467,10 @@ func easyjson6601e8cdDecodeGithubComMailRuImBotGolang6(in *jlexer.Lexer, out *Ev (out.AddedBy).UnmarshalEasyJSON(in) case "removedBy": (out.RemovedBy).UnmarshalEasyJSON(in) + case "queryId": + out.QueryID = string(in.String()) + case "callbackData": + out.CallbackData = string(in.String()) default: in.SkipRecursive() } @@ -653,7 +481,7 @@ func easyjson6601e8cdDecodeGithubComMailRuImBotGolang6(in *jlexer.Lexer, out *Ev in.Consumed() } } -func easyjson6601e8cdEncodeGithubComMailRuImBotGolang6(out *jwriter.Writer, in EventPayload) { +func easyjson6601e8cdEncodeBotGolang4(out *jwriter.Writer, in EventPayload) { out.RawByte('{') first := true _ = first @@ -684,11 +512,11 @@ func easyjson6601e8cdEncodeGithubComMailRuImBotGolang6(out *jwriter.Writer, in E out.RawString("null") } else { out.RawByte('[') - for v7, v8 := range in.Parts { - if v7 > 0 { + for v4, v5 := range in.Parts { + if v4 > 0 { out.RawByte(',') } - (v8).MarshalEasyJSON(out) + (v5).MarshalEasyJSON(out) } out.RawByte(']') } @@ -705,11 +533,11 @@ func easyjson6601e8cdEncodeGithubComMailRuImBotGolang6(out *jwriter.Writer, in E out.RawString("null") } else { out.RawByte('[') - for v9, v10 := range in.LeftMembers { - if v9 > 0 { + for v6, v7 := range in.LeftMembers { + if v6 > 0 { out.RawByte(',') } - (v10).MarshalEasyJSON(out) + (v7).MarshalEasyJSON(out) } out.RawByte(']') } @@ -721,11 +549,11 @@ func easyjson6601e8cdEncodeGithubComMailRuImBotGolang6(out *jwriter.Writer, in E out.RawString("null") } else { out.RawByte('[') - for v11, v12 := range in.NewMembers { - if v11 > 0 { + for v8, v9 := range in.NewMembers { + if v8 > 0 { out.RawByte(',') } - (v12).MarshalEasyJSON(out) + (v9).MarshalEasyJSON(out) } out.RawByte(']') } @@ -740,33 +568,43 @@ func easyjson6601e8cdEncodeGithubComMailRuImBotGolang6(out *jwriter.Writer, in E out.RawString(prefix) (in.RemovedBy).MarshalEasyJSON(out) } + { + const prefix string = ",\"queryId\":" + out.RawString(prefix) + out.String(string(in.QueryID)) + } + { + const prefix string = ",\"callbackData\":" + out.RawString(prefix) + out.String(string(in.CallbackData)) + } out.RawByte('}') } // MarshalJSON supports json.Marshaler interface func (v EventPayload) MarshalJSON() ([]byte, error) { w := jwriter.Writer{} - easyjson6601e8cdEncodeGithubComMailRuImBotGolang6(&w, v) + easyjson6601e8cdEncodeBotGolang4(&w, v) return w.Buffer.BuildBytes(), w.Error } // MarshalEasyJSON supports easyjson.Marshaler interface func (v EventPayload) MarshalEasyJSON(w *jwriter.Writer) { - easyjson6601e8cdEncodeGithubComMailRuImBotGolang6(w, v) + easyjson6601e8cdEncodeBotGolang4(w, v) } // UnmarshalJSON supports json.Unmarshaler interface func (v *EventPayload) UnmarshalJSON(data []byte) error { r := jlexer.Lexer{Data: data} - easyjson6601e8cdDecodeGithubComMailRuImBotGolang6(&r, v) + easyjson6601e8cdDecodeBotGolang4(&r, v) return r.Error() } // UnmarshalEasyJSON supports easyjson.Unmarshaler interface func (v *EventPayload) UnmarshalEasyJSON(l *jlexer.Lexer) { - easyjson6601e8cdDecodeGithubComMailRuImBotGolang6(l, v) + easyjson6601e8cdDecodeBotGolang4(l, v) } -func easyjson6601e8cdDecodeGithubComMailRuImBotGolang7(in *jlexer.Lexer, out *Event) { +func easyjson6601e8cdDecodeBotGolang5(in *jlexer.Lexer, out *Contact) { isTopLevel := in.IsStart() if in.IsNull() { if isTopLevel { @@ -785,12 +623,12 @@ func easyjson6601e8cdDecodeGithubComMailRuImBotGolang7(in *jlexer.Lexer, out *Ev continue } switch key { - case "eventId": - out.EventID = int(in.Int()) - case "type": - out.Type = EventType(in.String()) - case "payload": - (out.Payload).UnmarshalEasyJSON(in) + case "userId": + out.UserID = string(in.String()) + case "firstName": + out.FirstName = string(in.String()) + case "lastName": + out.LastName = string(in.String()) default: in.SkipRecursive() } @@ -801,52 +639,52 @@ func easyjson6601e8cdDecodeGithubComMailRuImBotGolang7(in *jlexer.Lexer, out *Ev in.Consumed() } } -func easyjson6601e8cdEncodeGithubComMailRuImBotGolang7(out *jwriter.Writer, in Event) { +func easyjson6601e8cdEncodeBotGolang5(out *jwriter.Writer, in Contact) { out.RawByte('{') first := true _ = first { - const prefix string = ",\"eventId\":" + const prefix string = ",\"userId\":" out.RawString(prefix[1:]) - out.Int(int(in.EventID)) + out.String(string(in.UserID)) } { - const prefix string = ",\"type\":" + const prefix string = ",\"firstName\":" out.RawString(prefix) - out.String(string(in.Type)) + out.String(string(in.FirstName)) } { - const prefix string = ",\"payload\":" + const prefix string = ",\"lastName\":" out.RawString(prefix) - (in.Payload).MarshalEasyJSON(out) + out.String(string(in.LastName)) } out.RawByte('}') } // MarshalJSON supports json.Marshaler interface -func (v Event) MarshalJSON() ([]byte, error) { +func (v Contact) MarshalJSON() ([]byte, error) { w := jwriter.Writer{} - easyjson6601e8cdEncodeGithubComMailRuImBotGolang7(&w, v) + easyjson6601e8cdEncodeBotGolang5(&w, v) return w.Buffer.BuildBytes(), w.Error } // MarshalEasyJSON supports easyjson.Marshaler interface -func (v Event) MarshalEasyJSON(w *jwriter.Writer) { - easyjson6601e8cdEncodeGithubComMailRuImBotGolang7(w, v) +func (v Contact) MarshalEasyJSON(w *jwriter.Writer) { + easyjson6601e8cdEncodeBotGolang5(w, v) } // UnmarshalJSON supports json.Unmarshaler interface -func (v *Event) UnmarshalJSON(data []byte) error { +func (v *Contact) UnmarshalJSON(data []byte) error { r := jlexer.Lexer{Data: data} - easyjson6601e8cdDecodeGithubComMailRuImBotGolang7(&r, v) + easyjson6601e8cdDecodeBotGolang5(&r, v) return r.Error() } // UnmarshalEasyJSON supports easyjson.Unmarshaler interface -func (v *Event) UnmarshalEasyJSON(l *jlexer.Lexer) { - easyjson6601e8cdDecodeGithubComMailRuImBotGolang7(l, v) +func (v *Contact) UnmarshalEasyJSON(l *jlexer.Lexer) { + easyjson6601e8cdDecodeBotGolang5(l, v) } -func easyjson6601e8cdDecodeGithubComMailRuImBotGolang8(in *jlexer.Lexer, out *Contact) { +func easyjson6601e8cdDecodeBotGolang6(in *jlexer.Lexer, out *eventsResponse) { isTopLevel := in.IsStart() if in.IsNull() { if isTopLevel { @@ -865,12 +703,39 @@ func easyjson6601e8cdDecodeGithubComMailRuImBotGolang8(in *jlexer.Lexer, out *Co continue } switch key { - case "userId": - out.UserID = string(in.String()) - case "firstName": - out.FirstName = string(in.String()) - case "lastName": - out.LastName = string(in.String()) + case "ok": + out.OK = bool(in.Bool()) + case "events": + if in.IsNull() { + in.Skip() + out.Events = nil + } else { + in.Delim('[') + if out.Events == nil { + if !in.IsDelim(']') { + out.Events = make([]*Event, 0, 8) + } else { + out.Events = []*Event{} + } + } else { + out.Events = (out.Events)[:0] + } + for !in.IsDelim(']') { + var v10 *Event + if in.IsNull() { + in.Skip() + v10 = nil + } else { + if v10 == nil { + v10 = new(Event) + } + (*v10).UnmarshalEasyJSON(in) + } + out.Events = append(out.Events, v10) + in.WantComma() + } + in.Delim(']') + } default: in.SkipRecursive() } @@ -881,52 +746,62 @@ func easyjson6601e8cdDecodeGithubComMailRuImBotGolang8(in *jlexer.Lexer, out *Co in.Consumed() } } -func easyjson6601e8cdEncodeGithubComMailRuImBotGolang8(out *jwriter.Writer, in Contact) { +func easyjson6601e8cdEncodeBotGolang6(out *jwriter.Writer, in eventsResponse) { out.RawByte('{') first := true _ = first { - const prefix string = ",\"userId\":" + const prefix string = ",\"ok\":" out.RawString(prefix[1:]) - out.String(string(in.UserID)) - } - { - const prefix string = ",\"firstName\":" - out.RawString(prefix) - out.String(string(in.FirstName)) + out.Bool(bool(in.OK)) } { - const prefix string = ",\"lastName\":" + const prefix string = ",\"events\":" out.RawString(prefix) - out.String(string(in.LastName)) + if in.Events == nil && (out.Flags&jwriter.NilSliceAsEmpty) == 0 { + out.RawString("null") + } else { + out.RawByte('[') + for v11, v12 := range in.Events { + if v11 > 0 { + out.RawByte(',') + } + if v12 == nil { + out.RawString("null") + } else { + (*v12).MarshalEasyJSON(out) + } + } + out.RawByte(']') + } } out.RawByte('}') } // MarshalJSON supports json.Marshaler interface -func (v Contact) MarshalJSON() ([]byte, error) { +func (v eventsResponse) MarshalJSON() ([]byte, error) { w := jwriter.Writer{} - easyjson6601e8cdEncodeGithubComMailRuImBotGolang8(&w, v) + easyjson6601e8cdEncodeBotGolang6(&w, v) return w.Buffer.BuildBytes(), w.Error } // MarshalEasyJSON supports easyjson.Marshaler interface -func (v Contact) MarshalEasyJSON(w *jwriter.Writer) { - easyjson6601e8cdEncodeGithubComMailRuImBotGolang8(w, v) +func (v eventsResponse) MarshalEasyJSON(w *jwriter.Writer) { + easyjson6601e8cdEncodeBotGolang6(w, v) } // UnmarshalJSON supports json.Unmarshaler interface -func (v *Contact) UnmarshalJSON(data []byte) error { +func (v *eventsResponse) UnmarshalJSON(data []byte) error { r := jlexer.Lexer{Data: data} - easyjson6601e8cdDecodeGithubComMailRuImBotGolang8(&r, v) + easyjson6601e8cdDecodeBotGolang6(&r, v) return r.Error() } // UnmarshalEasyJSON supports easyjson.Unmarshaler interface -func (v *Contact) UnmarshalEasyJSON(l *jlexer.Lexer) { - easyjson6601e8cdDecodeGithubComMailRuImBotGolang8(l, v) +func (v *eventsResponse) UnmarshalEasyJSON(l *jlexer.Lexer) { + easyjson6601e8cdDecodeBotGolang6(l, v) } -func easyjson6601e8cdDecodeGithubComMailRuImBotGolang9(in *jlexer.Lexer, out *BotInfo) { +func easyjson6601e8cdDecodeBotGolang7(in *jlexer.Lexer, out *BotInfo) { isTopLevel := in.IsStart() if in.IsNull() { if isTopLevel { @@ -986,7 +861,7 @@ func easyjson6601e8cdDecodeGithubComMailRuImBotGolang9(in *jlexer.Lexer, out *Bo in.Consumed() } } -func easyjson6601e8cdEncodeGithubComMailRuImBotGolang9(out *jwriter.Writer, in BotInfo) { +func easyjson6601e8cdEncodeBotGolang7(out *jwriter.Writer, in BotInfo) { out.RawByte('{') first := true _ = first @@ -1032,23 +907,162 @@ func easyjson6601e8cdEncodeGithubComMailRuImBotGolang9(out *jwriter.Writer, in B // MarshalJSON supports json.Marshaler interface func (v BotInfo) MarshalJSON() ([]byte, error) { w := jwriter.Writer{} - easyjson6601e8cdEncodeGithubComMailRuImBotGolang9(&w, v) + easyjson6601e8cdEncodeBotGolang7(&w, v) return w.Buffer.BuildBytes(), w.Error } // MarshalEasyJSON supports easyjson.Marshaler interface func (v BotInfo) MarshalEasyJSON(w *jwriter.Writer) { - easyjson6601e8cdEncodeGithubComMailRuImBotGolang9(w, v) + easyjson6601e8cdEncodeBotGolang7(w, v) } // UnmarshalJSON supports json.Unmarshaler interface func (v *BotInfo) UnmarshalJSON(data []byte) error { r := jlexer.Lexer{Data: data} - easyjson6601e8cdDecodeGithubComMailRuImBotGolang9(&r, v) + easyjson6601e8cdDecodeBotGolang7(&r, v) return r.Error() } // UnmarshalEasyJSON supports easyjson.Unmarshaler interface func (v *BotInfo) UnmarshalEasyJSON(l *jlexer.Lexer) { - easyjson6601e8cdDecodeGithubComMailRuImBotGolang9(l, v) + easyjson6601e8cdDecodeBotGolang7(l, v) +} +func easyjson6601e8cdDecodeBotGolang8(in *jlexer.Lexer, out *Photo) { + isTopLevel := in.IsStart() + if in.IsNull() { + if isTopLevel { + in.Consumed() + } + in.Skip() + return + } + in.Delim('{') + for !in.IsDelim('}') { + key := in.UnsafeString() + in.WantColon() + if in.IsNull() { + in.Skip() + in.WantComma() + continue + } + switch key { + case "url": + out.URL = string(in.String()) + default: + in.SkipRecursive() + } + in.WantComma() + } + in.Delim('}') + if isTopLevel { + in.Consumed() + } +} +func easyjson6601e8cdEncodeBotGolang8(out *jwriter.Writer, in Photo) { + out.RawByte('{') + first := true + _ = first + { + const prefix string = ",\"url\":" + out.RawString(prefix[1:]) + out.String(string(in.URL)) + } + out.RawByte('}') +} + +// MarshalJSON supports json.Marshaler interface +func (v Photo) MarshalJSON() ([]byte, error) { + w := jwriter.Writer{} + easyjson6601e8cdEncodeBotGolang8(&w, v) + return w.Buffer.BuildBytes(), w.Error +} + +// MarshalEasyJSON supports easyjson.Marshaler interface +func (v Photo) MarshalEasyJSON(w *jwriter.Writer) { + easyjson6601e8cdEncodeBotGolang8(w, v) +} + +// UnmarshalJSON supports json.Unmarshaler interface +func (v *Photo) UnmarshalJSON(data []byte) error { + r := jlexer.Lexer{Data: data} + easyjson6601e8cdDecodeBotGolang8(&r, v) + return r.Error() +} + +// UnmarshalEasyJSON supports easyjson.Unmarshaler interface +func (v *Photo) UnmarshalEasyJSON(l *jlexer.Lexer) { + easyjson6601e8cdDecodeBotGolang8(l, v) +} +func easyjson6601e8cdDecodeBotGolang9(in *jlexer.Lexer, out *Response) { + isTopLevel := in.IsStart() + if in.IsNull() { + if isTopLevel { + in.Consumed() + } + in.Skip() + return + } + in.Delim('{') + for !in.IsDelim('}') { + key := in.UnsafeString() + in.WantColon() + if in.IsNull() { + in.Skip() + in.WantComma() + continue + } + switch key { + case "ok": + out.OK = bool(in.Bool()) + case "description": + out.Description = string(in.String()) + default: + in.SkipRecursive() + } + in.WantComma() + } + in.Delim('}') + if isTopLevel { + in.Consumed() + } +} +func easyjson6601e8cdEncodeBotGolang9(out *jwriter.Writer, in Response) { + out.RawByte('{') + first := true + _ = first + { + const prefix string = ",\"ok\":" + out.RawString(prefix[1:]) + out.Bool(bool(in.OK)) + } + if in.Description != "" { + const prefix string = ",\"description\":" + out.RawString(prefix) + out.String(string(in.Description)) + } + out.RawByte('}') +} + +// MarshalJSON supports json.Marshaler interface +func (v Response) MarshalJSON() ([]byte, error) { + w := jwriter.Writer{} + easyjson6601e8cdEncodeBotGolang9(&w, v) + return w.Buffer.BuildBytes(), w.Error +} + +// MarshalEasyJSON supports easyjson.Marshaler interface +func (v Response) MarshalEasyJSON(w *jwriter.Writer) { + easyjson6601e8cdEncodeBotGolang9(w, v) +} + +// UnmarshalJSON supports json.Unmarshaler interface +func (v *Response) UnmarshalJSON(data []byte) error { + r := jlexer.Lexer{Data: data} + easyjson6601e8cdDecodeBotGolang9(&r, v) + return r.Error() +} + +// UnmarshalEasyJSON supports easyjson.Unmarshaler interface +func (v *Response) UnmarshalEasyJSON(l *jlexer.Lexer) { + easyjson6601e8cdDecodeBotGolang9(l, v) } From 4c9ac16ec9a61683ae77c1fcc558a204fcda1916 Mon Sep 17 00:00:00 2001 From: bulletmys Date: Mon, 13 Apr 2020 01:15:52 +0300 Subject: [PATCH 3/6] [UPD] tests and example --- api_mock.go | 12 ++++++++++++ client_test.go | 12 ++++++++++++ example/main.go | 17 +++++++++++++++-- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/api_mock.go b/api_mock.go index 9461908..768d7b5 100644 --- a/api_mock.go +++ b/api_mock.go @@ -221,6 +221,18 @@ func (h *MockHandler) GetEvents(w http.ResponseWriter) { "lastName": "SurName" } } + }, + { + "eventId": 8, + "payload": { + "callbackData": "echo", + "from": { + "firstName": "Name", + "userId": "1234567890" + }, + "queryId": "SVR:123456" + }, + "type": "callbackQuery" } ] } diff --git a/client_test.go b/client_test.go index 4340c5f..4dbf99a 100644 --- a/client_test.go +++ b/client_test.go @@ -233,6 +233,18 @@ func TestClient_GetEvents_OK(t *testing.T) { }, }, }, + { + EventID: 8, + Type: CALLBACK_QUERY, + Payload: EventPayload{ + CallbackData: "echo", + From: Contact{ + UserID: "1234567890", + FirstName: "Name", + }, + QueryID: "SVR:123456", + }, + }, } client := Client{ diff --git a/example/main.go b/example/main.go index 1a25553..69eda11 100644 --- a/example/main.go +++ b/example/main.go @@ -54,7 +54,7 @@ func main() { log.Println(err) } - // Simple 30-seconds echo bot + // Simple 30-seconds echo bot with buttons ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() updates := bot.GetUpdatesChannel(ctx) @@ -63,6 +63,11 @@ func main() { switch update.Type { case botgolang.NEW_MESSAGE: message := update.Payload.Message() + + helloBtn := botgolang.NewCallbackButton("Hello", "echo") + goBtn := botgolang.NewURLButton("go", "https://golang.org/") + message.AttachInlineKeyboard([][]botgolang.Button{{helloBtn, goBtn}}) + if err := message.Send(); err != nil { log.Printf("failed to send message: %s", err) } @@ -71,7 +76,15 @@ func main() { if err := message.Reply("do not edit!"); err != nil { log.Printf("failed to reply to message: %s", err) } - + case botgolang.CALLBACK_QUERY: + data := update.Payload.CallbackQuery() + switch data.CallbackData { + case "echo": + response := bot.NewButtonResponse(data.QueryID, "", "Hello World!", false) + if err := response.Send(); err != nil { + log.Printf("failed to reply on button click: %s", err) + } + } } } From 001a44ce4cc5c1b42cc4457950e83142ad056fb6 Mon Sep 17 00:00:00 2001 From: bulletmys Date: Mon, 13 Apr 2020 01:34:17 +0300 Subject: [PATCH 4/6] [ADD] inlineKeyboardMarkup field to some methods --- client.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/client.go b/client.go index 2f41957..1d63743 100644 --- a/client.go +++ b/client.go @@ -211,6 +211,16 @@ func (c *Client) EditMessage(message *Message) error { "chatId": []string{message.Chat.ID}, "text": []string{message.Text}, } + + if message.InlineKeyboard != nil { + data, err := json.Marshal(message.InlineKeyboard) + if err != nil { + return fmt.Errorf("cannot marshal inline keyboard markup: %s", err) + } + + params.Set("inlineKeyboardMarkup", string(data)) + } + response, err := c.Do("/messages/editText", params, nil) if err != nil { return fmt.Errorf("error while editing text: %s", err) @@ -252,6 +262,15 @@ func (c *Client) SendFileMessage(message *Message) error { params.Set("forwardChatId", message.ForwardChatID) } + if message.InlineKeyboard != nil { + data, err := json.Marshal(message.InlineKeyboard) + if err != nil { + return fmt.Errorf("cannot marshal inline keyboard markup: %s", err) + } + + params.Set("inlineKeyboardMarkup", string(data)) + } + response, err := c.Do("/messages/sendFile", params, nil) if err != nil { return fmt.Errorf("error while making request: %s", err) @@ -280,6 +299,15 @@ func (c *Client) SendVoiceMessage(message *Message) error { params.Set("forwardChatId", message.ForwardChatID) } + if message.InlineKeyboard != nil { + data, err := json.Marshal(message.InlineKeyboard) + if err != nil { + return fmt.Errorf("cannot marshal inline keyboard markup: %s", err) + } + + params.Set("inlineKeyboardMarkup", string(data)) + } + response, err := c.Do("/messages/sendVoice", params, nil) if err != nil { return fmt.Errorf("error while making request: %s", err) @@ -298,6 +326,15 @@ func (c *Client) UploadFile(message *Message) error { "caption": {message.Text}, } + if message.InlineKeyboard != nil { + data, err := json.Marshal(message.InlineKeyboard) + if err != nil { + return fmt.Errorf("cannot marshal inline keyboard markup: %s", err) + } + + params.Set("inlineKeyboardMarkup", string(data)) + } + response, err := c.Do("/messages/sendFile", params, message.File) if err != nil { return fmt.Errorf("error while making request: %s", err) @@ -316,6 +353,15 @@ func (c *Client) UploadVoice(message *Message) error { "caption": {message.Text}, } + if message.InlineKeyboard != nil { + data, err := json.Marshal(message.InlineKeyboard) + if err != nil { + return fmt.Errorf("cannot marshal inline keyboard markup: %s", err) + } + + params.Set("inlineKeyboardMarkup", string(data)) + } + response, err := c.Do("/messages/sendVoice", params, message.File) if err != nil { return fmt.Errorf("error while making request: %s", err) From e70fc2c7066692367a64a98c4b625d39ff4fe885 Mon Sep 17 00:00:00 2001 From: bulletmys Date: Mon, 13 Apr 2020 16:43:24 +0300 Subject: [PATCH 5/6] [UPD] deleted []string --- client.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/client.go b/client.go index 1d63743..f20fe9b 100644 --- a/client.go +++ b/client.go @@ -421,10 +421,10 @@ func (c *Client) UnpinMessage(message *Message) error { func (c *Client) SendAnswerCallbackQuery(answer *ButtonResponse) error { params := url.Values{ - "queryId": []string{answer.QueryID}, - "text": []string{answer.Text}, - "url": []string{answer.URL}, - "showAlert": []string{strconv.FormatBool(answer.ShowAlert)}, + "queryId": {answer.QueryID}, + "text": {answer.Text}, + "url": {answer.URL}, + "showAlert": {strconv.FormatBool(answer.ShowAlert)}, } _, err := c.Do("/messages/answerCallbackQuery", params, nil) From ef67618c17bc1ee2efb2cbde38d9f81c2bdecaa4 Mon Sep 17 00:00:00 2001 From: bulletmys Date: Mon, 13 Apr 2020 18:43:26 +0300 Subject: [PATCH 6/6] [UPD] deleted all []string in client.go --- client.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/client.go b/client.go index f20fe9b..7fba6ad 100644 --- a/client.go +++ b/client.go @@ -171,8 +171,8 @@ func (c *Client) GetVoiceInfo(fileID string) (*File, error) { func (c *Client) SendTextMessage(message *Message) error { params := url.Values{ - "chatId": []string{message.Chat.ID}, - "text": []string{message.Text}, + "chatId": {message.Chat.ID}, + "text": {message.Text}, } if message.ReplyMsgID != "" { @@ -207,9 +207,9 @@ func (c *Client) SendTextMessage(message *Message) error { func (c *Client) EditMessage(message *Message) error { params := url.Values{ - "msgId": []string{message.ID}, - "chatId": []string{message.Chat.ID}, - "text": []string{message.Text}, + "msgId": {message.ID}, + "chatId": {message.Chat.ID}, + "text": {message.Text}, } if message.InlineKeyboard != nil { @@ -235,8 +235,8 @@ func (c *Client) EditMessage(message *Message) error { func (c *Client) DeleteMessage(message *Message) error { params := url.Values{ - "msgId": []string{message.ID}, - "chatId": []string{message.Chat.ID}, + "msgId": {message.ID}, + "chatId": {message.Chat.ID}, } _, err := c.Do("/messages/deleteMessages", params, nil) if err != nil { @@ -395,8 +395,8 @@ func (c *Client) GetEvents(lastEventID int, pollTime int) ([]*Event, error) { func (c *Client) PinMessage(message *Message) error { params := url.Values{ - "chatId": []string{message.Chat.ID}, - "msgId": []string{message.ID}, + "chatId": {message.Chat.ID}, + "msgId": {message.ID}, } _, err := c.Do("/chats/pinMessage", params, nil) if err != nil { @@ -408,8 +408,8 @@ func (c *Client) PinMessage(message *Message) error { func (c *Client) UnpinMessage(message *Message) error { params := url.Values{ - "chatId": []string{message.Chat.ID}, - "msgId": []string{message.ID}, + "chatId": {message.Chat.ID}, + "msgId": {message.ID}, } _, err := c.Do("/chats/unpinMessage", params, nil) if err != nil {