import 'core-js/fn/object/values' ; import Clova from './types' ; class export class Context implements Clova . ClientContext { public requestObject : Clova . RequestBody ; public responseObject : Clova . ResponseBody ; constructor ( req ) { this . requestObject = req ; this . responseObject = { response : { card : { } , directives : [ ] , outputSpeech : { } , shouldEndSession : req . request . type === 'EventRequest' , } , sessionAttributes : ( req . session && req . session . sessionAttributes ) || { } , version : req . version , } ; } memberOf public endSession ( ) { this . responseObject . response . shouldEndSession = true ; this . responseObject . sessionAttributes = { } ; } memberOf public getSessionId ( ) { return this . requestObject . session . sessionId ; } memberOf public getIntentName ( ) { const request = this . requestObject . request as Clova . IntentRequest ; return request . intent ? request . intent . name : null ; } memberOf public getSlots ( ) { const request = this . requestObject . request as Clova . IntentRequest ; if ( ! request . intent || ! request . intent . slots ) { return { } ; } return Object . values ( request . intent . slots ) . reduce ( ( acc , curr ) => { return { ... acc , [ curr . name ] : curr . value } ; } , { } ) ; } memberOf public getSlotValueTypes ( ) { const request = this . requestObject . request as Clova . IntentRequest ; if ( ! request . intent || ! request . intent . slots ) { return { } ; } return Object . values ( request . intent . slots ) . filter ( curr => curr . valueType ) . reduce ( ( acc , curr ) => { return { ... acc , [ curr . name ] : curr . valueType } ; } , { } ) ; } memberOf public getSlotUnits ( ) { const request = this . requestObject . request as Clova . IntentRequest ; if ( ! request . intent || ! request . intent . slots ) { return { } ; } return Object . values ( request . intent . slots ) . filter ( curr => curr . unit ) . reduce ( ( acc , curr ) => { return { ... acc , [ curr . name ] : curr . unit } ; } , { } ) ; } param { string } slotName memberOf public getSlot ( slotName ) { return this . getSlots ( ) [ slotName ] || null ; } param { string } slotName memberOf public getSlotValueType ( slotName ) { return this . getSlotValueTypes ( ) [ slotName ] || null ; } param { string } slotName memberOf public getSlotUnit ( slotName ) { return this . getSlotUnits ( ) [ slotName ] || null ; } memberOf public getUser ( ) { return this . requestObject . session . user ; } param { Clova . OutputSpeech } outputSpeech param { boolean } reprompt memberOf public setOutputSpeech ( outputSpeech , reprompt = false ) { if ( reprompt ) { this . responseObject . response . reprompt = { outputSpeech } ; } else { this . responseObject . response . outputSpeech = outputSpeech ; } } param { Clova . OutputSpeech } outputSpeech memberOf public setReprompt ( outputSpeech ) { this . responseObject . response . reprompt = { outputSpeech } ; } param { Clova . SpeechInfoObject } speechInfo param { boolean } reprompt memberOf public setSimpleSpeech ( speechInfo , reprompt = false ) { const outputSpeech = { type : 'SimpleSpeech' , values : speechInfo , } ; this . setOutputSpeech ( outputSpeech , reprompt ) ; return this ; } param { Clova . SpeechInfoObject [ ] } speechInfo param { boolean } reprompt memberOf public setSpeechList ( speechInfo , reprompt = false ) { const outputSpeech = { type : 'SpeechList' , values : speechInfo , } ; this . setOutputSpeech ( outputSpeech , reprompt ) ; return this ; } param { Clova . SpeechInfoObject } speechInfoBrief param { Clova . OutputSpeechListVerbose } speechInfoVerbose param { boolean } reprompt memberOf public setSpeechSet ( speechInfoBrief , speechInfoVerbose , reprompt = false ) { const outputSpeech = { brief : speechInfoBrief , type : 'SpeechSet' , verbose : speechInfoVerbose , } ; this . setOutputSpeech ( outputSpeech , reprompt ) ; return this ; } memberOf public getSessionAttributes ( ) { return this . requestObject . session . sessionAttributes ; } memberOf public setSessionAttributes ( sessionAttributes ) { this . responseObject . sessionAttributes = sessionAttributes ; } }