import * as program from "commander" ; import { Global } from "../lib/core/global" ; import { VirtualDeviceClient } from "../lib/external/virtual-device" ; import { BstStatistics , BstCommand } from "../lib/statistics/bst-statistics" ; import { LoggingHelper } from "../lib/core/logging-helper" ; program . version ( Global . version ( ) ) ; Global . initializeCLI ( ) . then ( ( ) => { program . usage ( "[options] <utterance>" ) . option ( "-l, --locale <locale>" , "The locale to use for the virtual device (en-US, en-GB, de-DE, etc.)" ) . option ( "-t, --token <token>" , `The token for interacting with your virtual device - get it here:

        \thttps://${ Global . SpokesDashboardHost }
        \t(Bespoken Dashboard account required for use of this feature)\n` ) . option ( "--voiceID <voiceID>" , "The AWS Polly voice ID to use for generating speech" ) . description ( "Speaks to your virtual Alexa device" ) . action ( async function ( ) { let utterance = "" ; for ( let i = 0 ; i < program . args . length ; i ++ ) { let arg = program . args [ i ] ; if ( typeof arg !== "string" ) { break ; } if ( utterance . length > 0 ) { utterance += " " ; } utterance += arg ; } const options = program ; const token = options . token ; const locale = options . locale ; const voiceID = options . voiceID ; let virtualDeviceResponse ; try { virtualDeviceResponse = await VirtualDeviceClient . speak ( utterance , token , locale , voiceID ) ; } catch ( ex ) { if ( ex . message === "Token Required" ) { console . log ( "You need a token for this option to work, get it here:" ) ; console . log ( ) ; console . log ( "\thttps://" + Global . SpokesDashboardHost ) ; console . log ( "\t(Bespoken Dashboard account required for use of this feature)" ) ; console . log ( ) ; console . log ( "Then try again with:" ) ; console . log ( ) ; console . log ( "\tbst speak --token <ProvidedToken> <Speech to try>" ) ; console . log ( ) ; } else { let displayMessage = getError ( ex ) ; const Logger = "BST" ; console . log ( displayMessage ) ; LoggingHelper . prepareForFileLoggingAndDisableConsole ( "bst-debug.log" ) ; LoggingHelper . error ( Logger , "Error using bst version: " + Global . version ( ) + " on Node: " + process . version ) ; LoggingHelper . error ( Logger , ex ) ; } process . exit ( 0 ) ; return ; } if ( token ) { console . log ( "Your token is saved, you can now use this command without providing a token" ) ; } let nodeId = undefined ; if ( Global . config ( ) && Global . config ( ) . secretKey && Global . config ( ) . secretKey ( ) ) { nodeId = Global . config ( ) . secretKey ( ) ; } BstStatistics . instance ( ) . record ( BstCommand . speak , undefined , nodeId , Global . version ( ) ) ; console . log ( VirtualDeviceClient . renderResult ( virtualDeviceResponse ) ) ; } ) ; if ( process . argv . slice ( 2 ) . length === 0 ) { program . outputHelp ( ) ; } else { program . parse ( process . argv ) ; } } ) ; function getError ( ex ) { let objectError = undefined ; if ( ex === Object ( ex ) ) { objectError = tryParse ( ex . message ) || ex . message ; } else { objectError = tryParse ( ex ) || ex ; } if ( typeof ( objectError ) === "string" ) { return objectError ; } else if ( typeof ( objectError ) === "object" ) { if ( objectError . error ) { if ( typeof ( objectError . error ) === "string" ) { return objectError . error ; } else if ( Array . isArray ( objectError . error ) ) { return objectError . error . join ( ", " ) ; } } return "Sorry, something went wrong. Please try again in a few minutes" ; } } function tryParse ( object ) { try { return JSON . parse ( object ) ; } catch ( error ) { return undefined ; } }