Repository: Yoctol/bottender
Pull request #165 (2018-01-18 04:39:03)

Implement context.answerInlineQuery for Telegram
```js
context.answerInlineQuery(
  [
    {
      type: 'photo',
      id: 'UNIQUE_ID',
      photo_file_id: 'FILE_ID',
      title: 'PHOTO_TITLE',
    },
    {
      type: 'audio',
      id: 'UNIQUE_ID',
      audio_file_id: 'FILE_ID',
      caption: 'AUDIO_TITLE',
    },
  ],
  {
    cache_time: 1000,
  }
);
```


Patch:
diff --git a/src/context/TelegramContext.js b/src/context/TelegramContext.js
index 5e07b10d..16509fb4 100644
--- a/src/context/TelegramContext.js
+++ b/src/context/TelegramContext.js
@@ -100,6 +100,22 @@ class TelegramContext extends Context implements PlatformContext {
     }
     return null;
   }
+
+  async answerInlineQuery(results: Array<Object>, options?: {}) {
+    if (!this._event.isInlineQuery) {
+      warning(
+        false,
+        'answerInlineQuery: should only be called to answer InlineQuery event'
+      );
+      return;
+    }
+
+    this._isHandled = true;
+
+    const inlineQueryId = (this._event.inlineQuery: any).id;
+
+    return this._client.answerInlineQuery(inlineQueryId, results, options);
+  }
 }
 
 const sendMethods = [


Test patch:
diff --git a/src/context/__tests__/TelegramContext.spec.js b/src/context/__tests__/TelegramContext.spec.js
index fa6d4b10..f176312c 100644
--- a/src/context/__tests__/TelegramContext.spec.js
+++ b/src/context/__tests__/TelegramContext.spec.js
@@ -34,6 +34,7 @@ const createMockTelegramClient = () => ({
   sendGame: jest.fn(),
   setGameScore: jest.fn(),
   getGameHighScores: jest.fn(),
+  answerInlineQuery: jest.fn(),
 });
 
 const _rawEvent = {
@@ -145,13 +146,7 @@ describe('#sendMessage', () => {
   });
 
   it('should not call send method if dont have session', async () => {
-    const { client, event } = setup();
-    const args = {
-      client,
-      event,
-      session: undefined,
-    };
-    const context = new TelegramContext(args);
+    const { context, client } = setup({ session: null });
 
     await context.sendMessage('xxx.com');
 
@@ -498,6 +493,127 @@ describe('#getGameHighScores', () => {
   });
 });
 
+describe('#answerInlineQuery', () => {
+  const inlineQuery = {
+    update_id: 141921700,
+    inline_query: {
+      id: '1837258670654537434',
+      from: {
+        id: 427770117,
+        is_bot: false,
+        first_name: 'user_first',
+        last_name: 'user_last',
+        language_code: 'en',
+      },
+      query: '123',
+      offset: '',
+    },
+  };
+  it('should to call client.answerInlineQuery', async () => {
+    const { context, client } = setup({ rawEvent: inlineQuery });
+
+    const response = {
+      ok: true,
+    };
+
+    client.answerInlineQuery.mockReturnValue(Promise.resolve(response));
+
+    const result = await context.answerInlineQuery(
+      [
+        {
+          type: 'photo',
+          id: 'UNIQUE_ID',
+          photo_file_id: 'FILE_ID',
+          title: 'PHOTO_TITLE',
+        },
+        {
+          type: 'audio',
+          id: 'UNIQUE_ID',
+          audio_file_id: 'FILE_ID',
+          caption: 'AUDIO_TITLE',
+        },
+      ],
+      {
+        cache_time: 1000,
+      }
+    );
+
+    expect(client.answerInlineQuery).toBeCalledWith(
+      '1837258670654537434',
+      [
+        {
+          type: 'photo',
+          id: 'UNIQUE_ID',
+          photo_file_id: 'FILE_ID',
+          title: 'PHOTO_TITLE',
+        },
+        {
+          type: 'audio',
+          id: 'UNIQUE_ID',
+          audio_file_id: 'FILE_ID',
+          caption: 'AUDIO_TITLE',
+        },
+      ],
+      {
+        cache_time: 1000,
+      }
+    );
+    expect(result).toEqual(response);
+  });
+
+  it('should mark context as handled', async () => {
+    const { context } = setup({ rawEvent: inlineQuery });
+
+    await context.answerInlineQuery(
+      [
+        {
+          type: 'photo',
+          id: 'UNIQUE_ID',
+          photo_file_id: 'FILE_ID',
+          title: 'PHOTO_TITLE',
+        },
+        {
+          type: 'audio',
+          id: 'UNIQUE_ID',
+          audio_file_id: 'FILE_ID',
+          caption: 'AUDIO_TITLE',
+        },
+      ],
+      {
+        cache_time: 1000,
+      }
+    );
+
+    expect(context.isHandled).toBe(true);
+  });
+
+  it('should not call answerInlineQuery method if event type is not InlineQuery', async () => {
+    const { context, client } = setup();
+
+    await context.answerInlineQuery(
+      [
+        {
+          type: 'photo',
+          id: 'UNIQUE_ID',
+          photo_file_id: 'FILE_ID',
+          title: 'PHOTO_TITLE',
+        },
+        {
+          type: 'audio',
+          id: 'UNIQUE_ID',
+          audio_file_id: 'FILE_ID',
+          caption: 'AUDIO_TITLE',
+        },
+      ],
+      {
+        cache_time: 1000,
+      }
+    );
+
+    expect(client.answerInlineQuery).not.toBeCalled();
+  });
+});
+
 describe('#typing', () => {
   it('avoid delay 0', async () => {
     const { context } = setup();