Skip to content

Commit

Permalink
Merge pull request #20 from nikon72ru/callback_message
Browse files Browse the repository at this point in the history
Parse message for callback query event payload
  • Loading branch information
DmitryDorofeev authored Jul 27, 2021
2 parents ed140b0 + d281dcf commit a124e4e
Show file tree
Hide file tree
Showing 4 changed files with 275 additions and 117 deletions.
14 changes: 14 additions & 0 deletions api_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
147 changes: 88 additions & 59 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,21 @@ func TestClient_GetEvents_OK(t *testing.T) {
EventID: 1,
Type: NEW_MESSAGE,
Payload: EventPayload{
MsgID: "57883346846815030",
Chat: Chat{
ID: "[email protected]",
Type: "channel",
Title: "The best channel",
},
From: Contact{
User: User{"1234567890"},
FirstName: "Name",
LastName: "SurName",
BaseEventPayload: BaseEventPayload{
MsgID: "57883346846815030",
Chat: Chat{
ID: "[email protected]",
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,
Expand Down Expand Up @@ -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: "[email protected]",
Type: "channel",
Title: "The best channel",
},
From: Contact{
User: User{"1234567890"},
FirstName: "Name",
LastName: "SurName",
BaseEventPayload: BaseEventPayload{
MsgID: "57883346846815030",
Chat: Chat{
ID: "[email protected]",
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: "[email protected]",
Type: "channel",
Title: "The best channel",
BaseEventPayload: BaseEventPayload{
MsgID: "57883346846815030",
Chat: Chat{
ID: "[email protected]",
Type: "channel",
Title: "The best channel",
},
Timestamp: 1546290000,
},
Timestamp: 1546290000,
},
},
{
EventID: 4,
Type: PINNED_MESSAGE,
Payload: EventPayload{
MsgID: "6720509406122810000",
Chat: Chat{
ID: "[email protected]",
Type: "group",
Title: "The best group",
},
From: Contact{
User: User{"9876543210"},
FirstName: "Name",
LastName: "SurName",
BaseEventPayload: BaseEventPayload{
MsgID: "6720509406122810000",
Chat: Chat{
ID: "[email protected]",
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: "[email protected]",
Type: "group",
Title: "The best group",
BaseEventPayload: BaseEventPayload{
MsgID: "6720509406122810000",
Chat: Chat{
ID: "[email protected]",
Type: "group",
Title: "The best group",
},
Timestamp: 1564740530,
},
Timestamp: 1564740530,
},
},
{
EventID: 6,
Type: NEW_CHAT_MEMBERS,
Payload: EventPayload{
Chat: Chat{
ID: "[email protected]",
Type: "group",
Title: "The best group",
BaseEventPayload: BaseEventPayload{
Chat: Chat{
ID: "[email protected]",
Type: "group",
Title: "The best group",
},
},
NewMembers: []Contact{
{
Expand All @@ -214,10 +226,12 @@ func TestClient_GetEvents_OK(t *testing.T) {
EventID: 7,
Type: LEFT_CHAT_MEMBERS,
Payload: EventPayload{
Chat: Chat{
ID: "[email protected]",
Type: "group",
Title: "The best group",
BaseEventPayload: BaseEventPayload{
Chat: Chat{
ID: "[email protected]",
Type: "group",
Title: "The best group",
},
},
LeftMembers: []Contact{
{
Expand All @@ -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",
},
Expand Down
38 changes: 26 additions & 12 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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"`
Expand All @@ -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,
}
}

Expand Down
Loading

0 comments on commit a124e4e

Please sign in to comment.