All files / core/blong-gogo/src/adapter/server/slack slackPromptFormRequestSend.ts

32.72% Statements 18/55
100% Branches 0/0
100% Functions 0/0
32.72% Lines 18/55

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 561x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                            
import type {IChatAccountMap, IChatPrompt} from '@feasibleone/blong/types';
import {handler} from '@feasibleone/blong/types';
 
/**
 * `slack.prompt.form` — the modal a multiple choice or a free-text question is answered in.
 *
 * `trigger_id` is the part that cannot be invented: Slack lets a modal open only within three
 * seconds of an interaction, so the modal is opened from the click on the message's button. The realm
 * receives that click as `kind: 'prompt.form'` and calls this method with the trigger it carried.
 */
export interface ISlackFormParams {
    account?: string;
    conversationId: string;
    prompt: IChatPrompt;
    triggerId: string;
}
 
export default handler(({lib: {error, slackTokenGet, slackBaseGet, slackPromptView}}) => {
    const errors = error({
        'slack.accountMissing': {
            message: 'No Slack account {account} is configured',
            statusCode: 404,
        },
        'slack.triggerMissing': {
            message: 'Opening a Slack form needs the interaction trigger it was asked from',
            statusCode: 400,
        },
    });

    return function slackPromptFormRequestSend(
        this: {config?: {accounts?: IChatAccountMap; url?: string}},
        params: ISlackFormParams,
    ) {
        if (!params.triggerId) throw errors['slack.triggerMissing']({});
        const token = slackTokenGet(this.config?.accounts, params.account);
        if (!token) throw errors['slack.accountMissing']({params: {account: params.account ?? ''}});
        return {
            url: new URL(
                'views.open',
                slackBaseGet(this.config?.accounts, params.account, this.config?.url),
            ),
            path: 'views.open',
            method: 'POST',
            responseType: 'json',
            headers: {
                authorization: `Bearer ${token}`,
                'content-type': 'application/json; charset=utf-8',
            },
            json: {
                trigger_id: params.triggerId,
                view: slackPromptView(params.prompt, params.conversationId),
            },
        };
    };
});