Repository: Yoctol/bottender (ts)

Issue:
ConsoleBot sendText format
The consoleBot always sendText with `Bot > ${text}\nYou >`
I think that we should support multiple sentences at a time.

The usage is just like: 
```
await context.sendText('Validation failed!', 'Please try again.');
```

And the output will be:
```
Bot > Validation failed!
Bot > Please try again.
You >
```


Pull request:
Use sync mode in ConsoleBot
close #181 

### input
```
await sendText('Hello world!');
await sendText('Good to hear from you.');
```

### output
```
Bot > Hello world!
Bot > Good to hear from you.
You >
```

Patch:
diff --git a/package.json b/package.json
index 7c7a59b8..42d37750 100644
--- a/package.json
+++ b/package.json
@@ -114,6 +114,7 @@
     "jest-junit": "^3.4.1",
     "lint-staged": "^6.0.1",
     "ngrok": "^2.2.25",
+    "once": "^1.4.0",
     "prettier": "^1.10.2",
     "prettier-package-json": "^1.4.0",
     "rimraf": "^2.6.2",
diff --git a/src/bot/ConsoleBot.js b/src/bot/ConsoleBot.js
index 4aebdcf4..c693b403 100644
--- a/src/bot/ConsoleBot.js
+++ b/src/bot/ConsoleBot.js
@@ -7,12 +7,9 @@ import Bot from './Bot';
 import ConsoleConnector from './ConsoleConnector';
 
 export default class ConsoleBot extends Bot {
-  constructor({
-    sessionStore,
-    sync,
-  }: { sessionStore: SessionStore, sync?: boolean } = {}) {
+  constructor({ sessionStore }: { sessionStore: SessionStore } = {}) {
     const connector = new ConsoleConnector();
-    super({ connector, sessionStore, sync });
+    super({ connector, sessionStore, sync: true });
   }
 
   createRuntime() {
@@ -22,19 +19,25 @@ export default class ConsoleBot extends Bot {
       output: process.stdout,
     });
 
-    process.stdout.write('You > ');
-
-    rl.on('line', async (line: string = '') => {
+    const handleLine = async (line: string = '') => {
       if (line.toLowerCase() === '/quit') {
         rl.close();
         process.exit();
       }
 
-      requestHandler({
-        message: {
-          text: line,
-        },
-      });
-    });
+      await Promise.resolve(
+        requestHandler({
+          message: {
+            text: line,
+          },
+        })
+      );
+
+      process.stdout.write('You > ');
+      rl.once('line', handleLine);
+    };
+
+    process.stdout.write('You > ');
+    rl.once('line', handleLine);
   }
 }
diff --git a/src/bot/ConsoleConnector.js b/src/bot/ConsoleConnector.js
index 78073511..89c2498b 100644
--- a/src/bot/ConsoleConnector.js
+++ b/src/bot/ConsoleConnector.js
@@ -22,7 +22,7 @@ export default class ConsoleConnector implements Connector<ConsoleRequestBody> {
   constructor({ client }: ConstructorOptions = {}) {
     this._client = client || {
       sendText: text => {
-        process.stdout.write(`Bot > ${text}\nYou > `);
+        process.stdout.write(`Bot > ${text}\n`);
       },
     };
   }


Test patch:
diff --git a/src/bot/__tests__/ConsoleBot.spec.js b/src/bot/__tests__/ConsoleBot.spec.js
index c61dffc6..a5d4fa38 100644
--- a/src/bot/__tests__/ConsoleBot.spec.js
+++ b/src/bot/__tests__/ConsoleBot.spec.js
@@ -1,5 +1,7 @@
 import readline from 'readline';
 
+import once from 'once';
+
 import ConsoleBot from '../ConsoleBot';
 import ConsoleConnector from '../ConsoleConnector';
 
@@ -28,10 +30,12 @@ describe('createRuntime', () => {
   it('should work', () => {
     const bot = new ConsoleBot();
     const handler = jest.fn();
+
     bot.onEvent(handler);
 
     readline.createInterface.mockReturnValue({
-      on: (string, fn) => fn(),
+      once: once((string, fn) => fn()),
+      close: jest.fn(),
     });
 
     bot.createRuntime();
@@ -46,10 +50,11 @@ describe('createRuntime', () => {
   it('should exit when entering /quit', () => {
     const bot = new ConsoleBot();
     const handler = jest.fn();
+
     bot.onEvent(handler);
 
     readline.createInterface.mockReturnValue({
-      on: (string, fn) => fn('/quit'),
+      once: once((string, fn) => fn('/quit')),
       close: jest.fn(),
     });
 
diff --git a/src/bot/__tests__/ConsoleConnector.spec.js b/src/bot/__tests__/ConsoleConnector.spec.js
index 05fe7283..6006e46e 100644
--- a/src/bot/__tests__/ConsoleConnector.spec.js
+++ b/src/bot/__tests__/ConsoleConnector.spec.js
@@ -51,7 +51,7 @@ describe('#client', () => {
 
       jest.runTimersToTime(0);
 
-      expect(process.stdout.write).toBeCalledWith('Bot > hello\nYou > ');
+      expect(process.stdout.write).toHaveBeenCalledWith('Bot > hello\n');
     });
   });
 });