Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #3 from rewe-digital-incubator/feature/user_id_mig…
Browse files Browse the repository at this point in the history
…ration

User id migration
  • Loading branch information
Volkmar Vogel authored Jul 15, 2019
2 parents e8da9a3 + 9f12ed0 commit 2d8979a
Show file tree
Hide file tree
Showing 20 changed files with 83 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ cache:
- $HOME/.gradle/wrapper/

jdk:
- oraclejdk8
- openjdk10

after_success:
- bash <(curl -s https://codecov.io/bash)
2 changes: 1 addition & 1 deletion alexa-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ description 'The Alexa plugin for Dialog to write voice applications for Dialogf
dependencies {
implementation project(":core")
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation "com.amazon.alexa:ask-sdk-core:2.17.0"
implementation "com.amazon.alexa:ask-sdk-core:2.19.0"
}

dokka {
Expand Down
4 changes: 2 additions & 2 deletions alexa-spring-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ dependencies {

implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
implementation 'org.jetbrains.kotlin:kotlin-reflect'
implementation 'org.springframework:spring-context:5.1.4.RELEASE'
implementation 'com.amazon.alexa:ask-sdk-core:2.11.2'
implementation 'org.springframework:spring-context:5.1.8.RELEASE'
implementation 'com.amazon.alexa:ask-sdk-core:2.19.0'
}

task dokkaJavadoc(type: org.jetbrains.dokka.gradle.DokkaTask) {
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ apply plugin: 'com.github.ben-manes.versions'

buildscript {
ext.versions = [:]
ext.versions.kotlin = '1.3.30'
ext.versions.kotlin = '1.3.41'
ext.versions.dokka = '0.9.18'
ext.versions.core = '1.0.1'
ext.versions.core = '1.0.2'
ext.versions.alexa = '1.0.1'
ext.versions.ssml = '1.0.2'
ext.versions.ssml_plugin = '1.0.2'
Expand Down
1 change: 1 addition & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ description 'Dialog is a Dialogflow v2 API implementation written in Kotlin. Wit

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation 'com.google.code.gson:gson:2.8.5'
}

task dokkaJavadoc(type: org.jetbrains.dokka.gradle.DokkaTask) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package org.rewedigital.dialog.handler

import org.rewedigital.dialog.extensions.getSelectedOption
import org.rewedigital.dialog.extensions.hasSurfaceCapability
import org.rewedigital.dialog.extensions.permissionGranted
import org.rewedigital.dialog.extensions.signInStatus
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import org.rewedigital.dialog.extensions.*
import org.rewedigital.dialog.model.dialogflow.DialogflowParams
import org.rewedigital.dialog.model.dialogflow.OutputContext
import org.rewedigital.dialog.model.dialogflow.WebhookRequest
import org.rewedigital.dialog.model.google.Conversation
import org.rewedigital.dialog.model.google.SurfaceCapabilities
import java.util.*
import kotlin.collections.HashMap

/**
* Wrapper of [WebhookRequest] which provides utility functions for easier context access and other parameters.
Expand All @@ -19,12 +20,22 @@ class DialogflowHandler(private val webhookRequest: WebhookRequest) {
/**
* Holds context related information.
*/
val context: DialogflowHandler.ContextWrapper =
DialogflowHandler.ContextWrapper(webhookRequest.queryResult?.outputContexts.orEmpty().map {
val context: ContextWrapper =
ContextWrapper(webhookRequest.queryResult?.outputContexts.orEmpty().map {
// clone the list and remove the session prefix
it.copy(name = it.name?.replace("${webhookRequest.session.orEmpty()}/contexts/", ""))
}.toMutableList(), webhookRequest.session.orEmpty())

/**
* The stored user data aka user storage.
* @see https://developers.google.com/actions/assistant/save-data#json
*/
val userData: MutableMap<String, Any?> = run {
val userData = webhookRequest.originalDetectIntentRequest?.payload?.user?.userStorage
?: return@run mutableMapOf<String, Any?>()
fromJson(userData).toMutableMap()
}

/**
* The action name defined in Dialogflow.
*/
Expand Down Expand Up @@ -52,9 +63,22 @@ class DialogflowHandler(private val webhookRequest: WebhookRequest) {
/**
* An unique identifier of the users google account.
*/
val userId: String?
get() = webhookRequest.originalDetectIntentRequest?.payload?.user?.userId
?: webhookRequest.originalDetectIntentRequest?.payload?.user?.idToken
val userId: String
get() {
return if (userData.containsKey("userId")) {
userData["userId"] as String
} else {
val oldUserId = webhookRequest.originalDetectIntentRequest?.payload?.user?.userId
if (oldUserId.isNullOrEmpty()) {
val newUserId = UUID.randomUUID().toString()
userData["userId"] = newUserId
newUserId
} else {
userData["userId"] = oldUserId
oldUserId
}
}
}

/**
* The unique identifier of detectIntent request session.
Expand Down Expand Up @@ -182,6 +206,26 @@ class DialogflowHandler(private val webhookRequest: WebhookRequest) {
context[name, key] = value
}

private fun fromJson(serializedValue: String?): Map<String, Any> {
if (serializedValue != null && serializedValue.isNotEmpty()) {
val gson = Gson()
try {
val map: Map<String, Any> = gson.fromJson(
serializedValue,
object : TypeToken<Map<String, Any>>() {}.type
)
// NOTE: The format of the opaque string is:
// keyValueData: {key:value; key:value; }
if (map["data"] != null) {
return map["data"] as Map<String, Any>
}
} catch (e: Exception) {
println(e.message)
}
}
return HashMap()
}

class ContextWrapper(
private val context: MutableList<OutputContext>,
private val session: String
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.rewedigital.dialog.handler

import com.google.gson.Gson
import org.rewedigital.dialog.extensions.validate
import org.rewedigital.dialog.factories.SystemIntentFactory
import org.rewedigital.dialog.model.dialogflow.*
Expand All @@ -11,7 +12,11 @@ class DialogflowResponseBuilder(private val dialogflowHandler: DialogflowHandler
private val response = WebhookResponse()

private fun WebhookResponse.getOrCreatePayload() =
payload ?: Payload(google = GooglePayload(richResponse = RichResponse())).also { newPayload ->
payload ?: Payload(
google = GooglePayload(
richResponse = RichResponse()
)
).also { newPayload ->
payload = newPayload
}

Expand Down Expand Up @@ -262,6 +267,16 @@ class DialogflowResponseBuilder(private val dialogflowHandler: DialogflowHandler
.apply {
source = "Webhook"
outputContexts = dialogflowHandler.getContextList().toMutableList()
getOrCreatePayload().google?.userStorage = run {
val dataMap = HashMap<String, Any?>()
dataMap["data"] = dialogflowHandler.userData
Gson().toJson(dataMap)
}

// Remove RichResponse if there are no items in it
if (payload?.google?.richResponse?.items?.isEmpty() == true) {
payload?.google?.richResponse = null
}
}
.validate()
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ data class User(
val permissions: List<Permissions>?,
val locale: String?,
val lastSeen: String?,
val userStorage: String?
var userStorage: String?
) {
data class Profile(
val displayName: String?,
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
rootProject.name = 'dialog'
include 'core'
include 'ssml'
include 'ssml-builder'
include 'ssml-plugin'
include 'spring-sample'
include 'spring-plugin'
Expand Down
2 changes: 1 addition & 1 deletion spring-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ dependencies {

implementation 'org.jetbrains.kotlin:kotlin-reflect'
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
implementation 'org.springframework:spring-context:5.1.6.RELEASE'
implementation 'org.springframework:spring-context:5.1.8.RELEASE'
}

dokka {
Expand Down
6 changes: 3 additions & 3 deletions spring-sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version '1.0.0-SNAPSHOT'

buildscript {
ext {
springBootVersion = '2.1.4.RELEASE'
springBootVersion = '2.1.6.RELEASE'
}
repositories {
jcenter()
Expand Down Expand Up @@ -35,10 +35,10 @@ compileTestKotlin {
}

dependencies {
def askSdkVersion = '2.17.0'
def askSdkVersion = '2.19.0'

implementation project(':core')
implementation project(':ssml')
implementation project(':ssml-builder')
implementation project(':ssml-plugin')
implementation project(':spring-plugin')
implementation project(':konversation-plugin')
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion ssml-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ apply from: '../docu.gradle'
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation project(':core')
implementation project(':ssml')
implementation project(':ssml-builder')
}

dokka {
Expand Down

0 comments on commit 2d8979a

Please sign in to comment.