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 AccountLink event #91

Merged
merged 5 commits into from Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
76 changes: 55 additions & 21 deletions linebot/event.go
Expand Up @@ -25,13 +25,14 @@ type EventType string

// EventType constants
const (
EventTypeMessage EventType = "message"
EventTypeFollow EventType = "follow"
EventTypeUnfollow EventType = "unfollow"
EventTypeJoin EventType = "join"
EventTypeLeave EventType = "leave"
EventTypePostback EventType = "postback"
EventTypeBeacon EventType = "beacon"
EventTypeMessage EventType = "message"
EventTypeFollow EventType = "follow"
EventTypeUnfollow EventType = "unfollow"
EventTypeJoin EventType = "join"
EventTypeLeave EventType = "leave"
EventTypePostback EventType = "postback"
EventTypeBeacon EventType = "beacon"
EventTypeAccountLink EventType = "accountLink"
)

// EventSourceType type
Expand Down Expand Up @@ -82,25 +83,42 @@ type Beacon struct {
DeviceMessage []byte
}

// AccountLinkResult type
type AccountLinkResult string

// AccountLinkResult constants
const (
AccountLinkResultOK AccountLinkResult = "ok"
AccountLinkResultFailed AccountLinkResult = "failed"
)

// AccountLink type
type AccountLink struct {
Result AccountLinkResult
Nonce string
}

// Event type
type Event struct {
ReplyToken string
Type EventType
Timestamp time.Time
Source *EventSource
Message Message
Postback *Postback
Beacon *Beacon
ReplyToken string
Type EventType
Timestamp time.Time
Source *EventSource
Message Message
Postback *Postback
Beacon *Beacon
AccountLink *AccountLink
}

type rawEvent struct {
ReplyToken string `json:"replyToken,omitempty"`
Type EventType `json:"type"`
Timestamp int64 `json:"timestamp"`
Source *EventSource `json:"source"`
Message *rawEventMessage `json:"message,omitempty"`
*Postback `json:"postback,omitempty"`
Beacon *rawBeaconEvent `json:"beacon,omitempty"`
ReplyToken string `json:"replyToken,omitempty"`
Type EventType `json:"type"`
Timestamp int64 `json:"timestamp"`
Source *EventSource `json:"source"`
Message *rawEventMessage `json:"message,omitempty"`
*Postback `json:"postback,omitempty"`
Beacon *rawBeaconEvent `json:"beacon,omitempty"`
AccountLink *rawAccountLinkEvent `json:"link,omitempty"`
}

type rawEventMessage struct {
Expand All @@ -124,6 +142,11 @@ type rawBeaconEvent struct {
DM string `json:"dm,omitempty"`
}

type rawAccountLinkEvent struct {
Result AccountLinkResult `json:"result"`
Nonce string `json:"nonce"`
}

const (
millisecPerSec = int64(time.Second / time.Millisecond)
nanosecPerMillisec = int64(time.Millisecond / time.Nanosecond)
Expand All @@ -145,6 +168,12 @@ func (e *Event) MarshalJSON() ([]byte, error) {
DM: hex.EncodeToString(e.Beacon.DeviceMessage),
}
}
if e.AccountLink != nil {
raw.AccountLink = &rawAccountLinkEvent{
Result: e.AccountLink.Result,
Nonce: e.AccountLink.Nonce,
}
}

switch m := e.Message.(type) {
case *TextMessage:
Expand Down Expand Up @@ -256,6 +285,11 @@ func (e *Event) UnmarshalJSON(body []byte) (err error) {
Type: rawEvent.Beacon.Type,
DeviceMessage: deviceMessage,
}
case EventTypeAccountLink:
e.AccountLink = &AccountLink{
Result: rawEvent.AccountLink.Result,
Nonce: rawEvent.AccountLink.Nonce,
}
}
return
}
26 changes: 26 additions & 0 deletions linebot/webhook_test.go
Expand Up @@ -236,6 +236,19 @@ var webhookTestRequestBody = `{
"type":"enter",
"dm":"1234567890abcdef"
}
},
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please replace tabs to spaces...? (Here is just inside the JSON literal..)

"type": "accountLink",
"replyToken": "nHuyWiB7yP5Zw52FIkcQobQuGDXCTA",
"source": {
"userId": "U012345678901234567890123456789ab",
"type": "user"
},
"timestamp": 1462629479859,
"link": {
"result": "ok",
"nonce": "xxxxxxxxxxxxxxx"
}
}
]
}
Expand Down Expand Up @@ -444,6 +457,19 @@ var webhookTestWantEvents = []*Event{
DeviceMessage: []byte{0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef},
},
},
{
ReplyToken: "nHuyWiB7yP5Zw52FIkcQobQuGDXCTA",
Type: EventTypeAccountLink,
Timestamp: time.Date(2016, time.May, 7, 13, 57, 59, int(859*time.Millisecond), time.UTC),
Source: &EventSource{
Type: EventSourceTypeUser,
UserID: "U012345678901234567890123456789ab",
},
AccountLink: &AccountLink{
Result: "ok",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe Result: AccountLinkResultOK is more better.

Nonce: "xxxxxxxxxxxxxxx",
},
},
}

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