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

29.35% Statements 32/109
100% Branches 0/0
100% Functions 0/0
29.35% Lines 32/109

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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 1101x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                                                                                            
import type {IChatAccountMap} from '@feasibleone/blong/types';
import {adapter, type Adapter} from '@feasibleone/blong/types';
import got from 'got';
 
import {chatPublicUrl} from '../../chatAccounts.ts';
import {NGROK_AGENT_URL, ngrokPublicUrl, ngrokTunnelsGet} from '../impl/ngrokTunnels.ts';
 
/**
 * viber.ts — the Viber port.
 *
 * The same two directions as Slack, against a smaller API: `send_message` posts, `set_webhook` names
 * the address messages arrive at, and a request is verified by an HMAC of its own bytes keyed by the
 * bot's auth token (`@@D-676`). Viber renders keyboard buttons rather than a form, so the capability
 * that is missing is refused there rather than imitated (`@@D-678`).
 *
 * The webhook is registered from `ready` rather than from a deployment step: the address is the one
 * this process just listened on, and an ngrok hostname changes between runs more often than anything
 * else here (`@@D-677`).
 */
export interface IConfig {
    url?: string;
    path?: string;
    server?: {host?: string; port?: number};
    /** Where the ngrok agent answers, when it is not the default `127.0.0.1:4040`. */
    ngrokAgentUrl?: string;
    accounts?: IChatAccountMap;
}
 
/** The events a bridge needs; anything more is a subscription nobody reads. */
const EVENT_TYPES = ['message', 'conversation_started', 'delivered', 'seen', 'failed'];
 
export default adapter<IConfig>(() => ({
    extends: 'adapter.webhook',
    activation: {
        default: {
            type: 'viber',
            url: 'https://chatapi.viber.com/pa/',
            listen: true,
            path: '/chat/viber/{appId}/{clientId}',
            hook: 'viberIn',
            namespace: 'viber',
            // Viber also wants its answer quickly, and it wants `{status: 0}` in the body.
            mode: 'server',
            server: {port: 8096},
            response: {code: 200, body: {status: 0}},
            destination: 'chat.in.receive',
            imports: [/\.viber$/],
            accounts: {},
        },
    },
    /**
     * Register the public address of every account that names one.
     *
     * A failure is logged rather than thrown: a process started before the bot exists, or with a
     * hostname that has not been pointed at it yet, should still serve what it can, and the next
     * restart retries the registration.
     */
    async ready(this: Adapter<IConfig>) {
        const result = await super.ready();
        const cfg = this.config as unknown as IConfig;
        const base = cfg.url ?? 'https://chatapi.viber.com/pa/';
        // The tunnel that forwards this listener, asked once: an ngrok hostname changes more often
        // than anything else in a local run, and this is what keeps `set_webhook` in step with it.
        const discovered = cfg.server?.port
            ? ngrokPublicUrl(
                  await ngrokTunnelsGet(cfg.ngrokAgentUrl ?? NGROK_AGENT_URL),
                  cfg.server.port,
              )
            : undefined;
        await Promise.all(
            Object.entries(cfg.accounts ?? {}).map(async ([name, account]) => {
                if (!account.authToken) return;
                // `webhookUrl` is the address outright, `ngrokUrl` a fixed base, and an account that
                // names neither takes the agent's answer — unless it talks to its own `url`, which
                // means it is a simulator and nothing publishes it.
                const publicBase = account.ngrokUrl ?? (account.url ? undefined : discovered);
                const webhookUrl =
                    account.webhookUrl ??
                    (publicBase && cfg.path
                        ? chatPublicUrl(cfg.path, publicBase, account)
                        : undefined);
                if (!webhookUrl) return;
                try {
                    const answer = await got.post(new URL('set_webhook', base), {
                        json: {
                            auth_token: account.authToken,
                            url: webhookUrl,
                            event_types: EVENT_TYPES,
                        },
                        responseType: 'json',
                        throwHttpErrors: false,
                    });
                    const body = answer.body as {status?: number; status_message?: string};
                    if (body?.status !== 0)
                        this.signal?.frameworkViberSendRefused?.({
                            account: name,
                            status: body?.status,
                            reason: body?.status_message,
                        });
                } catch (error) {
                    this.signal?.frameworkViberSendFailed?.({
                        error: error instanceof Error ? error.message : String(error),
                    });
                }
            }),
        );
        return result;
    },
}));