diff --git a/api_mock.go b/api_mock.go index 768d7b5..2ae17dd 100644 --- a/api_mock.go +++ b/api_mock.go @@ -230,6 +230,20 @@ func (h *MockHandler) GetEvents(w http.ResponseWriter) { "firstName": "Name", "userId": "1234567890" }, + "message": { + "chat": { + "chatId": "1234567890", + "type": "private" + }, + "from": { + "firstName": "bot_name", + "nick": "bot_nick", + "userId": "bot_id" + }, + "msgId": "6720509406122810000", + "text": "Some important information!", + "timestamp": 1564740530 + }, "queryId": "SVR:123456" }, "type": "callbackQuery" diff --git a/client_test.go b/client_test.go index 3ce1280..e91c40d 100644 --- a/client_test.go +++ b/client_test.go @@ -60,18 +60,21 @@ func TestClient_GetEvents_OK(t *testing.T) { EventID: 1, Type: NEW_MESSAGE, Payload: EventPayload{ - MsgID: "57883346846815030", - Chat: Chat{ - ID: "681869378@chat.agent", - Type: "channel", - Title: "The best channel", - }, - From: Contact{ - User: User{"1234567890"}, - FirstName: "Name", - LastName: "SurName", + BaseEventPayload: BaseEventPayload{ + MsgID: "57883346846815030", + Chat: Chat{ + ID: "681869378@chat.agent", + Type: "channel", + Title: "The best channel", + }, + From: Contact{ + User: User{"1234567890"}, + FirstName: "Name", + LastName: "SurName", + }, + Text: "Hello!", + Timestamp: 1546290000, }, - Text: "Hello!", Parts: []Part{ { Type: STICKER, @@ -120,81 +123,90 @@ func TestClient_GetEvents_OK(t *testing.T) { }, }, }, - Timestamp: 1546290000, }, }, { EventID: 2, Type: EDITED_MESSAGE, Payload: EventPayload{ - MsgID: "57883346846815030", - Chat: Chat{ - ID: "681869378@chat.agent", - Type: "channel", - Title: "The best channel", - }, - From: Contact{ - User: User{"1234567890"}, - FirstName: "Name", - LastName: "SurName", + BaseEventPayload: BaseEventPayload{ + MsgID: "57883346846815030", + Chat: Chat{ + ID: "681869378@chat.agent", + Type: "channel", + Title: "The best channel", + }, + From: Contact{ + User: User{"1234567890"}, + FirstName: "Name", + LastName: "SurName", + }, + Text: "Hello!", + Timestamp: 1546290000, }, - Text: "Hello!", - Timestamp: 1546290000, }, }, { EventID: 3, Type: DELETED_MESSAGE, Payload: EventPayload{ - MsgID: "57883346846815030", - Chat: Chat{ - ID: "681869378@chat.agent", - Type: "channel", - Title: "The best channel", + BaseEventPayload: BaseEventPayload{ + MsgID: "57883346846815030", + Chat: Chat{ + ID: "681869378@chat.agent", + Type: "channel", + Title: "The best channel", + }, + Timestamp: 1546290000, }, - Timestamp: 1546290000, }, }, { EventID: 4, Type: PINNED_MESSAGE, Payload: EventPayload{ - MsgID: "6720509406122810000", - Chat: Chat{ - ID: "681869378@chat.agent", - Type: "group", - Title: "The best group", - }, - From: Contact{ - User: User{"9876543210"}, - FirstName: "Name", - LastName: "SurName", + BaseEventPayload: BaseEventPayload{ + MsgID: "6720509406122810000", + Chat: Chat{ + ID: "681869378@chat.agent", + Type: "group", + Title: "The best group", + }, + From: Contact{ + User: User{"9876543210"}, + FirstName: "Name", + LastName: "SurName", + }, + Text: "Some important information!", + Timestamp: 1564740530, }, - Text: "Some important information!", - Timestamp: 1564740530, }, }, { EventID: 5, Type: UNPINNED_MESSAGE, Payload: EventPayload{ - MsgID: "6720509406122810000", - Chat: Chat{ - ID: "681869378@chat.agent", - Type: "group", - Title: "The best group", + BaseEventPayload: BaseEventPayload{ + MsgID: "6720509406122810000", + Chat: Chat{ + ID: "681869378@chat.agent", + Type: "group", + Title: "The best group", + }, + Timestamp: 1564740530, }, - Timestamp: 1564740530, }, }, { EventID: 6, Type: NEW_CHAT_MEMBERS, Payload: EventPayload{ - Chat: Chat{ - ID: "681869378@chat.agent", - Type: "group", - Title: "The best group", + BaseEventPayload: BaseEventPayload{ + Chat: Chat{ + ID: "681869378@chat.agent", + Type: "group", + Title: "The best group", + }, }, NewMembers: []Contact{ { @@ -214,10 +226,12 @@ func TestClient_GetEvents_OK(t *testing.T) { EventID: 7, Type: LEFT_CHAT_MEMBERS, Payload: EventPayload{ - Chat: Chat{ - ID: "681869378@chat.agent", - Type: "group", - Title: "The best group", + BaseEventPayload: BaseEventPayload{ + Chat: Chat{ + ID: "681869378@chat.agent", + Type: "group", + Title: "The best group", + }, }, LeftMembers: []Contact{ { @@ -238,9 +252,24 @@ func TestClient_GetEvents_OK(t *testing.T) { Type: CALLBACK_QUERY, Payload: EventPayload{ CallbackData: "echo", - From: Contact{ - User: User{"1234567890"}, - FirstName: "Name", + CallbackMsg: BaseEventPayload{ + MsgID: "6720509406122810000", + Chat: Chat{ + ID: "1234567890", + Type: "private", + }, + From: Contact{ + User: User{"bot_id"}, + FirstName: "bot_name", + }, + Text: "Some important information!", + Timestamp: 1564740530, + }, + BaseEventPayload: BaseEventPayload{ + From: Contact{ + User: User{"1234567890"}, + FirstName: "Name", + }, }, QueryID: "SVR:123456", }, diff --git a/types.go b/types.go index 5003900..cb3e536 100644 --- a/types.go +++ b/types.go @@ -83,9 +83,7 @@ type Contact struct { LastName string `json:"lastName"` } -type EventPayload struct { - client *Client - +type BaseEventPayload struct { // Id of the message. // Presented in newMessage, editedMessage, deletedMessage, pinnedMessage, unpinnedMessage events. MsgID string `json:"msgId"` @@ -102,17 +100,26 @@ type EventPayload struct { // Presented in newMessage, editedMessage and pinnedMessage events. Text string `json:"text"` + // Timestamp of the event. + Timestamp int `json:"timestamp"` +} + +type EventPayload struct { + client *Client + BaseEventPayload + // Parts of the message. // Presented only in newMessage event. Parts []Part `json:"parts"` - // Timestamp of the event. - Timestamp int `json:"timestamp"` - // Id of the query. // Presented only in callbackQuery event. QueryID string `json:"queryId"` + // Callback message of the query (parent message for button). + // Presented only in callbackQuery event. + CallbackMsg BaseEventPayload `json:"message"` + // CallbackData of the query (id of button). // Presented only in callbackQuery event. CallbackData string `json:"callbackData"` @@ -127,14 +134,21 @@ type EventPayload struct { } func (ep *EventPayload) Message() *Message { - ep.Chat.client = ep.client + return message(ep.client, ep.BaseEventPayload) +} + +func (ep *EventPayload) CallbackMessage() *Message { + return message(ep.client, ep.CallbackMsg) +} +func message(client *Client, msg BaseEventPayload) *Message { + msg.Chat.client = client return &Message{ - client: ep.client, - ID: ep.MsgID, - Text: ep.Text, - Chat: ep.Chat, - Timestamp: ep.Timestamp, + client: client, + ID: msg.MsgID, + Text: msg.Text, + Chat: msg.Chat, + Timestamp: msg.Timestamp, } } diff --git a/types_easyjson.go b/types_easyjson.go index 3877e6d..7fc1187 100644 --- a/types_easyjson.go +++ b/types_easyjson.go @@ -822,14 +822,12 @@ func easyjson6601e8cdDecodeGithubComMailRuImBotGolang9(in *jlexer.Lexer, out *Ev continue } switch key { - case "msgId": - out.MsgID = string(in.String()) - case "chat": - (out.Chat).UnmarshalEasyJSON(in) - case "from": - (out.From).UnmarshalEasyJSON(in) - case "text": - out.Text = string(in.String()) + case "queryId": + out.QueryID = string(in.String()) + case "message": + (out.CallbackMsg).UnmarshalEasyJSON(in) + case "callbackData": + out.CallbackData = string(in.String()) case "parts": if in.IsNull() { in.Skip() @@ -853,12 +851,6 @@ func easyjson6601e8cdDecodeGithubComMailRuImBotGolang9(in *jlexer.Lexer, out *Ev } in.Delim(']') } - case "timestamp": - out.Timestamp = int(in.Int()) - case "queryId": - out.QueryID = string(in.String()) - case "callbackData": - out.CallbackData = string(in.String()) case "leftMembers": if in.IsNull() { in.Skip() @@ -909,6 +901,16 @@ func easyjson6601e8cdDecodeGithubComMailRuImBotGolang9(in *jlexer.Lexer, out *Ev (out.AddedBy).UnmarshalEasyJSON(in) case "removedBy": (out.RemovedBy).UnmarshalEasyJSON(in) + case "msgId": + out.MsgID = string(in.String()) + case "chat": + (out.Chat).UnmarshalEasyJSON(in) + case "from": + (out.From).UnmarshalEasyJSON(in) + case "text": + out.Text = string(in.String()) + case "timestamp": + out.Timestamp = int(in.Int()) default: in.SkipRecursive() } @@ -924,24 +926,19 @@ func easyjson6601e8cdEncodeGithubComMailRuImBotGolang9(out *jwriter.Writer, in E first := true _ = first { - const prefix string = ",\"msgId\":" + const prefix string = ",\"queryId\":" out.RawString(prefix[1:]) - out.String(string(in.MsgID)) - } - { - const prefix string = ",\"chat\":" - out.RawString(prefix) - (in.Chat).MarshalEasyJSON(out) + out.String(string(in.QueryID)) } { - const prefix string = ",\"from\":" + const prefix string = ",\"message\":" out.RawString(prefix) - (in.From).MarshalEasyJSON(out) + (in.CallbackMsg).MarshalEasyJSON(out) } { - const prefix string = ",\"text\":" + const prefix string = ",\"callbackData\":" out.RawString(prefix) - out.String(string(in.Text)) + out.String(string(in.CallbackData)) } { const prefix string = ",\"parts\":" @@ -959,21 +956,6 @@ func easyjson6601e8cdEncodeGithubComMailRuImBotGolang9(out *jwriter.Writer, in E out.RawByte(']') } } - { - const prefix string = ",\"timestamp\":" - out.RawString(prefix) - out.Int(int(in.Timestamp)) - } - { - const prefix string = ",\"queryId\":" - out.RawString(prefix) - out.String(string(in.QueryID)) - } - { - const prefix string = ",\"callbackData\":" - out.RawString(prefix) - out.String(string(in.CallbackData)) - } { const prefix string = ",\"leftMembers\":" out.RawString(prefix) @@ -1016,6 +998,31 @@ func easyjson6601e8cdEncodeGithubComMailRuImBotGolang9(out *jwriter.Writer, in E out.RawString(prefix) (in.RemovedBy).MarshalEasyJSON(out) } + { + const prefix string = ",\"msgId\":" + out.RawString(prefix) + out.String(string(in.MsgID)) + } + { + const prefix string = ",\"chat\":" + out.RawString(prefix) + (in.Chat).MarshalEasyJSON(out) + } + { + const prefix string = ",\"from\":" + out.RawString(prefix) + (in.From).MarshalEasyJSON(out) + } + { + const prefix string = ",\"text\":" + out.RawString(prefix) + out.String(string(in.Text)) + } + { + const prefix string = ",\"timestamp\":" + out.RawString(prefix) + out.Int(int(in.Timestamp)) + } out.RawByte('}') } @@ -1408,7 +1415,101 @@ func (v *BotInfo) UnmarshalJSON(data []byte) error { func (v *BotInfo) UnmarshalEasyJSON(l *jlexer.Lexer) { easyjson6601e8cdDecodeGithubComMailRuImBotGolang13(l, v) } -func easyjson6601e8cdDecodeGithubComMailRuImBotGolang14(in *jlexer.Lexer, out *AdminsListResponse) { +func easyjson6601e8cdDecodeGithubComMailRuImBotGolang14(in *jlexer.Lexer, out *BaseEventPayload) { + 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 "msgId": + out.MsgID = string(in.String()) + case "chat": + (out.Chat).UnmarshalEasyJSON(in) + case "from": + (out.From).UnmarshalEasyJSON(in) + case "text": + out.Text = string(in.String()) + case "timestamp": + out.Timestamp = int(in.Int()) + default: + in.SkipRecursive() + } + in.WantComma() + } + in.Delim('}') + if isTopLevel { + in.Consumed() + } +} +func easyjson6601e8cdEncodeGithubComMailRuImBotGolang14(out *jwriter.Writer, in BaseEventPayload) { + out.RawByte('{') + first := true + _ = first + { + const prefix string = ",\"msgId\":" + out.RawString(prefix[1:]) + out.String(string(in.MsgID)) + } + { + const prefix string = ",\"chat\":" + out.RawString(prefix) + (in.Chat).MarshalEasyJSON(out) + } + { + const prefix string = ",\"from\":" + out.RawString(prefix) + (in.From).MarshalEasyJSON(out) + } + { + const prefix string = ",\"text\":" + out.RawString(prefix) + out.String(string(in.Text)) + } + { + const prefix string = ",\"timestamp\":" + out.RawString(prefix) + out.Int(int(in.Timestamp)) + } + out.RawByte('}') +} + +// MarshalJSON supports json.Marshaler interface +func (v BaseEventPayload) MarshalJSON() ([]byte, error) { + w := jwriter.Writer{} + easyjson6601e8cdEncodeGithubComMailRuImBotGolang14(&w, v) + return w.Buffer.BuildBytes(), w.Error +} + +// MarshalEasyJSON supports easyjson.Marshaler interface +func (v BaseEventPayload) MarshalEasyJSON(w *jwriter.Writer) { + easyjson6601e8cdEncodeGithubComMailRuImBotGolang14(w, v) +} + +// UnmarshalJSON supports json.Unmarshaler interface +func (v *BaseEventPayload) UnmarshalJSON(data []byte) error { + r := jlexer.Lexer{Data: data} + easyjson6601e8cdDecodeGithubComMailRuImBotGolang14(&r, v) + return r.Error() +} + +// UnmarshalEasyJSON supports easyjson.Unmarshaler interface +func (v *BaseEventPayload) UnmarshalEasyJSON(l *jlexer.Lexer) { + easyjson6601e8cdDecodeGithubComMailRuImBotGolang14(l, v) +} +func easyjson6601e8cdDecodeGithubComMailRuImBotGolang15(in *jlexer.Lexer, out *AdminsListResponse) { isTopLevel := in.IsStart() if in.IsNull() { if isTopLevel { @@ -1460,7 +1561,7 @@ func easyjson6601e8cdDecodeGithubComMailRuImBotGolang14(in *jlexer.Lexer, out *A in.Consumed() } } -func easyjson6601e8cdEncodeGithubComMailRuImBotGolang14(out *jwriter.Writer, in AdminsListResponse) { +func easyjson6601e8cdEncodeGithubComMailRuImBotGolang15(out *jwriter.Writer, in AdminsListResponse) { out.RawByte('{') first := true _ = first @@ -1486,23 +1587,23 @@ func easyjson6601e8cdEncodeGithubComMailRuImBotGolang14(out *jwriter.Writer, in // MarshalJSON supports json.Marshaler interface func (v AdminsListResponse) MarshalJSON() ([]byte, error) { w := jwriter.Writer{} - easyjson6601e8cdEncodeGithubComMailRuImBotGolang14(&w, v) + easyjson6601e8cdEncodeGithubComMailRuImBotGolang15(&w, v) return w.Buffer.BuildBytes(), w.Error } // MarshalEasyJSON supports easyjson.Marshaler interface func (v AdminsListResponse) MarshalEasyJSON(w *jwriter.Writer) { - easyjson6601e8cdEncodeGithubComMailRuImBotGolang14(w, v) + easyjson6601e8cdEncodeGithubComMailRuImBotGolang15(w, v) } // UnmarshalJSON supports json.Unmarshaler interface func (v *AdminsListResponse) UnmarshalJSON(data []byte) error { r := jlexer.Lexer{Data: data} - easyjson6601e8cdDecodeGithubComMailRuImBotGolang14(&r, v) + easyjson6601e8cdDecodeGithubComMailRuImBotGolang15(&r, v) return r.Error() } // UnmarshalEasyJSON supports easyjson.Unmarshaler interface func (v *AdminsListResponse) UnmarshalEasyJSON(l *jlexer.Lexer) { - easyjson6601e8cdDecodeGithubComMailRuImBotGolang14(l, v) + easyjson6601e8cdDecodeGithubComMailRuImBotGolang15(l, v) }