Skip to content

Commit

Permalink
feat (slack): support slack block kits and view events (#545)
Browse files Browse the repository at this point in the history
* feat (slack): support slack block kits and view events

* test (slack): get slack view channel id in private_metadata
  • Loading branch information
darkbtf authored and chentsulin committed Dec 27, 2019
1 parent 8d5632d commit 4318e10
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 5 deletions.
9 changes: 8 additions & 1 deletion packages/bottender/src/slack/SlackConnector.ts
Expand Up @@ -138,6 +138,11 @@ export default class SlackConnector
return rawEvent.channelId;
}

// For slack modal
if (rawEvent.view && rawEvent.view.privateMetadata) {
return JSON.parse(rawEvent.view.privateMetadata).channelId;
}

// For reaction_added format
if (
rawEvent.item &&
Expand All @@ -159,7 +164,9 @@ export default class SlackConnector
let userFromBody;
if (
rawEvent.type === 'interactive_message' ||
rawEvent.type === 'block_actions'
rawEvent.type === 'block_actions' ||
rawEvent.type === 'view_submission' ||
rawEvent.type === 'view_closed'
) {
userFromBody = rawEvent.user.id;
} else {
Expand Down
13 changes: 11 additions & 2 deletions packages/bottender/src/slack/SlackContext.ts
Expand Up @@ -8,7 +8,7 @@ import Context from '../context/Context';
import Session from '../session/Session';
import { RequestContext } from '../types';

import SlackEvent from './SlackEvent';
import SlackEvent, { UIEvent } from './SlackEvent';

type Options = {
client: SlackOAuthClient;
Expand Down Expand Up @@ -374,7 +374,16 @@ export default class SlackContext extends Context<
_openView(options: SlackTypes.OpenViewOptions): Promise<any> {
this._isHandled = true;

return this._client.views.open(options);
return this._client.views.open({
...options,
view: {
...options.view,
privateMetadata: JSON.stringify({
original: options.view.privateMetadata,
channelId: (this._event.rawEvent as UIEvent).channel.id,
}),
},
});
}

/**
Expand Down
23 changes: 22 additions & 1 deletion packages/bottender/src/slack/SlackEvent.ts
Expand Up @@ -102,10 +102,15 @@ export type BlockActionEvent = UIEvent & {
type: 'block_actions';
};

export type ViewEvent = UIEvent & {
type: 'view_submission' | 'view_closed';
};

export type SlackRawEvent =
| Message
| InteractiveMessageEvent
| BlockActionEvent;
| BlockActionEvent
| ViewEvent;

export default class SlackEvent implements Event<SlackRawEvent> {
_rawEvent: SlackRawEvent;
Expand Down Expand Up @@ -225,6 +230,22 @@ export default class SlackEvent implements Event<SlackRawEvent> {
return this._rawEvent.type === 'block_actions';
}

/**
* Determine if the event is a view submission event.
*
*/
get isViewSubmission(): boolean {
return this._rawEvent.type === 'view_submission';
}

/**
* Determine if the event is a view closed event.
*
*/
get isViewClosed(): boolean {
return this._rawEvent.type === 'view_closed';
}

/**
* Determine if the event is an UI Event (block actions or interactive message)
*
Expand Down
43 changes: 43 additions & 0 deletions packages/bottender/src/slack/__tests__/SlackConnector.spec.ts
Expand Up @@ -100,6 +100,43 @@ const interactiveMessageRequest = {
},
};

const viewSubmissionRequest = {
body: {
type: 'view_submission',
team: { id: 'T02RUPSBS', domain: 'yoctolinfo' },
user: {
id: 'UCL2D708M',
username: 'darkbtf',
name: 'darkbtf',
teamId: 'T02RUPSBS',
},
apiAppId: 'A604E7GSJ',
token: 'zBoHd4fjrvVcVuN9yTmlHMKC',
triggerId: '873508362498.2878808400.763026ca2acb11b3dfbcb85836d1c3d8',
view: {
id: 'VRQQ7JA4T',
teamId: 'T02RUPSBS',
type: 'modal',
blocks: [[Object]],
privateMetadata: '{"channelId":"C02ELGNBH"}',
callbackId: '截止',
state: { values: {} },
hash: '1577340522.d58ea69f',
title: { type: 'plain_text', text: '確認截止?', emoji: true },
clearOnClose: false,
notifyOnClose: false,
close: { type: 'plain_text', text: '取消', emoji: true },
submit: { type: 'plain_text', text: '送出 :boom:', emoji: true },
previousViewId: null,
rootViewId: 'VRQQ7JA4T',
appId: 'A604E7GSJ',
externalId: '',
appInstalledTeamId: 'T02RUPSBS',
botId: 'B618CBATV',
},
},
};

const RtmMessage = {
type: 'message',
channel: 'G7W5WAAAA',
Expand Down Expand Up @@ -184,6 +221,12 @@ describe('#getUniqueSessionKey', () => {
const channelId = connector.getUniqueSessionKey(PinAddedRequest.body);
expect(channelId).toBe('C02ELGNBH');
});

it("extract correct channel id from view event's private_metadata", () => {
const { connector } = setup();
const channelId = connector.getUniqueSessionKey(viewSubmissionRequest.body);
expect(channelId).toBe('C02ELGNBH');
});
});

describe('#updateSession', () => {
Expand Down
5 changes: 4 additions & 1 deletion packages/bottender/src/slack/__tests__/SlackContext.spec.ts
Expand Up @@ -483,7 +483,10 @@ describe('#views.open', () => {

expect(client.views.open).toBeCalledWith({
triggerId: '12345.98765.abcd2358fdea',
view: VIEW_PAYLOAD,
view: {
...VIEW_PAYLOAD,
privateMetadata: '{"original":"Shh it is a secret"}',
},
});
});
});
Expand Down

0 comments on commit 4318e10

Please sign in to comment.