/* eslint-disable consistent-return */ import Table from 'cli-table3'; import chalk from 'chalk'; import invariant from 'invariant'; import { MessengerClient } from 'messaging-api-messenger'; import getChannelConfig from '../../../shared/getChannelConfig'; import getSubArgs from '../sh/utils/getSubArgs'; import { Channel } from '../../../types'; import { CliContext } from '../..'; import { bold, error, print } from '../../../shared/log'; const help = () => { console.log(` bottender messenger persona [option] ${chalk.dim('Commands:')} list List all personas. create Create a new persona with name and profile picture URL. get Get persona by persona ID. del, delete Delete persona by persona ID. ${chalk.dim('Options:')} --name Specify persona's name when create --pic Specify persona's profile image URL when create --id Specify persona's ID to get or delete ${chalk.dim('Examples:')} ${chalk.dim('-')} Create a new persona ${chalk.cyan( '$ bottender messenger persona create --name --pic ' )} ${chalk.dim('-')} Get persona by ID ${chalk.cyan('$ bottender messenger persona get --id ')} ${chalk.dim('-')} Delete persona with specific access token ${chalk.cyan('$ bottender messenger persona delete --id ')} `); }; export async function createPersona(ctx: CliContext): Promise { const argv = getSubArgs(ctx.argv, { '--name': String, '--pic': String, }); const personaName = argv['--name']; const personaUrl = argv['--pic']; try { const config = getChannelConfig(Channel.Messenger); const { accessToken } = config; invariant( accessToken, '`accessToken` is not found in the `bottender.config.js` file' ); invariant( personaName, '`name` is required but not found. Use --name to specify persona name' ); invariant( personaUrl, '`pic` is required but not found. Use --pic to specify persona profile picture URL' ); const client = new MessengerClient({ accessToken, }); const persona = { name: personaName as string, profilePictureUrl: personaUrl as string, }; const personaID = await client.createPersona(persona); print(`Successfully create ${bold('persona')} ${bold(personaID.id)}`); } catch (err) { error(`Failed to create ${bold('persona')}`); if (err.response) { error(`status: ${bold(err.response.status)}`); if (err.response.data) { error(`data: ${bold(JSON.stringify(err.response.data, null, 2))}`); } } else { error(err.message); } return process.exit(1); } } export async function listPersona(_: CliContext): Promise { try { const config = getChannelConfig(Channel.Messenger); const { accessToken } = config; invariant( accessToken, '`accessToken` is not found in the `bottender.config.js` file' ); const client = new MessengerClient({ accessToken, }); const personas = await client.getAllPersonas(); if (personas.length !== 0) { print('Personas'); const table = new Table({ head: ['id', 'name', 'image URL'], colWidths: [30, 30, 30], }); personas.forEach((item) => { table.push([item.id, item.name, item.profilePictureUrl] as any); }); console.log(table.toString()); // eslint-disable-line no-console } else { print('No personas are found.'); } } catch (err) { error(`Failed to list ${bold('personas')}`); if (err.response) { error(`status: ${bold(err.response.status)}`); if (err.response.data) { error(`data: ${bold(JSON.stringify(err.response.data, null, 2))}`); } } else { error(err.message); } return process.exit(1); } } export async function getPersona(ctx: CliContext): Promise { const argv = getSubArgs(ctx.argv, { '--id': String, }); const personaId = argv['--id']; try { const config = getChannelConfig(Channel.Messenger); const { accessToken } = config; invariant( accessToken, '`accessToken` is not found in the `bottender.config.js` file' ); invariant( personaId, '`id` is required but not found. Use --id to specify persona id' ); const client = new MessengerClient({ accessToken, }); const persona = await client.getPersona(personaId as string); if (persona !== undefined) { print(`Information of persona ${bold(personaId as string)}:`); print(`Name: ${bold(persona.name)}`); print(`Profile picture: ${bold(persona.profilePictureUrl)}`); } else { print(`Cannot get persona of ID ${bold(personaId as string)}`); } } catch (err) { error( `Failed to get ${bold('persona')} of ID ${bold(personaId as string)}` ); if (err.response) { error(`status: ${bold(err.response.status)}`); if (err.response.data) { error(`data: ${bold(JSON.stringify(err.response.data, null, 2))}`); } } else { error(err.message); } return process.exit(1); } } export async function deletePersona(ctx: CliContext): Promise { const argv = getSubArgs(ctx.argv, { '--id': String, }); const personaId = argv['--id']; try { const config = getChannelConfig(Channel.Messenger); const { accessToken } = config; invariant( accessToken, '`accessToken` is not found in the `bottender.config.js` file' ); invariant( personaId, '`id` is required but not found. Use --id to specify persona id' ); const client = new MessengerClient({ accessToken, }); const res = await client.deletePersona(personaId as string); if (res.success === true || res.success === 'true') { print(`Sucessfully delete persona of ID ${bold(personaId as string)}`); } else { print(`Cannot get persona of ID ${bold(personaId as string)}`); } } catch (err) { error( `Failed to delete ${bold('persona')} of ID ${bold(personaId as string)}` ); if (err.response) { error(`status: ${bold(err.response.status)}`); if (err.response.data) { error(`data: ${bold(JSON.stringify(err.response.data, null, 2))}`); } } else { error(err.message); } return process.exit(1); } } export default async function main(ctx: CliContext) { const subcommand = ctx.argv._[2]; switch (subcommand) { case 'create': return createPersona(ctx); case 'list': return listPersona(ctx); case 'get': return getPersona(ctx); case 'delete': case 'del': return deletePersona(ctx); default: error(`Please specify a valid subcommand: create, list, get, delete`); help(); } }