首页 > 解决方案 > 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 的新手,所以我真的不知道如何解决这个问题。任何帮助,将不胜感激!

标签: kotlinchatbotpepper

解决方案


首先,为什么要使用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
       }
   }
}

推荐阅读