Skip to content
This repository was archived by the owner on May 30, 2023. It is now read-only.

CEK Request Handler

Yuichi Morimichi edited this page Oct 16, 2018 · 2 revisions

Java file

Create a Handler class in the package below the class as the entry point for starting with Spring Boot (e.g. Application.java).

@CEKRequestHandler
public class SampleHandler {
}

Implement each process in this class. All of the examples shown below should be implemented in the SampleHandler class.

Mapping

Define what kind of request is handled by annotation. The example below is a handler for LaunchRequest.

@LaunchMapping
CEKResponse handleLaunch() {
    return CEKResponse.builder()
                      .outputSpeech(OutputSpeech.text("Launched"))
                      .shouldEndSession(false) // default value is true
                      .build();
}

The return type of the handler method is always CEKResponse.

The mapping annotations are all with the following.

  • @SessionEndedMapping
  • @EventMapping
  • @IntentMapping

Handler Method Arguments

The parameters that can be specified in the handler method are as follows.

type annotation Optional validation name detection
Context
(extends ContextProperty)
@ContextValue
(optional)
supported supported supported
Slot @SlotValue
(required)
supported not supported supported
Event Payload
(extends Payload)
- not supported supported -
Session Attribute @SessionValue
(required)
supported supported
(conditional)
supported
Session
(CEKRequestMessage.Session)
- - - -
SessionHolder - - - -
HttpServletRequest - - - -

Mapping for named request

EventRequest and IntentRequest have a name. You need to specify that name in annotation.

IntentRequest

Below is a custom intent named Echo as an example.

@IntentMapping("Echo")
CEKResponse handleRepeatIntent(@SlotValue Optional<Integer> number) {
    String outputSpeechText = number.map(num -> "The number is " + num + ".")
                                    .orElse("I couldn't catch it.");
    return CEKResponse.builder()
                      .outputSpeech(OutputSpeech.text(outputSpeechText))
                      .shouldEndSession(false)
                      .build();
}

Slots

In the example above we have received a custom slot. Custom slots can receive any type as long as Jackson ObjectMapper is convertible.

Required / Optional

There are one of the following ways to cover patterns that can not acquire Slot.

@SlotValue Optional<Integer> number

If the value of Slot can not be obtained, number is Optional.empty().

@SlotValue(required = false) Integer number

If the value of Slot can not be acquired, number is null.

To occur an error if the value of Slot can not be obtained, specify it as follows.

@SlotValue Integer number

If the value of Slot can not be acquired, MissingSlotException will occur.

Slot Name

The name of the Slot matches the name of the method parameter.

@SlotValue Optional<String> trackId

In this example, the name of Slot is trackId. In addition, we support the following case format.

Case Format e.g.
Upper Kebab TRACK-ID
Lower Kebab track-id
Upper Snake TRACK_ID
Lower Snake track_id
Pascal TrackId

In any of these Case Formats, the parameter name of the handler method can be received as trackId.

If it does not match the parameter name of the handler method, specify it as follows.

@SlotValue("trackId") Optional<String> trackIdOpt

EventMapping

Below shows handlers for events of "namespace":"AudioPlayer", "name":"PlayStarted".

@EventMapping("AudioPlayer.PlayStarted")
CEKResponse handlePlayStarted(@Valid Optional<AudioPlayerPlaybackStatePayload> payload) {
    return CEKResponse.empty();
}

Connect namespace and name with ..

The EventRequest payload can be received as shown in the example. The specification of Required/Optional is the same as Slots.

AudioPlayerPlaybackStatePayload inherits com.linecorp.clova.extension.boot.message.payload.Payload. The EventRequest payload can receive whatever type it inherits Payload. The value is mapped by Jackson ObjectMapper.

@Valid and @Validated can also be used. Validation can be handled in the same way as Spring MVC Controller's method. Exceptions are the same.

Clone this wiki locally