Repository: Yoctol/bottender
Pull request #217 (2018-03-22 03:18:01)

export TelegramContext update APIs
- `editMessageText(text [, options])`
- `editMessageCaption(caption [, options])`
- `editMessageReplyMarkup(replyMarkup [, options])`
- `deleteMessage(messageId)`
- `editMessageLiveLocation(location [, options])`
- `stopMessageLiveLocation(options)`
- `forwardMessageFrom(fromChatId, messageId [, options])`
- `forwardMessageTo(toChatId, messageId [, options])`


Patch:
diff --git a/src/context/TelegramContext.js b/src/context/TelegramContext.js
index 8fea5dbd..c2c0270e 100644
--- a/src/context/TelegramContext.js
+++ b/src/context/TelegramContext.js
@@ -224,6 +224,157 @@ class TelegramContext extends Context implements PlatformContext {
     const { result } = await this._client.getChatMember(chatId, userId);
     return result;
   }
+
+  async editMessageText(text: string, options: ?Object) {
+    if (!this._session) {
+      warning(
+        false,
+        'editMessageText: should not be called in context without session'
+      );
+      return;
+    }
+
+    this._isHandled = true;
+
+    const chatId = this._getChatId();
+
+    return this._client.editMessageText(text, { chat_id: chatId, ...options });
+  }
+
+  async editMessageCaption(caption: string, options: ?Object) {
+    if (!this._session) {
+      warning(
+        false,
+        'editMessageCaption: should not be called in context without session'
+      );
+      return;
+    }
+
+    this._isHandled = true;
+
+    const chatId = this._getChatId();
+
+    return this._client.editMessageCaption(caption, {
+      chat_id: chatId,
+      ...options,
+    });
+  }
+
+  async editMessageReplyMarkup(replyMarkup: Object, options: ?Object) {
+    if (!this._session) {
+      warning(
+        false,
+        'editMessageReplyMarkup: should not be called in context without session'
+      );
+      return;
+    }
+
+    this._isHandled = true;
+
+    const chatId = this._getChatId();
+
+    return this._client.editMessageReplyMarkup(replyMarkup, {
+      chat_id: chatId,
+      ...options,
+    });
+  }
+
+  async deleteMessage(messageId: number) {
+    if (!this._session) {
+      warning(
+        false,
+        'deleteMessage: should not be called in context without session'
+      );
+      return;
+    }
+
+    this._isHandled = true;
+
+    const chatId = this._getChatId();
+
+    return this._client.deleteMessage(chatId, messageId);
+  }
+
+  async editMessageLiveLocation(
+    location: { latitude: number, longitude: number },
+    options: ?Object
+  ) {
+    if (!this._session) {
+      warning(
+        false,
+        'editMessageLiveLocation: should not be called in context without session'
+      );
+      return;
+    }
+
+    this._isHandled = true;
+
+    const chatId = this._getChatId();
+
+    return this._client.editMessageLiveLocation(location, {
+      chat_id: chatId,
+      ...options,
+    });
+  }
+
+  async stopMessageLiveLocation(options: ?Object) {
+    if (!this._session) {
+      warning(
+        false,
+        'stopMessageLiveLocation: should not be called in context without session'
+      );
+      return;
+    }
+
+    this._isHandled = true;
+
+    const chatId = this._getChatId();
+
+    return this._client.stopMessageLiveLocation({
+      chat_id: chatId,
+      ...options,
+    });
+  }
+
+  async forwardMessageFrom(
+    fromChatId: string,
+    messageId: number,
+    options: ?Object
+  ) {
+    if (!this._session) {
+      warning(
+        false,
+        'forwardMessageFrom: should not be called in context without session'
+      );
+      return;
+    }
+
+    this._isHandled = true;
+
+    const chatId = this._getChatId();
+
+    return this._client.forwardMessage(chatId, fromChatId, messageId, options);
+  }
+
+  async forwardMessageTo(
+    toChatId: string,
+    messageId: number,
+    options: ?Object
+  ) {
+    if (!this._session) {
+      warning(
+        false,
+        'forwardMessageTo: should not be called in context without session'
+      );
+      return;
+    }
+
+    this._isHandled = true;
+
+    const chatId = this._getChatId();
+
+    return this._client.forwardMessage(toChatId, chatId, messageId, options);
+  }
 }
 
 const sendMethods = [


Test patch:
diff --git a/src/context/__tests__/TelegramContext.spec.js b/src/context/__tests__/TelegramContext.spec.js
index a8ea579d..391980f0 100644
--- a/src/context/__tests__/TelegramContext.spec.js
+++ b/src/context/__tests__/TelegramContext.spec.js
@@ -34,6 +34,13 @@ const createMockTelegramClient = () => ({
   sendGame: jest.fn(),
   setGameScore: jest.fn(),
   getGameHighScores: jest.fn(),
+  editMessageText: jest.fn(),
+  editMessageCaption: jest.fn(),
+  editMessageReplyMarkup: jest.fn(),
+  deleteMessage: jest.fn(),
+  editMessageLiveLocation: jest.fn(),
+  stopMessageLiveLocation: jest.fn(),
+  forwardMessage: jest.fn(),
   kickChatMember: jest.fn(),
   unbanChatMember: jest.fn(),
   restrictChatMember: jest.fn(),
@@ -514,6 +521,193 @@ describe('#getGameHighScores', () => {
   });
 });
 
+describe('#editMessageText', () => {
+  it('should to call client.editMessageText', async () => {
+    const { context, client } = setup();
+
+    await context.editMessageText('new text');
+
+    expect(client.editMessageText).toBeCalledWith('new text', {
+      chat_id: 427770117,
+    });
+  });
+
+  it('should mark context as handled', async () => {
+    const { context } = setup();
+
+    await context.editMessageText('new text');
+
+    expect(context.isHandled).toBe(true);
+  });
+});
+
+describe('#editMessageCaption', () => {
+  it('should to call client.editMessageCaption', async () => {
+    const { context, client } = setup();
+
+    await context.editMessageCaption('new caption');
+
+    expect(client.editMessageCaption).toBeCalledWith('new caption', {
+      chat_id: 427770117,
+    });
+  });
+
+  it('should mark context as handled', async () => {
+    const { context } = setup();
+
+    await context.editMessageCaption('new caption');
+
+    expect(context.isHandled).toBe(true);
+  });
+});
+
+describe('#editMessageReplyMarkup', () => {
+  const markup = {
+    keyboard: [[{ text: 'new_button_1' }, { text: 'new_button_2' }]],
+    resize_keyboard: true,
+    one_time_keyboard: true,
+  };
+
+  it('should to call client.editMessageReplyMarkup', async () => {
+    const { context, client } = setup();
+
+    await context.editMessageReplyMarkup(markup);
+
+    expect(client.editMessageReplyMarkup).toBeCalledWith(markup, {
+      chat_id: 427770117,
+    });
+  });
+
+  it('should mark context as handled', async () => {
+    const { context } = setup();
+
+    await context.editMessageReplyMarkup(markup);
+
+    expect(context.isHandled).toBe(true);
+  });
+});
+
+describe('#deleteMessage', () => {
+  it('should to call client.deleteMessage', async () => {
+    const { context, client } = setup();
+
+    await context.deleteMessage('message_id');
+
+    expect(client.deleteMessage).toBeCalledWith(427770117, 'message_id');
+  });
+
+  it('should mark context as handled', async () => {
+    const { context } = setup();
+
+    await context.deleteMessage('message_id');
+
+    expect(context.isHandled).toBe(true);
+  });
+});
+
+describe('#editMessageLiveLocation', () => {
+  const location = {
+    latitude: 30,
+    longitude: 45,
+  };
+
+  it('should to call client.editMessageLiveLocation', async () => {
+    const { context, client } = setup();
+
+    await context.editMessageLiveLocation(location);
+
+    expect(client.editMessageLiveLocation).toBeCalledWith(location, {
+      chat_id: 427770117,
+    });
+  });
+
+  it('should mark context as handled', async () => {
+    const { context } = setup();
+
+    await context.editMessageLiveLocation(location);
+
+    expect(context.isHandled).toBe(true);
+  });
+});
+
+describe('#stopMessageLiveLocation', () => {
+  it('should to call client.stopMessageLiveLocation', async () => {
+    const { context, client } = setup();
+
+    await context.stopMessageLiveLocation();
+
+    expect(client.stopMessageLiveLocation).toBeCalledWith({
+      chat_id: 427770117,
+    });
+  });
+
+  it('should mark context as handled', async () => {
+    const { context } = setup();
+
+    await context.stopMessageLiveLocation();
+
+    expect(context.isHandled).toBe(true);
+  });
+});
+
+describe('#forwardMessageFrom', () => {
+  it('should to call client.forwardMessage', async () => {
+    const { context, client } = setup();
+
+    await context.forwardMessageFrom(313534466, 'message_id', {
+      disable_notification: true,
+    });
+
+    expect(client.forwardMessage).toBeCalledWith(
+      427770117,
+      313534466,
+      'message_id',
+      {
+        disable_notification: true,
+      }
+    );
+  });
+
+  it('should mark context as handled', async () => {
+    const { context } = setup();
+
+    await context.forwardMessageFrom(313534466, 'message_id', {
+      disable_notification: true,
+    });
+
+    expect(context.isHandled).toBe(true);
+  });
+});
+
+describe('#forwardMessageTo', () => {
+  it('should to call client.forwardMessageTo', async () => {
+    const { context, client } = setup();
+
+    await context.forwardMessageTo(413534466, 'message_id', {
+      disable_notification: true,
+    });
+
+    expect(client.forwardMessage).toBeCalledWith(
+      413534466,
+      427770117,
+      'message_id',
+      {
+        disable_notification: true,
+      }
+    );
+  });
+
+  it('should mark context as handled', async () => {
+    const { context } = setup();
+
+    await context.forwardMessageTo(413534466, 'message_id', {
+      disable_notification: true,
+    });
+
+    expect(context.isHandled).toBe(true);
+  });
+});
+
 describe('#kickChatMember', () => {
   it('should to call client.kickChatMember', async () => {
     const { context, client } = setup();