All files / core/blong-gogo/src/adapter/server/viber viberPromptSendRequestSend.ts

23.63% Statements 13/55
100% Branches 0/0
100% Functions 0/0
23.63% Lines 13/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                                                                                      
import type {IChatAccountMap, IChatPromptParams} from '@feasibleone/blong/types';
import {handler, type IMeta} from '@feasibleone/blong/types';
 
import type {IViberKeyboard} from './keyboard.ts';
 
/**
 * `viber.prompt.send` — the question, with whatever Viber can render for it.
 *
 * A choice or a confirmation becomes keyboard buttons; a free-text question becomes the text a person
 * replies to. A multiple choice is refused: Viber has no checkbox, and answering it as a sequence of
 * single choices would be a different question from the one asked (`@@D-678`).
 */
export default handler(({lib: {error, viberTokenGet, viberBaseGet, viberPromptKeyboard}}) => {
    const errors = error({
        'viber.accountMissing': {
            message: 'No Viber account {account} is configured',
            statusCode: 404,
        },
        'viber.controlUnsupported': {
            message: 'Viber cannot render the {controlKind} control',
            statusCode: 400,
        },
    });

    return function viberPromptSendRequestSend(
        this: {config?: {accounts?: IChatAccountMap; url?: string}},
        params: IChatPromptParams,
        $meta: IMeta,
    ) {
        if (params.prompt.kind === 'multiChoice')
            throw errors['viber.controlUnsupported']({params: {controlKind: 'multiChoice'}}, $meta);
        const token = viberTokenGet(this.config?.accounts, params.account);
        if (!token) throw errors['viber.accountMissing']({params: {account: params.account ?? ''}});
        const keyboard = viberPromptKeyboard(params.prompt) as IViberKeyboard | undefined;
        return {
            url: new URL(
                'send_message',
                viberBaseGet(this.config?.accounts, params.account, this.config?.url),
            ),
            path: 'send_message',
            method: 'POST',
            responseType: 'json',
            headers: {
                'x-viber-auth-token': token,
                'content-type': 'application/json; charset=utf-8',
            },
            json: {
                receiver: params.conversationId,
                type: 'text',
                text: params.prompt.text,
                ...(keyboard && {keyboard}),
            },
        };
    };
});