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

24.44% Statements 11/45
100% Branches 0/0
100% Functions 0/0
24.44% Lines 11/45

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 461x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                      
import type {IChatAccountMap, IChatPromptParams} from '@feasibleone/blong/types';
import {handler} from '@feasibleone/blong/types';
 
/**
 * `slack.prompt.send` — the question, as a message.
 *
 * A choice or a confirmation is answered in the message itself; a multiple choice or a free-text
 * question gets one button that asks for the form (`slack.prompt.form`), because Slack renders those
 * controls in a modal only (`@@D-678`).
 */
export default handler(({lib: {error, slackTokenGet, slackBaseGet, slackPromptBlocks}}) => {
    const errors = error({
        'slack.accountMissing': {
            message: 'No Slack account {account} is configured',
            statusCode: 404,
        },
    });

    return function slackPromptSendRequestSend(
        this: {config?: {accounts?: IChatAccountMap; url?: string}},
        params: IChatPromptParams,
    ) {
        const token = slackTokenGet(this.config?.accounts, params.account);
        if (!token) throw errors['slack.accountMissing']({params: {account: params.account ?? ''}});
        return {
            url: new URL(
                'chat.postMessage',
                slackBaseGet(this.config?.accounts, params.account, this.config?.url),
            ),
            path: 'chat.postMessage',
            method: 'POST',
            responseType: 'json',
            headers: {
                authorization: `Bearer ${token}`,
                'content-type': 'application/json; charset=utf-8',
            },
            json: {
                channel: params.conversationId,
                mrkdwn: true,
                text: params.prompt.text,
                blocks: slackPromptBlocks(params.prompt),
            },
        };
    };
});