Repository: Yoctol/bottender (ts)

Issue:
support LINE accountLink event
https://developers.line.me/en/docs/messaging-api/reference/#account-link-event

Pull request description:
add LINE linkAccount events support
close #241

Patch:
diff --git a/src/context/LineEvent.js b/src/context/LineEvent.js
index f13f25ec..49c184f1 100644
--- a/src/context/LineEvent.js
+++ b/src/context/LineEvent.js
@@ -11,7 +11,8 @@ type EventType =
   | 'unfollow'
   | 'join'
   | 'leave'
-  | 'beacon';
+  | 'beacon'
+  | 'accountLink';
 
 type UserSource = {
   type: 'user',
@@ -53,8 +54,13 @@ type Beacon = {|
   type: string,
 |};
 
+type AccountLink = {|
+  result: 'ok' | 'false',
+  nonce: string,
+|};
+
 export type LineRawEvent = {
-  // only message, follow, join, postback, beacon events have replyToken
+  // only message, follow, join, postback, beacon, accountLink events have replyToken
   replyToken?: ReplyToken,
   type: EventType,
   timestamp: number,
@@ -62,6 +68,7 @@ export type LineRawEvent = {
   message?: Message,
   postback?: Postback,
   beacon?: Beacon,
+  link?: AccountLink,
 };
 
 export default class LineEvent implements Event {
@@ -388,4 +395,20 @@ export default class LineEvent implements Event {
   get beacon(): ?Beacon {
     return this._rawEvent.beacon || null;
   }
+
+  /**
+   * Determine if the event is an accountLink event.
+   *
+   */
+  get isAccountLink(): boolean {
+    return this._rawEvent.type === 'accountLink';
+  }
+
+  /**
+   * The link object from LINE raw event.
+   *
+   */
+  get accountLink(): ?AccountLink {
+    return this._rawEvent.link || null;
+  }
 }


Test patch:
diff --git a/src/context/__tests__/LineEvent.spec.js b/src/context/__tests__/LineEvent.spec.js
index f4fc23d6..b07609dd 100644
--- a/src/context/__tests__/LineEvent.spec.js
+++ b/src/context/__tests__/LineEvent.spec.js
@@ -204,6 +204,20 @@ const beacon = {
   },
 };
 
+const accountLink = {
+  type: 'accountLink',
+  replyToken: 'b60d432864f44d079f6d8efe86cf404b',
+  source: {
+    userId: 'U206d25c2ea6bd87c17655609a1c37cb8',
+    type: 'user',
+  },
+  timestamp: 1513669370317,
+  link: {
+    result: 'ok',
+    nonce: 'xxxxxxxxxxxxxxx',
+  },
+};
+
 const noSourceMessage = {};
 
 it('#rawEvent', () => {
@@ -652,3 +666,28 @@ it('#beacon', () => {
     type: 'enter',
   });
 });
+
+it('#isAccountLink', () => {
+  expect(new LineEvent(textMessage).isAccountLink).toEqual(false);
+  expect(new LineEvent(follow).isAccountLink).toEqual(false);
+  expect(new LineEvent(unfollow).isAccountLink).toEqual(false);
+  expect(new LineEvent(join).isAccountLink).toEqual(false);
+  expect(new LineEvent(leave).isAccountLink).toEqual(false);
+  expect(new LineEvent(postback).isAccountLink).toEqual(false);
+  expect(new LineEvent(beacon).isAccountLink).toEqual(false);
+  expect(new LineEvent(accountLink).isAccountLink).toEqual(true);
+});
+
+it('#accountLink', () => {
+  expect(new LineEvent(textMessage).accountLink).toEqual(null);
+  expect(new LineEvent(follow).accountLink).toEqual(null);
+  expect(new LineEvent(unfollow).accountLink).toEqual(null);
+  expect(new LineEvent(join).accountLink).toEqual(null);
+  expect(new LineEvent(leave).accountLink).toEqual(null);
+  expect(new LineEvent(postback).accountLink).toEqual(null);
+  expect(new LineEvent(beacon).accountLink).toEqual(null);
+  expect(new LineEvent(accountLink).accountLink).toEqual({
+    result: 'ok',
+    nonce: 'xxxxxxxxxxxxxxx',
+  });
+});