Repository: Yoctol/bottender
Pull request #562 (2019-12-05 07:30:55)

feat: sendPoll and sendAnimation
telegram context support sendPoll and sendAnimation


Patch:
diff --git a/packages/bottender/src/telegram/TelegramContext.ts b/packages/bottender/src/telegram/TelegramContext.ts
index 99081f8c..2dcb7c77 100644
--- a/packages/bottender/src/telegram/TelegramContext.ts
+++ b/packages/bottender/src/telegram/TelegramContext.ts
@@ -664,6 +664,33 @@ class TelegramContext extends Context<TelegramClient, TelegramEvent>
     return this._client.sendVideo(chatId, video, options);
   }
 
+  async sendAnimation(
+    animation: string,
+    options?: Type.SendAnimationOption
+  ): Promise<Type.Message | null> {
+    if (!this._session) {
+      warning(
+        false,
+        'sendVideo: should not be called in context without session'
+      );
+      return null;
+    }
+
+    this._isHandled = true;
+
+    const chatId = this._getChatId();
+
+    if (chatId === null) {
+      warning(
+        false,
+        'sendVideo: should not be called in context without chatId'
+      );
+      return null;
+    }
+
+    return this._client.sendAnimation(chatId, animation, options);
+  }
+
   async sendVoice(
     voice: string,
     options?: Type.SendVoiceOption
@@ -826,6 +853,34 @@ class TelegramContext extends Context<TelegramClient, TelegramEvent>
     return this._client.sendContact(chatId, requiredOptions, options);
   }
 
+  async sendPoll(
+    question: string,
+    options: string[],
+    otherOptions?: Type.SendPollOption
+  ): Promise<Type.Message | null> {
+    if (!this._session) {
+      warning(
+        false,
+        'sendContact: should not be called in context without session'
+      );
+      return null;
+    }
+
+    this._isHandled = true;
+
+    const chatId = this._getChatId();
+
+    if (chatId === null) {
+      warning(
+        false,
+        'sendContact: should not be called in context without chatId'
+      );
+      return null;
+    }
+
+    return this._client.sendPoll(chatId, question, options, otherOptions);
+  }
+
   async sendChatAction(action: Type.ChatAction): Promise<boolean | null> {
     if (!this._session) {
       warning(


Test patch:
diff --git a/packages/bottender/src/telegram/__tests__/TelegramContext.spec.ts b/packages/bottender/src/telegram/__tests__/TelegramContext.spec.ts
index 7ffc38e9..11143934 100644
--- a/packages/bottender/src/telegram/__tests__/TelegramContext.spec.ts
+++ b/packages/bottender/src/telegram/__tests__/TelegramContext.spec.ts
@@ -229,6 +229,28 @@ describe('#sendVideo', () => {
   });
 });
 
+describe('#sendAnimation', () => {
+  it('should call client.sendAnimation', async () => {
+    const { context, client } = setup();
+
+    await context.sendAnimation('xxx.mp4');
+
+    expect(client.sendAnimation).toBeCalledWith(
+      427770117,
+      'xxx.mp4',
+      undefined
+    );
+  });
+
+  it('should mark context as handled', async () => {
+    const { context } = setup();
+
+    await context.sendAnimation('xxx.mp4');
+
+    expect(context.isHandled).toBe(true);
+  });
+});
+
 describe('#sendVoice', () => {
   it('should call client.sendVoice', async () => {
     const { context, client } = setup();
@@ -361,6 +383,35 @@ describe('#sendContact', () => {
   });
 });
 
+describe('#sendPoll', () => {
+  it('should to call client.sendPoll', async () => {
+    const { context, client } = setup();
+
+    const question = 'question';
+    const options = ['a', 'b'];
+
+    await context.sendPoll(question, options);
+
+    expect(client.sendPoll).toBeCalledWith(
+      427770117,
+      question,
+      options,
+      undefined
+    );
+  });
+
+  it('should mark context as handled', async () => {
+    const { context } = setup();
+
+    const question = 'question';
+    const options = ['a', 'b'];
+
+    await context.sendPoll(question, options);
+
+    expect(context.isHandled).toBe(true);
+  });
+});
+
 describe('#sendChatAction', () => {
   it('should to call client.sendChatAction', async () => {
     const { context, client } = setup();