Skip to content

Commit

Permalink
Fix #534 respond support in view_submission listeners (it works only …
Browse files Browse the repository at this point in the history
…when response_url_enabled is true) (#889)
  • Loading branch information
seratch authored Apr 21, 2021
1 parent 22f938e commit 3db8da7
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
45 changes: 45 additions & 0 deletions src/App.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1469,6 +1469,51 @@ describe('App', () => {
ack: noop,
});

// Assert
assert.equal(fakeAxiosPost.callCount, 1);
// Assert that each call to fakeAxiosPost had the right arguments
assert(fakeAxiosPost.calledWith(responseUrl, responseObject));
});
it('should be able to use respond for view_submission payloads', async () => {
// Arrange
const responseObject = { text: 'response' };
const responseUrl = 'https://fake.slack/response_url';
const fakeAxiosPost = sinon.fake.resolves({});
const overrides = buildOverrides([withNoopWebClient(), withAxiosPost(fakeAxiosPost)]);
const App = await importApp(overrides); // eslint-disable-line @typescript-eslint/naming-convention, no-underscore-dangle, id-blacklist, id-match

// Act
const app = new App({ receiver: fakeReceiver, authorize: sinon.fake.resolves(dummyAuthorizationResult) });
app.view('view-id', async ({ respond }) => {
await respond(responseObject);
});
app.error(fakeErrorHandler);
await fakeReceiver.sendEvent({
ack: noop,
body: {
type: 'view_submission',
team: {},
user: {},
view: {
id: 'V111',
type: 'modal',
callback_id: 'view-id',
state: {},
title: {},
close: {},
submit: {},
},
response_urls: [
{
block_id: 'b',
action_id: 'a',
channel_id: 'C111',
response_url: 'https://fake.slack/response_url',
},
],
},
});

// Assert
assert.equal(fakeAxiosPost.callCount, 1);
// Assert that each call to fakeAxiosPost had the right arguments
Expand Down
21 changes: 15 additions & 6 deletions src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { SecureContextOptions } from 'tls';
import util from 'util';
import { WebClient, ChatPostMessageArguments, addAppMetadata, WebClientOptions } from '@slack/web-api';
import { Logger, LogLevel, ConsoleLogger } from '@slack/logger';
import axios, { AxiosInstance } from 'axios';
import axios, { AxiosInstance, AxiosResponse } from 'axios';
import SocketModeReceiver from './receivers/SocketModeReceiver';
import HTTPReceiver, { HTTPReceiverOptions } from './receivers/HTTPReceiver';
import {
Expand Down Expand Up @@ -765,11 +765,10 @@ export default class App {

// Set respond() utility
if (body.response_url) {
listenerArgs.respond = (response: string | RespondArguments): Promise<any> => {
const validResponse: RespondArguments = typeof response === 'string' ? { text: response } : response;

return this.axios.post(body.response_url, validResponse);
};
listenerArgs.respond = buildRespondFn(this.axios, body.response_url);
} else if (typeof body.response_urls !== 'undefined' && body.response_urls.length > 0) {
// This can exist only when view_submission payloads - response_url_enabled: true
listenerArgs.respond = buildRespondFn(this.axios, body.response_urls[0].response_url);
}

// Set ack() utility
Expand Down Expand Up @@ -1124,6 +1123,16 @@ function selectToken(context: Context): string | undefined {
return context.botToken !== undefined ? context.botToken : context.userToken;
}

function buildRespondFn(
axiosInstance: AxiosInstance,
response_url: string,
): (response: string | RespondArguments) => Promise<AxiosResponse> {
return async (message: string | RespondArguments) => {
const normalizedArgs: RespondArguments = typeof message === 'string' ? { text: message } : message;
return axiosInstance.post(response_url, normalizedArgs);
};
}

// ----------------------------
// For listener registration methods

Expand Down
3 changes: 2 additions & 1 deletion src/types/view/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { View, PlainTextElement } from '@slack/types';
import { StringIndexed } from '../helpers';
import { AckFn } from '../utilities';
import { AckFn, RespondFn } from '../utilities';

/**
* Known view action types
Expand All @@ -19,6 +19,7 @@ export interface SlackViewMiddlewareArgs<ViewActionType extends SlackViewAction
view: this['payload'];
body: ViewActionType;
ack: ViewAckFn<ViewActionType>;
respond: RespondFn;
}

interface PlainTextElementOutput {
Expand Down

0 comments on commit 3db8da7

Please sign in to comment.