Repository: Yoctol/bottender
Pull request #207 (2018-03-13 03:57:32)

support using both /quit an /exit to exit
```
> /exit
> /quit
```

Should both work


Patch:
diff --git a/src/bot/ConsoleBot.js b/src/bot/ConsoleBot.js
index d21144cd..5e6046e3 100644
--- a/src/bot/ConsoleBot.js
+++ b/src/bot/ConsoleBot.js
@@ -23,7 +23,8 @@ export default class ConsoleBot extends Bot {
     });
 
     const handleLine = async (line: string = '') => {
-      if (line.toLowerCase() === '/quit') {
+      const lowerCaseLine = line.toLowerCase();
+      if (lowerCaseLine === '/quit' || lowerCaseLine === '/exit') {
         rl.close();
         process.exit();
       }
diff --git a/src/bot/MessengerBot.js b/src/bot/MessengerBot.js
index fe4017ad..aa6f4ff7 100644
--- a/src/bot/MessengerBot.js
+++ b/src/bot/MessengerBot.js
@@ -12,20 +12,17 @@ export default class MessengerBot extends Bot {
     sessionStore,
     sync,
     mapPageToAccessToken,
-    verifyToken,
   }: {
     accessToken: string,
     appSecret: string,
     sessionStore: SessionStore,
     sync?: boolean,
     mapPageToAccessToken?: (pageId: string) => Promise<string>,
-    verifyToken?: string,
   }) {
     const connector = new MessengerConnector({
       accessToken,
       appSecret,
       mapPageToAccessToken,
-      verifyToken,
     });
     super({ connector, sessionStore, sync });
   }
diff --git a/src/bot/MessengerConnector.js b/src/bot/MessengerConnector.js
index 8e193bea..ca01c655 100644
--- a/src/bot/MessengerConnector.js
+++ b/src/bot/MessengerConnector.js
@@ -78,7 +78,6 @@ type ConstructorOptions = {|
   appSecret?: string,
   client?: MessengerClient,
   mapPageToAccessToken?: (pageId: string) => Promise<string>,
-  verifyToken?: ?string,
 |};
 
 export default class MessengerConnector
@@ -86,19 +85,16 @@ export default class MessengerConnector
   _client: MessengerClient;
   _appSecret: string;
   _mapPageToAccessToken: ?(pageId: string) => ?Promise<string>;
-  _verifyToken: ?string;
 
   constructor({
     accessToken,
     appSecret,
     client,
     mapPageToAccessToken,
-    verifyToken,
   }: ConstructorOptions) {
     this._client = client || MessengerClient.connect(accessToken || '');
     this._appSecret = appSecret || '';
     this._mapPageToAccessToken = mapPageToAccessToken;
-    this._verifyToken = verifyToken;
     if (!this._appSecret) {
       warning(
         false,
@@ -165,10 +161,6 @@ export default class MessengerConnector
     return this._client;
   }
 
-  get verifyToken(): ?string {
-    return this._verifyToken;
-  }
-
   getUniqueSessionKey(body: MessengerRequestBody): ?string {
     const rawEvent = this._getRawEventsFromRequest(body)[0];
     if (
diff --git a/src/express/registerRoutes.js b/src/express/registerRoutes.js
index 1f0f6dd2..ec507eb4 100644
--- a/src/express/registerRoutes.js
+++ b/src/express/registerRoutes.js
@@ -17,8 +17,7 @@ function registerRoutes(server, bot, config = {}) {
   const middleware = config.webhookMiddleware ? [config.webhookMiddleware] : [];
 
   if (bot.connector.platform === 'messenger') {
-    verifyToken =
-      config.verifyToken || bot.connector.verifyToken || shortid.generate();
+    verifyToken = config.verifyToken || shortid.generate();
     server.get(path, verifyMessengerWebhook({ verifyToken }));
     middleware.unshift(verifyMessengerSignature(bot));
   } else if (bot.connector.platform === 'slack') {
diff --git a/src/koa/registerRoutes.js b/src/koa/registerRoutes.js
index 6a54f1b2..6799adc5 100644
--- a/src/koa/registerRoutes.js
+++ b/src/koa/registerRoutes.js
@@ -20,8 +20,7 @@ function registerRoutes(server, bot, config = {}) {
   const middleware = config.webhookMiddleware ? [config.webhookMiddleware] : [];
 
   if (bot.connector.platform === 'messenger') {
-    verifyToken =
-      config.verifyToken || bot.connector.verifyToken || shortid.generate();
+    verifyToken = config.verifyToken || shortid.generate();
     router.get(path, verifyMessengerWebhook({ verifyToken }));
     middleware.unshift(verifyMessengerSignature(bot));
   } else if (bot.connector.platform === 'slack') {
diff --git a/src/micro/createServer.js b/src/micro/createServer.js
index 11230560..1b13b84c 100644
--- a/src/micro/createServer.js
+++ b/src/micro/createServer.js
@@ -6,8 +6,7 @@ import connectNgrok from '../connectNgrok';
 import createRequestHandler from './createRequestHandler';
 
 function createServer(bot, config = {}) {
-  config.verifyToken =
-    config.verifyToken || bot.connector.verifyToken || shortid.generate();
+  config.verifyToken = config.verifyToken || shortid.generate();
 
   const server = micro(createRequestHandler(bot, config));
 
diff --git a/src/restify/registerRoutes.js b/src/restify/registerRoutes.js
index 5aeff10c..dbbdebce 100644
--- a/src/restify/registerRoutes.js
+++ b/src/restify/registerRoutes.js
@@ -17,8 +17,7 @@ function registerRoutes(server, bot, config = {}) {
   const middleware = config.webhookMiddleware ? [config.webhookMiddleware] : [];
 
   if (bot.connector.platform === 'messenger') {
-    verifyToken =
-      config.verifyToken || bot.connector.verifyToken || shortid.generate();
+    verifyToken = config.verifyToken || shortid.generate();
     server.get(path, verifyMessengerWebhook({ verifyToken }));
     middleware.unshift(verifyMessengerSignature(bot));
   } else if (bot.connector.platform === 'slack') {


Test patch:
diff --git a/src/bot/__tests__/ConsoleBot.spec.js b/src/bot/__tests__/ConsoleBot.spec.js
index 4c121b0e..dae12d91 100644
--- a/src/bot/__tests__/ConsoleBot.spec.js
+++ b/src/bot/__tests__/ConsoleBot.spec.js
@@ -63,6 +63,22 @@ describe('createRuntime', () => {
     expect(process.exit).toBeCalled();
   });
 
+  it('should exit when entering /exit', () => {
+    const bot = new ConsoleBot();
+    const handler = jest.fn();
+
+    bot.onEvent(handler);
+
+    readline.createInterface.mockReturnValue({
+      once: once((string, fn) => fn('/exit')),
+      close: jest.fn(),
+    });
+
+    bot.createRuntime();
+
+    expect(process.exit).toBeCalled();
+  });
+
   it('should call handler with text event when receive random text', async () => {
     const bot = new ConsoleBot();
 
diff --git a/src/bot/__tests__/MessengerConnector.spec.js b/src/bot/__tests__/MessengerConnector.spec.js
index 31891de8..e4217641 100644
--- a/src/bot/__tests__/MessengerConnector.spec.js
+++ b/src/bot/__tests__/MessengerConnector.spec.js
@@ -164,11 +164,10 @@ const webhookTestRequest = {
 };
 
 function setup(
-  { accessToken, appSecret, mapPageToAccessToken, verifyToken } = {
+  { accessToken, appSecret, mapPageToAccessToken } = {
     accessToken: ACCESS_TOKEN,
     appSecret: APP_SECRET,
     mapPageToAccessToken: jest.fn(),
-    verifyToken: undefined,
   }
 ) {
   const mockGraphAPIClient = {
@@ -182,7 +181,6 @@ function setup(
       accessToken,
       appSecret,
       mapPageToAccessToken,
-      verifyToken,
     }),
   };
 }
@@ -204,18 +202,6 @@ describe('#platform', () => {
   });
 });
 
-describe('#verifyToken', () => {
-  it('should be undefined when not be provided', () => {
-    const { connector } = setup();
-    expect(connector.verifyToken).toBe(undefined);
-  });
-
-  it('should equal when be provided', () => {
-    const { connector } = setup({ verifyToken: '1234' });
-    expect(connector.verifyToken).toBe('1234');
-  });
-});
-
 describe('#client', () => {
   it('should be client', () => {
     const { connector, mockGraphAPIClient } = setup();