Repository: Yoctol/bottender (ts)

Issue:
slack bot should reply in thread
when the event is from a message in a thread, sendText should send to the thread, not to the channel

Pull request description:
reply to thread events in thread
fix #129 

Patch:
diff --git a/src/context/SlackContext.js b/src/context/SlackContext.js
index 85f89d37..56ae1677 100644
--- a/src/context/SlackContext.js
+++ b/src/context/SlackContext.js
@@ -58,6 +58,7 @@ export default class SlackContext extends Context implements PlatformContext {
     this._isHandled = true;
 
     return this._client.postMessage(channelId, text, {
+      thread_ts: this._event.rawEvent.thread_ts,
       ...options,
     });
   }
diff --git a/src/context/SlackEvent.js b/src/context/SlackEvent.js
index b352ff81..a7328288 100644
--- a/src/context/SlackEvent.js
+++ b/src/context/SlackEvent.js
@@ -66,6 +66,7 @@ export type Message = {
   user: string,
   text: string,
   ts: string,
+  thread_ts?: string,
 };
 
 export type InteractiveMessageEvent = {
@@ -91,6 +92,7 @@ export type InteractiveMessageEvent = {
   original_message: Message,
   response_url: string,
   trigger_id: string,
+  thread_ts?: string,
 };
 
 export type SlackRawEvent = Message | InteractiveMessageEvent;


Test patch:
diff --git a/src/context/__tests__/SlackContext.spec.js b/src/context/__tests__/SlackContext.spec.js
index 6868e4d3..34a553dd 100644
--- a/src/context/__tests__/SlackContext.spec.js
+++ b/src/context/__tests__/SlackContext.spec.js
@@ -16,7 +16,7 @@ beforeEach(() => {
   /* eslint-enable global-require */
 });
 
-const rawEvent = {
+const messageRawEvent = {
   type: 'message',
   user: 'U13AGSN1X',
   text: 'Experience is the best teacher.',
@@ -25,6 +25,17 @@ const rawEvent = {
   event_ts: '1500435914.425136',
 };
 
+const threadRawEvent = {
+  type: 'message',
+  channel: 'C6A9RJJ3F',
+  user: 'U056KAAAA',
+  text: 'in a thread',
+  ts: '1515480368.000083',
+  source_team: 'T056KAAAA',
+  team: 'T056KAAAA',
+  thread_ts: '1515479974.000115',
+};
+
 const createMockSlackClient = () => ({
   postMessage: jest.fn(),
 });
@@ -37,7 +48,9 @@ const userSession = {
     id: 'C6A9RJJ3F',
   },
 };
-const setup = ({ session } = { session: userSession }) => {
+const setup = ({ session: _session, rawEvent: _rawEvent } = {}) => {
+  const session = _session === undefined ? userSession : _session;
+  const rawEvent = _rawEvent === undefined ? messageRawEvent : _rawEvent;
   const client = createMockSlackClient();
   const args = {
     client,
@@ -94,6 +107,16 @@ describe('#postMessage', () => {
     expect(client.postMessage).toBeCalledWith('C6A9RJJ3F', 'xxx.com', {});
   });
 
+  it('should set thread_ts if event is in a thread', async () => {
+    const { context, client } = setup({ rawEvent: threadRawEvent });
+
+    await context.postMessage('xxx.com');
+
+    expect(client.postMessage).toBeCalledWith('C6A9RJJ3F', 'xxx.com', {
+      thread_ts: '1515479974.000115',
+    });
+  });
+
   it('should support overwrite options', async () => {
     const { context, client } = setup();