Repository: Yoctol/bottender
Pull request #329 (2018-08-26 16:56:50)

get correct channel id from more slack event format
Now it handles following format

```js
{
  channel: { 
    id: 'channel_id',
  }
}
```

```js
{
  channel_id: 'channel_id',
}
```

```js
{
  channel: 'channel_id',
}
```

```js
{
  item: { 
    channel: 'channel_id',
  }
}
```


Patch:
diff --git a/src/bot/SlackConnector.js b/src/bot/SlackConnector.js
index aac28308..27cb1778 100644
--- a/src/bot/SlackConnector.js
+++ b/src/bot/SlackConnector.js
@@ -61,9 +61,11 @@ export default class SlackConnector implements Connector<SlackRequestBody> {
     if (body.event) {
       return (((body: any): EventsAPIBody).event: Message);
     }
+
     if (body.payload && typeof body.payload === 'string') {
       return (JSON.parse(body.payload): InteractiveMessageEvent);
     }
+
     // for RTM WebSocket messages
     return ((body: any): Message);
   }
@@ -85,12 +87,32 @@ export default class SlackConnector implements Connector<SlackRequestBody> {
   }
 
   getUniqueSessionKey(body: SlackRequestBody): string {
-    const rawEvent = this._getRawEventFromRequest(body);
+    // FIXME: define types for every slack events
+    const rawEvent = (this._getRawEventFromRequest(body): any);
 
-    if (rawEvent.type === 'interactive_message') {
+    // For interactive_message format
+    if (
+      rawEvent.channel &&
+      typeof rawEvent.channel === 'object' &&
+      rawEvent.channel.id
+    ) {
       return rawEvent.channel.id;
     }
 
+    // For pin_added format
+    if (rawEvent.channel_id) {
+      return rawEvent.channel_id;
+    }
+
+    // For reaction_added format
+    if (
+      rawEvent.item &&
+      typeof rawEvent.item === 'object' &&
+      typeof rawEvent.item.channel === 'string'
+    ) {
+      return rawEvent.item.channel;
+    }
+
     return ((rawEvent: any): Message).channel;
   }
 


Test patch:
diff --git a/src/bot/__tests__/SlackConnector.spec.js b/src/bot/__tests__/SlackConnector.spec.js
index b847d30e..b766ec00 100644
--- a/src/bot/__tests__/SlackConnector.spec.js
+++ b/src/bot/__tests__/SlackConnector.spec.js
@@ -51,6 +51,49 @@ const botRequest = {
   },
 };
 
+const ReactionAddedRequest = {
+  body: {
+    token: 'xxxxxxxxxxxxxxxxxxxxxxxxxxx',
+    team_id: 'T02R00000',
+    api_app_id: 'A6A00000',
+    event: {
+      type: 'reaction_added',
+      user: 'U024BE7LH',
+      reaction: 'thumbsup',
+      item_user: 'U0G9QF9C6',
+      item: {
+        type: 'message',
+        channel: 'C0G9QF9GZ',
+        ts: '1360782400.498405',
+      },
+      event_ts: '1360782804.083113',
+    },
+    type: 'event_callback',
+    authed_users: ['U6AK00000'],
+    event_id: 'Ev6BEYTAK0',
+    event_time: 1500435914,
+  },
+};
+
+const PinAddedRequest = {
+  body: {
+    token: 'xxxxxxxxxxxxxxxxxxxxxxxxxxx',
+    team_id: 'T02R00000',
+    api_app_id: 'A6A00000',
+    event: {
+      type: 'pin_added',
+      user: 'U024BE7LH',
+      channel_id: 'C02ELGNBH',
+      item: {},
+      event_ts: '1360782804.083113',
+    },
+    type: 'event_callback',
+    authed_users: ['U6AK00000'],
+    event_id: 'Ev6BEYTAK0',
+    event_time: 1500435914,
+  },
+};
+
 const interactiveMessageRequest = {
   body: {
     payload:
@@ -123,6 +166,18 @@ describe('#getUniqueSessionKey', () => {
     const channelId = connector.getUniqueSessionKey(RtmMessage);
     expect(channelId).toBe('G7W5WAAAA');
   });
+
+  it('extract correct channel id from reaction_added event', () => {
+    const { connector } = setup();
+    const channelId = connector.getUniqueSessionKey(ReactionAddedRequest.body);
+    expect(channelId).toBe('C0G9QF9GZ');
+  });
+
+  it('extract correct channel id from pin_added event', () => {
+    const { connector } = setup();
+    const channelId = connector.getUniqueSessionKey(PinAddedRequest.body);
+    expect(channelId).toBe('C02ELGNBH');
+  });
 });
 
 describe('#updateSession', () => {