Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat (slack): support slack block kits and view events #545

Merged
merged 2 commits into from Dec 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -9,7 +9,7 @@ import Session from '../session/Session';
import { PlatformContext } from '../context/PlatformContext';
import { RequestContext } from '../types';

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

type Options = {
client: SlackOAuthClient;
Expand Down Expand Up @@ -373,7 +373,16 @@ export default class SlackContext extends Context<SlackOAuthClient, SlackEvent>
_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