Repository: Yoctol/bottender
Pull request #85 (2017-12-06 09:35:52)

add referral getters for MessengerEvent


Patch:
diff --git a/src/context/MessengerEvent.js b/src/context/MessengerEvent.js
index de92e9e6..e89b14db 100644
--- a/src/context/MessengerEvent.js
+++ b/src/context/MessengerEvent.js
@@ -65,8 +65,16 @@ export type Read = {
   seq: number,
 };
 
+export type Referral = {
+  ref: string,
+  source: string,
+  type: string,
+  origin_domain?: string,
+};
+
 export type Postback = {
   payload: string,
+  referral?: Referral,
 };
 
 export type Optin = {
@@ -152,6 +160,7 @@ export type MessengerRawEvent = {
   app_roles?: AppRoles,
   pass_thread_control?: PassThreadControl,
   take_thread_control?: TakeThreadControl,
+  referral?: Referral,
 };
 
 type MessengerEventOptions = {
@@ -626,7 +635,7 @@ export default class MessengerEvent implements Event {
    *
    */
   get isFromCustomerChatPlugin(): boolean {
-    return (
+    const isMessageFromCustomerChatPlugin = !!(
       this.isMessage &&
       !!(this.message: any).tags &&
       (this.message: any).tags.length !== 0 &&
@@ -634,5 +643,49 @@ export default class MessengerEvent implements Event {
         tag => tag.source === 'customer_chat_plugin'
       )
     );
+
+    const isReferralFromCustomerChatPlugin = !!(
+      this.isReferral &&
+      this.referral &&
+      this.referral.source === 'CUSTOMER_CHAT_PLUGIN'
+    );
+
+    return isMessageFromCustomerChatPlugin || isReferralFromCustomerChatPlugin;
+  }
+
+  /**
+   * Determine if the event is a referral event.
+   *
+   */
+  get isReferral(): boolean {
+    return !!(
+      this._rawEvent.referral ||
+      (this._rawEvent.postback && this._rawEvent.postback.referral)
+    );
+  }
+
+  /**
+   * The referral object from Messenger event.
+   *
+   */
+  get referral(): ?Referral {
+    if (!this.isReferral) {
+      return null;
+    }
+    return (
+      this._rawEvent.referral ||
+      (this._rawEvent.postback && this._rawEvent.postback.referral)
+    );
+  }
+
+  /**
+   * The ref string from Messenger event.
+   *
+   */
+  get ref(): ?string {
+    if (!this.isReferral) {
+      return null;
+    }
+    return this.referral && this.referral.ref;
   }
 }


Test patch:
diff --git a/src/context/__tests__/MessengerEvent.spec.js b/src/context/__tests__/MessengerEvent.spec.js
index ac84e1e6..f2dc93ca 100644
--- a/src/context/__tests__/MessengerEvent.spec.js
+++ b/src/context/__tests__/MessengerEvent.spec.js
@@ -457,6 +457,52 @@ const textMessageFromCustomerChatPlugin = {
   },
 };
 
+const linkReferral = {
+  recipient: {
+    id: '701111199441168',
+  },
+  timestamp: 1511111143921,
+  sender: {
+    id: '1476077111119289',
+  },
+  referral: {
+    ref: 'aaaa',
+    source: 'SHORTLINK',
+    type: 'OPEN_THREAD',
+  },
+};
+
+const postbackReferral = {
+  recipient: {
+    id: '707356222221168',
+  },
+  timestamp: 1522222894827,
+  sender: {
+    id: '1476077422222289',
+  },
+  postback: {
+    payload: '__GET_STARTED__',
+    referral: {
+      source: 'SHORTLINK',
+      type: 'OPEN_THREAD',
+      ref: 'aaaa',
+    },
+    title: 'Get Started',
+  },
+};
+
+const customerChatPluginReferral = {
+  recipient: { id: '693344444818699' },
+  timestamp: 1512552044444,
+  sender: { id: '1242684444404904' },
+  referral: {
+    ref: 'bbbb',
+    source: 'CUSTOMER_CHAT_PLUGIN',
+    type: 'OPEN_THREAD',
+    origin_domain: 'https://test.domain.tw/',
+  },
+};
+
 it('#rawEvent', () => {
   expect(new MessengerEvent(textMessage).rawEvent).toEqual(textMessage);
   expect(new MessengerEvent(imageMessage).rawEvent).toEqual(imageMessage);
@@ -1059,4 +1105,46 @@ it('#isFromCustomerChatPlugin', () => {
     new MessengerEvent(textMessageFromCustomerChatPlugin)
       .isFromCustomerChatPlugin
   ).toEqual(true);
+  expect(
+    new MessengerEvent(customerChatPluginReferral).isFromCustomerChatPlugin
+  ).toEqual(true);
+});
+
+it('#isReferral', () => {
+  expect(new MessengerEvent(linkReferral).isReferral).toEqual(true);
+  expect(new MessengerEvent(postbackReferral).isReferral).toEqual(true);
+  expect(new MessengerEvent(customerChatPluginReferral).isReferral).toEqual(
+    true
+  );
+  expect(new MessengerEvent(postback).isReferral).toEqual(false);
+  expect(new MessengerEvent(textMessage).isReferral).toEqual(false);
+});
+
+it('#referral', () => {
+  expect(new MessengerEvent(linkReferral).referral).toEqual({
+    ref: 'aaaa',
+    source: 'SHORTLINK',
+    type: 'OPEN_THREAD',
+  });
+  expect(new MessengerEvent(postbackReferral).referral).toEqual({
+    ref: 'aaaa',
+    source: 'SHORTLINK',
+    type: 'OPEN_THREAD',
+  });
+  expect(new MessengerEvent(customerChatPluginReferral).referral).toEqual({
+    ref: 'bbbb',
+    source: 'CUSTOMER_CHAT_PLUGIN',
+    type: 'OPEN_THREAD',
+    origin_domain: 'https://test.domain.tw/',
+  });
+  expect(new MessengerEvent(postback).referral).toEqual(null);
+  expect(new MessengerEvent(textMessage).referral).toEqual(null);
+});
+
+it('#ref', () => {
+  expect(new MessengerEvent(linkReferral).ref).toEqual('aaaa');
+  expect(new MessengerEvent(postbackReferral).ref).toEqual('aaaa');
+  expect(new MessengerEvent(customerChatPluginReferral).ref).toEqual('bbbb');
+  expect(new MessengerEvent(postback).ref).toEqual(null);
+  expect(new MessengerEvent(textMessage).ref).toEqual(null);
 });