Repository: Yoctol/bottender
Pull request #617 (2020-01-06 03:59:48)

feat: add getter and method to label intent for intent tracking purpose
```js
context.intent; // null

context.setIntent('hello-world'); // null

context.intent; // 'hello-world'
```

This may help us build up the integration with chatbase and dashbot.

codecov-io: # [Codecov](https://codecov.io/gh/Yoctol/bottender/pull/617?src=pr&el=h1) Report
> Merging [#617](https://codecov.io/gh/Yoctol/bottender/pull/617?src=pr&el=desc) into [release/1.2](https://codecov.io/gh/Yoctol/bottender/commit/0d1a85161b2bb9a60775baf20beb70e165578b63?src=pr&el=desc) will **increase** coverage by `0.01%`.
> The diff coverage is `100%`.

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

```diff
@@              Coverage Diff               @@
##           release/1.2    #617      +/-   ##
==============================================
+ Coverage        80.79%   80.8%   +0.01%     
==============================================
  Files               95      95              
  Lines             4270    4273       +3     
  Branches          1046    1046              
==============================================
+ Hits              3450    3453       +3     
  Misses             779     779              
  Partials            41      41
```


| [Impacted Files](https://codecov.io/gh/Yoctol/bottender/pull/617?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [packages/bottender/src/context/Context.ts](https://codecov.io/gh/Yoctol/bottender/pull/617/diff?src=pr&el=tree#diff-cGFja2FnZXMvYm90dGVuZGVyL3NyYy9jb250ZXh0L0NvbnRleHQudHM=) | `95.55% <100%> (+0.31%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/Yoctol/bottender/pull/617?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/617?src=pr&el=footer). Last update [0d1a851...a79b894](https://codecov.io/gh/Yoctol/bottender/pull/617?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/packages/bottender/src/context/Context.ts b/packages/bottender/src/context/Context.ts
index e3ba6b23..89e3709f 100644
--- a/packages/bottender/src/context/Context.ts
+++ b/packages/bottender/src/context/Context.ts
@@ -50,6 +50,8 @@ export default abstract class Context<C extends Client, E extends Event> {
 
   _emitter: EventEmitter | null;
 
+  _intent: string | null;
+
   response: Response;
 
   constructor({
@@ -66,6 +68,7 @@ export default abstract class Context<C extends Client, E extends Event> {
     this._initialState = initialState || {};
     this._requestContext = requestContext || null;
     this._emitter = emitter || null;
+    this._intent = null;
 
     debugContext('Context created with rawEvent:');
     debugContext(JSON.stringify(this._event.rawEvent, null, 2));
@@ -99,7 +102,7 @@ export default abstract class Context<C extends Client, E extends Event> {
   }
 
   /**
-   * The context of request.
+   * The context of the request.
    *
    */
   get requestContext(): Record<string, any> | null {
@@ -127,7 +130,7 @@ export default abstract class Context<C extends Client, E extends Event> {
   }
 
   /**
-   * The state in the conversation context.
+   * The state of the conversation context.
    *
    */
   get state(): Record<string, any> {
@@ -186,6 +189,22 @@ export default abstract class Context<C extends Client, E extends Event> {
     }
   }
 
+  /**
+   * The intent of the conversation context.
+   *
+   */
+  get intent(): string | null {
+    return this._intent;
+  }
+
+  /**
+   * Set intent to the conversation context.
+   *
+   */
+  setIntent(intent: string): void {
+    this._intent = intent;
+  }
+
   emitError(err: Error): void {
     if (this._emitter) {
       this._emitter.emit('error', err, this);


Test patch:
diff --git a/packages/bottender/src/context/__tests__/Context.spec.ts b/packages/bottender/src/context/__tests__/Context.spec.ts
new file mode 100644
index 00000000..da09cb5d
--- /dev/null
+++ b/packages/bottender/src/context/__tests__/Context.spec.ts
@@ -0,0 +1,26 @@
+import Context from '../Context';
+
+class TestContext extends Context<any, any> {
+  get platform() {
+    return 'test';
+  }
+
+  // eslint-disable-next-line @typescript-eslint/no-empty-function
+  sendText() {}
+}
+
+describe('intent', () => {
+  it('should default to null', () => {
+    const context = new TestContext({ client: {}, event: {} });
+
+    expect(context.intent).toBeNull();
+  });
+
+  it('should support labeling intent', () => {
+    const context = new TestContext({ client: {}, event: {} });
+
+    context.setIntent('hello-world');
+
+    expect(context.intent).toEqual('hello-world');
+  });
+});