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

Commit 0973a07

Browse files
author
Volkmar Vogel
committed
Merge remote-tracking branch 'origin/master'
# Conflicts: # build.gradle
2 parents 898cf13 + 2d8979a commit 0973a07

File tree

19 files changed

+81
-27
lines changed

19 files changed

+81
-27
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ cache:
1616
- $HOME/.gradle/wrapper/
1717

1818
jdk:
19-
- oraclejdk8
19+
- openjdk10
2020

2121
after_success:
2222
- bash <(curl -s https://codecov.io/bash)

alexa-plugin/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ description 'The Alexa plugin for Dialog to write voice applications for Dialogf
88
dependencies {
99
implementation project(":core")
1010
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
11-
implementation "com.amazon.alexa:ask-sdk-core:2.17.0"
11+
implementation "com.amazon.alexa:ask-sdk-core:2.19.0"
1212
}
1313

1414
dokka {

alexa-spring-plugin/build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ dependencies {
2222

2323
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
2424
implementation 'org.jetbrains.kotlin:kotlin-reflect'
25-
implementation 'org.springframework:spring-context:5.1.4.RELEASE'
26-
implementation 'com.amazon.alexa:ask-sdk-core:2.11.2'
25+
implementation 'org.springframework:spring-context:5.1.8.RELEASE'
26+
implementation 'com.amazon.alexa:ask-sdk-core:2.19.0'
2727
}
2828

2929
task dokkaJavadoc(type: org.jetbrains.dokka.gradle.DokkaTask) {

core/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ description 'Dialog is a Dialogflow v2 API implementation written in Kotlin. Wit
77

88
dependencies {
99
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
10+
implementation 'com.google.code.gson:gson:2.8.5'
1011
}
1112

1213
task dokkaJavadoc(type: org.jetbrains.dokka.gradle.DokkaTask) {

core/src/main/kotlin/org/rewedigital/dialog/handler/DialogflowHandler.kt

+53-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package org.rewedigital.dialog.handler
22

3-
import org.rewedigital.dialog.extensions.getSelectedOption
4-
import org.rewedigital.dialog.extensions.hasSurfaceCapability
5-
import org.rewedigital.dialog.extensions.permissionGranted
6-
import org.rewedigital.dialog.extensions.signInStatus
3+
import com.google.gson.Gson
4+
import com.google.gson.reflect.TypeToken
5+
import org.rewedigital.dialog.extensions.*
76
import org.rewedigital.dialog.model.dialogflow.DialogflowParams
87
import org.rewedigital.dialog.model.dialogflow.OutputContext
98
import org.rewedigital.dialog.model.dialogflow.WebhookRequest
109
import org.rewedigital.dialog.model.google.Conversation
1110
import org.rewedigital.dialog.model.google.SurfaceCapabilities
11+
import java.util.*
12+
import kotlin.collections.HashMap
1213

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

29+
/**
30+
* The stored user data aka user storage.
31+
* @see https://developers.google.com/actions/assistant/save-data#json
32+
*/
33+
val userData: MutableMap<String, Any?> = run {
34+
val userData = webhookRequest.originalDetectIntentRequest?.payload?.user?.userStorage
35+
?: return@run mutableMapOf<String, Any?>()
36+
fromJson(userData).toMutableMap()
37+
}
38+
2839
/**
2940
* The action name defined in Dialogflow.
3041
*/
@@ -52,9 +63,22 @@ class DialogflowHandler(private val webhookRequest: WebhookRequest) {
5263
/**
5364
* An unique identifier of the users google account.
5465
*/
55-
val userId: String?
56-
get() = webhookRequest.originalDetectIntentRequest?.payload?.user?.userId
57-
?: webhookRequest.originalDetectIntentRequest?.payload?.user?.idToken
66+
val userId: String
67+
get() {
68+
return if (userData.containsKey("userId")) {
69+
userData["userId"] as String
70+
} else {
71+
val oldUserId = webhookRequest.originalDetectIntentRequest?.payload?.user?.userId
72+
if (oldUserId.isNullOrEmpty()) {
73+
val newUserId = UUID.randomUUID().toString()
74+
userData["userId"] = newUserId
75+
newUserId
76+
} else {
77+
userData["userId"] = oldUserId
78+
oldUserId
79+
}
80+
}
81+
}
5882

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

209+
private fun fromJson(serializedValue: String?): Map<String, Any> {
210+
if (serializedValue != null && serializedValue.isNotEmpty()) {
211+
val gson = Gson()
212+
try {
213+
val map: Map<String, Any> = gson.fromJson(
214+
serializedValue,
215+
object : TypeToken<Map<String, Any>>() {}.type
216+
)
217+
// NOTE: The format of the opaque string is:
218+
// keyValueData: {key:value; key:value; }
219+
if (map["data"] != null) {
220+
return map["data"] as Map<String, Any>
221+
}
222+
} catch (e: Exception) {
223+
println(e.message)
224+
}
225+
}
226+
return HashMap()
227+
}
228+
185229
class ContextWrapper(
186230
private val context: MutableList<OutputContext>,
187231
private val session: String

core/src/main/kotlin/org/rewedigital/dialog/handler/DialogflowResponseBuilder.kt

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.rewedigital.dialog.handler
22

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

1314
private fun WebhookResponse.getOrCreatePayload() =
14-
payload ?: Payload(google = GooglePayload(richResponse = RichResponse())).also { newPayload ->
15+
payload ?: Payload(
16+
google = GooglePayload(
17+
richResponse = RichResponse()
18+
)
19+
).also { newPayload ->
1520
payload = newPayload
1621
}
1722

@@ -262,6 +267,16 @@ class DialogflowResponseBuilder(private val dialogflowHandler: DialogflowHandler
262267
.apply {
263268
source = "Webhook"
264269
outputContexts = dialogflowHandler.getContextList().toMutableList()
270+
getOrCreatePayload().google?.userStorage = run {
271+
val dataMap = HashMap<String, Any?>()
272+
dataMap["data"] = dialogflowHandler.userData
273+
Gson().toJson(dataMap)
274+
}
275+
276+
// Remove RichResponse if there are no items in it
277+
if (payload?.google?.richResponse?.items?.isEmpty() == true) {
278+
payload?.google?.richResponse = null
279+
}
265280
}
266281
.validate()
267282
}

core/src/main/kotlin/org/rewedigital/dialog/model/google/User.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ data class User(
99
val permissions: List<Permissions>?,
1010
val locale: String?,
1111
val lastSeen: String?,
12-
val userStorage: String?
12+
var userStorage: String?
1313
) {
1414
data class Profile(
1515
val displayName: String?,

core/src/main/kotlin/org/rewedigital/dialog/model/google/UserStorage.kt

-6
This file was deleted.

settings.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
rootProject.name = 'dialog'
22
include 'core'
3-
include 'ssml'
3+
include 'ssml-builder'
44
include 'ssml-plugin'
55
include 'spring-sample'
66
include 'spring-plugin'

spring-plugin/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ dependencies {
2020

2121
implementation 'org.jetbrains.kotlin:kotlin-reflect'
2222
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
23-
implementation 'org.springframework:spring-context:5.1.6.RELEASE'
23+
implementation 'org.springframework:spring-context:5.1.8.RELEASE'
2424
}
2525

2626
dokka {

spring-sample/build.gradle

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ version '1.0.0-SNAPSHOT'
33

44
buildscript {
55
ext {
6-
springBootVersion = '2.1.4.RELEASE'
6+
springBootVersion = '2.1.6.RELEASE'
77
}
88
repositories {
99
jcenter()
@@ -35,10 +35,10 @@ compileTestKotlin {
3535
}
3636

3737
dependencies {
38-
def askSdkVersion = '2.17.0'
38+
def askSdkVersion = '2.19.0'
3939

4040
implementation project(':core')
41-
implementation project(':ssml')
41+
implementation project(':ssml-builder')
4242
implementation project(':ssml-plugin')
4343
implementation project(':spring-plugin')
4444
implementation project(':konversation-plugin')
File renamed without changes.

ssml-plugin/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ apply from: '../docu.gradle'
1010
dependencies {
1111
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
1212
implementation project(':core')
13-
implementation project(':ssml')
13+
implementation project(':ssml-builder')
1414
}
1515

1616
dokka {

0 commit comments

Comments
 (0)