Skip to content

Commit

Permalink
test (slack): get slack view channel id in private_metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
darkbtf committed Dec 26, 2019
1 parent 092768c commit 3865879
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
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
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',
team_id: 'T02RUPSBS',
},
api_app_id: 'A604E7GSJ',
token: 'zBoHd4fjrvVcVuN9yTmlHMKC',
trigger_id: '873508362498.2878808400.763026ca2acb11b3dfbcb85836d1c3d8',
view: {
id: 'VRQQ7JA4T',
team_id: 'T02RUPSBS',
type: 'modal',
blocks: [[Object]],
private_metadata: '{"channelId":"C02ELGNBH"}',
callback_id: '截止',
state: { values: {} },
hash: '1577340522.d58ea69f',
title: { type: 'plain_text', text: '確認截止?', emoji: true },
clear_on_close: false,
notify_on_close: false,
close: { type: 'plain_text', text: '取消', emoji: true },
submit: { type: 'plain_text', text: '送出 :boom:', emoji: true },
previous_view_id: null,
root_view_id: 'VRQQ7JA4T',
app_id: 'A604E7GSJ',
external_id: '',
app_installed_team_id: 'T02RUPSBS',
bot_id: '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 3865879

Please sign in to comment.