Skip to content

Instantly share code, notes, and snippets.

View kinisoftware's full-sized avatar

Joaquin Engelmo Moriche kinisoftware

View GitHub Profile
class NextIntentHandler : RequestHandler {
override fun canHandle(input: HandlerInput) = input.matches(
intentName("AMAZON.NextIntent")
.or(Predicates.requestType(NextCommandIssuedRequest::class.java))
)
override fun handle(input: HandlerInput): Optional<Response> {...}
}
fun HandlerInput.buildPlayResponse(audioStream: AudioStream): Optional<Response> {
val stream = Stream.builder()
.withOffsetInMilliseconds(audioStream.offsetInMilliseconds)
.withExpectedPreviousToken(null)
.withToken(audioStream.streamToken)
.withUrl(audioStream.file)
.build()
val audioItem = AudioItem.builder()
.withMetadata(
AudioItemMetadata.builder()
{
"type": "AudioPlayer.Play",
"playBehavior": "valid playBehavior value such as ENQUEUE",
"audioItem": {
"stream": {
"url": "https://cdn.example.com/url-of-the-stream-to-play",
"token": "opaque token representing this stream",
"expectedPreviousToken": "opaque token representing the previous stream",
"offsetInMilliseconds": 0,
"caption": {
class NextIntentHandler : RequestHandler {
override fun canHandle(input: HandlerInput) = input.matches(intentName("AMAZON.NextIntent"))
override fun handle(input: HandlerInput) =
input.attributesManager.persistentAttributes["lastPlayedAudio"]?.let {
PlayAudioResponseBuilder.buildNewsResponse(
input,
Audio(it.toAudio().getNextDate())
)
class NextAndPreviousIntentHandler : RequestHandler {
override fun canHandle(input: HandlerInput) = input.matches(
Predicates.intentName("AMAZON.NextIntent")
.or(Predicates.intentName("AMAZON.PreviousIntent"))
)
override fun handle(input: HandlerInput) = input.responseBuilder
.withSpeech(Translations.getMessage(input.getLanguage(), Translations.TranslationKey.NO_FEATURE))
.addAudioPlayerStopDirective()
class StartOverIntentHandler : RequestHandler {
override fun canHandle(input: HandlerInput) =
input.matches(Predicates.intentName("AMAZON.StartOverIntent"))
override fun handle(input: HandlerInput): Optional<Response> {
return input.attributesManager.persistentAttributes["lastPlayedAudio"]?.let {
input.playAudio(it.toAudio().url)
} ?: input.emptyResponse()
}
private fun HandlerInput.enqueuAudio(audioUrl: String) = responseBuilder
.addAudioPlayerPlayDirective(
PlayBehavior.ENQUEUE,
0L,
null,
"token",
audioUrl
)
.build()
private fun HandlerInput.buildResponse(audioUrl: String, offsetInMillis: Long? = 0L) = responseBuilder
.addAudioPlayerPlayDirective(
PlayBehavior.REPLACE_ALL,
offsetInMillis,
null,
"token",
audioUrl
)
.build()
class PlaybackStoppedHandler : RequestHandler {
override fun canHandle(input: HandlerInput) =
input.matches(Predicates.requestType(PlaybackStoppedRequest::class.java))
override fun handle(input: HandlerInput): Optional<Response> {
val request = input.requestEnvelope.request
val intentRequest = request as PlaybackStoppedRequest
val lastPlayedAudio = JacksonSerializer().deserialize(
class CancelAndStopIntentHandler : RequestHandler {
override fun canHandle(input: HandlerInput) =
input.matches(
intentName("AMAZON.StopIntent")
.or(intentName("AMAZON.CancelIntent"))
.or(intentName("AMAZON.PauseIntent"))
)
override fun handle(input: HandlerInput) = input.responseBuilder