Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new EventType. #117

Merged
merged 4 commits into from
Nov 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 39 additions & 14 deletions linebot/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@ type EventType string

// EventType constants
const (
EventTypeMessage EventType = "message"
EventTypeFollow EventType = "follow"
EventTypeUnfollow EventType = "unfollow"
EventTypeJoin EventType = "join"
EventTypeLeave EventType = "leave"
EventTypeMemberJoin EventType = "memberJoined"
EventTypeMemberLeave EventType = "memberLeft"
EventTypePostback EventType = "postback"
EventTypeBeacon EventType = "beacon"
EventTypeAccountLink EventType = "accountLink"
EventTypeMessage EventType = "message"
EventTypeFollow EventType = "follow"
EventTypeUnfollow EventType = "unfollow"
EventTypeJoin EventType = "join"
EventTypeLeave EventType = "leave"
EventTypeMemberJoined EventType = "memberJoined"
EventTypeMemberLeft EventType = "memberLeft"
EventTypePostback EventType = "postback"
EventTypeBeacon EventType = "beacon"
EventTypeAccountLink EventType = "accountLink"
EventTypeThings EventType = "things"
)

// EventSourceType type
Expand Down Expand Up @@ -62,6 +63,11 @@ type Params struct {
Datetime string `json:"datetime,omitempty"`
}

// Members type
type Members struct {
Members []EventSource `json:"members"`
}

// Postback type
type Postback struct {
Data string `json:"data"`
Expand Down Expand Up @@ -100,16 +106,25 @@ type AccountLink struct {
Nonce string
}

// Things type
type Things struct {
DeviceID string `json:"deviceId"`
Type string `json:"type"`
}

// Event type
type Event struct {
ReplyToken string
Type EventType
Timestamp time.Time
Source *EventSource
Message Message
Joined *Members `json:"joined"`
Left *Members `json:"left"`
Postback *Postback
Beacon *Beacon
AccountLink *AccountLink
Things *Things `json:"things"`
Members []*EventSource
}

Expand All @@ -124,6 +139,7 @@ type rawEvent struct {
AccountLink *rawAccountLinkEvent `json:"link,omitempty"`
Joined *rawMemberEvent `json:"joined,omitempty"`
Left *rawMemberEvent `json:"left,omitempty"`
Things *Things `json:"things,omitempty"`
}

type rawMemberEvent struct {
Expand Down Expand Up @@ -185,14 +201,19 @@ func (e *Event) MarshalJSON() ([]byte, error) {
}

switch e.Type {
case EventTypeMemberJoin:
case EventTypeMemberJoined:
raw.Joined = &rawMemberEvent{
Members: e.Members,
}
case EventTypeMemberLeave:
case EventTypeMemberLeft:
raw.Left = &rawMemberEvent{
Members: e.Members,
}
case EventTypeThings:
raw.Things = &Things{
DeviceID: e.Things.DeviceID,
Type: e.Things.Type,
}
}

switch m := e.Message.(type) {
Expand Down Expand Up @@ -310,10 +331,14 @@ func (e *Event) UnmarshalJSON(body []byte) (err error) {
Result: rawEvent.AccountLink.Result,
Nonce: rawEvent.AccountLink.Nonce,
}
case EventTypeMemberJoin:
case EventTypeMemberJoined:
e.Members = rawEvent.Joined.Members
case EventTypeMemberLeave:
case EventTypeMemberLeft:
e.Members = rawEvent.Left.Members
case EventTypeThings:
e.Things = new(Things)
e.Things.Type = rawEvent.Things.Type
e.Things.DeviceID = rawEvent.Things.DeviceID
}
return
}
52 changes: 50 additions & 2 deletions linebot/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,30 @@ var webhookTestRequestBody = `{
"userId": "U91eeaf62d901234567890123456789ab"
}
]
}
},
{
"type": "things",
"timestamp": 1462629479859,
"source": {
"type": "user",
"userId": "U91eeaf62d901234567890123456789ab"
},
"things": {
"deviceId": "t2c449c9d1...",
"type": "link"
}
},
{
"type": "things",
"timestamp": 1462629479859,
"source": {
"type": "user",
"userId": "U91eeaf62d901234567890123456789ab"
},
"things": {
"deviceId": "t2c449c9d1...",
"type": "unlink"
}
}
]
Expand Down Expand Up @@ -513,7 +537,7 @@ var webhookTestWantEvents = []*Event{
},
{
ReplyToken: "0f3779fba3b349968c5d07db31eabf65",
Type: EventTypeMemberJoin,
Type: EventTypeMemberJoined,
Timestamp: time.Date(2016, time.May, 7, 13, 57, 59, int(859*time.Millisecond), time.UTC),
Source: &EventSource{
Type: EventSourceTypeGroup,
Expand All @@ -531,7 +555,7 @@ var webhookTestWantEvents = []*Event{
},
},
{
Type: EventTypeMemberLeave,
Type: EventTypeMemberLeft,
Timestamp: time.Date(2016, time.May, 7, 13, 57, 59, int(960*time.Millisecond), time.UTC),
Source: &EventSource{
Type: EventSourceTypeGroup,
Expand All @@ -548,6 +572,30 @@ var webhookTestWantEvents = []*Event{
},
},
},
{
Type: EventTypeThings,
Timestamp: time.Date(2016, time.May, 7, 13, 57, 59, int(859*time.Millisecond), time.UTC),
Source: &EventSource{
Type: EventSourceTypeUser,
UserID: "U91eeaf62d901234567890123456789ab",
},
Things: &Things{
DeviceID: `t2c449c9d1...`,
Type: `link`,
},
},
{
Type: EventTypeThings,
Timestamp: time.Date(2016, time.May, 7, 13, 57, 59, int(859*time.Millisecond), time.UTC),
Source: &EventSource{
Type: EventSourceTypeUser,
UserID: "U91eeaf62d901234567890123456789ab",
},
Things: &Things{
DeviceID: `t2c449c9d1...`,
Type: `unlink`,
},
},
}

func TestParseRequest(t *testing.T) {
Expand Down