Skip to content

Instantly share code, notes, and snippets.

View kinisoftware's full-sized avatar

Joaquin Engelmo Moriche kinisoftware

View GitHub Profile
@kinisoftware
kinisoftware / Utils.kt
Created October 14, 2019 17:41
Utils.kt
object Utils {
fun getLanguage(locale: String) = locale.substringBefore('-')
fun getRegion(locale: String) = locale.substringAfter('-')
}
@kinisoftware
kinisoftware / UsingTranslations.kt
Created October 14, 2019 17:37
UsingTranslations.kt
override fun handle(input: HandlerInput): Optional<Response> {
val request = input.requestEnvelope.request
val intentRequest = request as IntentRequest
val intent = intentRequest.intent
val slots = intent.slots
val releasesDate = slots["releasesDate"]!!
val dateValue = releasesDate.value
DirectiveServiceHandler(input).onRequestingUpcomings(input.getLanguage())
val movies = moviesGetter.getUpcomings(intentRequest.locale, dateValue)
@kinisoftware
kinisoftware / Translations.kt
Created October 14, 2019 17:36
Translations.kt
object Translations {
enum class TranslationKey {
WELCOME, HELP, THANKS, UPCOMINGS_NOT_FOUND, ASKING_FOR_NOW_PLAYING, UPCOMINGS_RESPONSE,
ERROR_ASKING_NOW_PLAYING, NOW_PLAYING_RESPONSE, REQUESTING_UPCOMINGS, REQUESTING_NOW_PLAYING_MOVIES, AND
}
fun getMessage(language: String, messageKey: TranslationKey) =
messages.getValue(language)[messageKey]
?: throw InternalServerErrorException("There is not messages for $language - $messageKey")
@kinisoftware
kinisoftware / usingProgressiveResponse.kt
Created October 12, 2019 11:36
usingProgressiveResponse.kt
class NewReleasesIntentHandler(private val moviesGetter: MoviesGetter) : RequestHandler {
override fun canHandle(input: HandlerInput): Boolean {
return input.matches(Predicates.intentName("NewReleasesIntent"))
}
override fun handle(input: HandlerInput): Optional<Response> {
...
// Get slots values and so on
...
@kinisoftware
kinisoftware / progressiveResponse.kt
Created October 12, 2019 11:18
progressiveResponse.kt
...
import com.amazon.ask.model.services.directive.Header
import com.amazon.ask.model.services.directive.SendDirectiveRequest
import com.amazon.ask.model.services.directive.SpeakDirective
...
class DirectiveServiceHandler(
private val input: HandlerInput
) {
module.exports = {
talks: [
{"time": "09:00", "title":"Keynote", "topics":[]},
{"time": "10:15", "title":"Cultura de empresa y tecnología", "topics":["BUSINESS"]},
{"time": "11:15", "title":"Caja negra, caja blanca y el futuro inmediato de la Inteligencia Artificial", "topics":["DEV"]},
{"time": "12:15", "title":"Bioimpresión CCMI", "topics":["MAKERS"]},
{"time": "13:15", "title":"Gestión de la Innovación", "topics":["BUSINESS"]},
{"time": "15:30", "title":"Gestión de servicios con equipos distribuidos", "topics":["DEV"]},
{"time": "16:30", "title":"Programando diseños 3D", "topics":["MAKERS"]},
{"time": "17:45", "title":"Keynote de cierre", "topics":[]}
private fun getRandomOkSpeech(): String {
val randomOkSpeechcon = listOf("okey", "okey dokey", "okey makey")[Random.nextInt(2)]
return "<say-as interpret-as=\"interjection\">$randomOkSpeechcon</say-as>"
}
private fun Movie.getTitle() =
when {
originalTitle == title && originalLanguage != "es" -> {
val lang = ssmlLangByMovieOriginalLanguage[originalLanguage]
when {
lang != null && lang.isNotBlank() -> "<lang xml:lang=$lang>${originalTitle.sanitaze()}</lang>"
else -> {
println("Unhandled original language $originalLanguage")
title.sanitaze()
}
override fun handle(input: HandlerInput): Optional<Response> {
val request = input.requestEnvelope.request
val intentRequest = request as IntentRequest
...
val movies = moviesGetter.getUpcomings(intentRequest.locale, dateValue)
...
}
class MoviesGetter(gson: Gson) {
private val theMovieDBService = TheMovieDBService(gson)
fun getUpcomings(locale: String, releaseDate: String) = theMovieDBService.getUpcomings(locale)
.filter { it.isReleasedOnDate(releaseDate) }
.map { it.getTitle() }
.getResponse()
fun getNowPlayingMovies(locale: String) = theMovieDBService.getNowPlayingMovies(locale)