kotlin - Kotlin:未解决的参考:使用,
问题描述
我正在尝试将 Dialogflow 代理与 Pepper 集成:https ://developer.softbankrobotics.com/pepper-qisdk/lessons/integrating-chatbot-dialogflow
我遵循了所有步骤,直到在独立部分测试您的代理,我必须将以下 Kotlin 代码添加到 DialogflowSource 类:
import com.google.auth.oauth2.ServiceAccountCredentials
import com.google.cloud.dialogflow.v2.*
import java.io.InputStream
class DialogflowDataSource constructor(credentialsStream : InputStream) {
private val credentials : ServiceAccountCredentials
= ServiceAccountCredentials.fromStream(credentialsStream)
fun detectIntentTexts(
text: String,
sessionId: String,
languageCode: String
): String? {
val sessionsSettings = SessionsSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credentials))
.build()
SessionsClient.create(sessionsSettings).use { sessionsClient -> //Error: Unresolved reference for .use
val session = SessionName.of(credentials.projectId, sessionId)
val textInput = TextInput.newBuilder()
.setText(text).setLanguageCode(languageCode)
val queryInput = QueryInput
.newBuilder().setText(textInput).build()
val response = sessionsClient.detectIntent(session, queryInput)
return response.queryResult.fulfillmentText
}
} //Error: A 'return' expression required in a function with a block body ('{...}')
}
我是 Kotlin 的新手,所以我真的不知道如何解决这个问题。任何帮助,将不胜感激!
解决方案
首先,为什么要使用use
?看来你是打算打电话apply
来的。但实际上你可以写:
fun detectIntentTexts(
text: String,
sessionId: String,
languageCode: String
): String? {
val sessionsSettings = SessionsSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credentials))
.build()
val sessionClient = SessionsClient.create(sessionsSettings)
val session = SessionName.of(credentials.projectId, sessionId)
val textInput =
TextInput.newBuilder().setText(text).setLanguageCode(languageCode)
val queryInput = QueryInput.newBuilder().setText(textInput).build()
val response = sessionsClient.detectIntent(session, queryInput)
return response.queryResult.fulfillmentText
}
但是如果你关心使用use
(or apply
),你提供给它的 lambda 不应该直接让外部函数detectIntentTexts
返回。相反,让您的 lambda 在本地返回其结果,然后detectIntentTexts
返回它:
fun detectIntentTexts(
text: String,
sessionId: String,
languageCode: String
): String? {
val sessionsSettings = SessionsSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credentials))
.build()
return SessionsClient.create(sessionsSettings).apply { sessionsClient ->
val session = SessionName.of(credentials.projectId, sessionId)
val textInput = TextInput.newBuilder()
.setText(text).setLanguageCode(languageCode)
val queryInput = QueryInput
.newBuilder().setText(textInput).build()
val response = sessionsClient.detectIntent(session, queryInput)
response.queryResult.fulfillmentText
}
}
}
推荐阅读
- python - numpy 日志溢出记录在哪里?
- python - Matplotlib dateformatter 未按要求格式化日期
- reactjs - 在使用 useEffect 挂钩时,我在浏览器上收到此错误
- flutter - 调用 Navigator.of(context).pop() 后如何获取父上下文?
- google-apps-script - Google表格脚本:查找包含在公式中的文本的函数
- java - 限制读取字节数的 Java InputStream 包装器
- java - 是否可以在 Java 中创建动态多维 ArrayList?
- windows - 如何克服 CMD 中的 65535 行限制?
- python - Kivy:如何从 .py 代码中设置相对于其 parent 高度的小部件高度?
- c# - Microsoft Graph - Teams API 可在 Graph Explorer 上运行,但不能通过代码运行