Repository: Yoctol/bottender
Pull request #274 (2018-06-01 03:50:36)

Add isRequestThreadControlFromInbox
I think it would be nice if we can have isRequestThreadControlFromInbox, which determine if the requestThreadControl is sent from facebook integrated inbox.

Also, I modify some test cases according to the official document here: [request_thread_control](https://developers.facebook.com/docs/messenger-platform/reference/webhook-events/messaging_handovers/#request_thread_control)

codecov-io: # [Codecov](https://codecov.io/gh/Yoctol/bottender/pull/274?src=pr&el=h1) Report
> Merging [#274](https://codecov.io/gh/Yoctol/bottender/pull/274?src=pr&el=desc) into [master](https://codecov.io/gh/Yoctol/bottender/commit/deb2add0e141f622b521caefc75064be5222a749?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/Yoctol/bottender/pull/274/graphs/tree.svg?token=JX3EGtxYH3&height=150&src=pr&width=650)](https://codecov.io/gh/Yoctol/bottender/pull/274?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #274      +/-   ##
==========================================
+ Coverage   90.26%   90.27%   +<.01%     
==========================================
  Files         107      107              
  Lines        3216     3217       +1     
  Branches      766      766              
==========================================
+ Hits         2903     2904       +1     
  Misses        275      275              
  Partials       38       38
```


| [Impacted Files](https://codecov.io/gh/Yoctol/bottender/pull/274?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/context/MessengerEvent.js](https://codecov.io/gh/Yoctol/bottender/pull/274/diff?src=pr&el=tree#diff-c3JjL2NvbnRleHQvTWVzc2VuZ2VyRXZlbnQuanM=) | `100% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/Yoctol/bottender/pull/274?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/Yoctol/bottender/pull/274?src=pr&el=footer). Last update [deb2add...16c78bc](https://codecov.io/gh/Yoctol/bottender/pull/274?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/src/context/MessengerEvent.js b/src/context/MessengerEvent.js
index 3d5e7fa7..dcb6ee64 100644
--- a/src/context/MessengerEvent.js
+++ b/src/context/MessengerEvent.js
@@ -700,6 +700,19 @@ export default class MessengerEvent implements Event {
     );
   }
 
+  /**
+   * Determine if the event is a request thread control event sent by facebook integrated inbox (appId is 263902037430900).
+   *
+   */
+  get isRequestThreadControlFromInbox(): boolean {
+    return (
+      !!this._rawEvent.request_thread_control &&
+      typeof this._rawEvent.request_thread_control === 'object' &&
+      this._rawEvent.request_thread_control.requested_owner_app_id ===
+        263902037430900
+    );
+  }
+
   /**
    * The take thread control object from Messenger raw event.
    *


Test patch:
diff --git a/src/context/__tests__/MessengerEvent.spec.js b/src/context/__tests__/MessengerEvent.spec.js
index 3a563bf5..43225875 100644
--- a/src/context/__tests__/MessengerEvent.spec.js
+++ b/src/context/__tests__/MessengerEvent.spec.js
@@ -486,7 +486,21 @@ const requestThreadControl = {
   },
   timestamp: 1458692752478,
   request_thread_control: {
-    requested_owner_app_id: '123456789',
+    requested_owner_app_id: 123456789,
+    metadata: 'additional content that the caller wants to set',
+  },
+};
+
+const requestThreadControlFromInbox = {
+  sender: {
+    id: '404217156637689',
+  },
+  recipient: {
+    id: '1423587017700273',
+  },
+  timestamp: 1458692752478,
+  request_thread_control: {
+    requested_owner_app_id: 263902037430900,
     metadata: 'additional content that the caller wants to set',
   },
 };
@@ -1218,13 +1232,29 @@ it('#isRequestThreadControl', () => {
   ).toEqual(true);
 });
 
+it('#isRequestThreadControlFromInbox', () => {
+  expect(
+    new MessengerEvent(textMessage).isRequestThreadControlFromInbox
+  ).toEqual(false);
+  expect(new MessengerEvent(postback).isRequestThreadControlFromInbox).toEqual(
+    false
+  );
+  expect(
+    new MessengerEvent(requestThreadControlFromInbox).isRequestThreadControl
+  ).toEqual(true);
+  expect(
+    new MessengerEvent(requestThreadControlFromInbox)
+      .isRequestThreadControlFromInbox
+  ).toEqual(true);
+});
+
 it('#requestThreadControl', () => {
   expect(new MessengerEvent(textMessage).requestThreadControl).toEqual(null);
   expect(new MessengerEvent(postback).requestThreadControl).toEqual(null);
   expect(new MessengerEvent(requestThreadControl).requestThreadControl).toEqual(
     {
       metadata: 'additional content that the caller wants to set',
-      requested_owner_app_id: '123456789',
+      requested_owner_app_id: 123456789,
     }
   );
 });