Dialogflow V2,Java API将不显示基本卡

时间:2018-11-04 02:02:50

标签: java kotlin dialogflow

创建一个google助手应用并使用Java servlet在Kotlin中构建服务器。我想将BasicCard发送给用户。

此代码正确发送回一条简单消息

    @Throws(ServletException::class, IOException::class)
override fun doPost(req: HttpServletRequest, resp: HttpServletResponse) {
    val json = GsonFactory.getDefaultInstance().createJsonGenerator(resp.writer)
    val res = GoogleCloudDialogflowV2WebhookResponse()
    res.fulfillmentText = "Works Yah"
    json.serialize(res)
    json.flush()
}

此代码看起来应该发送BasicCard,但收到“由于语音响应为空而无法将Dialogflow响应解析为AppResponse”,

有什么想法吗?

   @Throws(ServletException::class, IOException::class)
override fun doPost(req: HttpServletRequest, resp: HttpServletResponse) {

    val json = GsonFactory.getDefaultInstance().createJsonGenerator(resp.writer)

    val text = GoogleCloudDialogflowV2IntentMessage()
    text.simpleResponses = GoogleCloudDialogflowV2IntentMessageSimpleResponses()
    text.simpleResponses.simpleResponses = mutableListOf(GoogleCloudDialogflowV2IntentMessageSimpleResponse().setDisplayText( "Hello Dialogflow"))

    val card = GoogleCloudDialogflowV2IntentMessage()
    card.basicCard = GoogleCloudDialogflowV2IntentMessageBasicCard().setTitle("Hello World")

    val res = GoogleCloudDialogflowV2WebhookResponse()
    res.fulfillmentMessages = mutableListOf<GoogleCloudDialogflowV2IntentMessage>()
    res.fulfillmentMessages.add(text)
    res.fulfillmentMessages.add(card)

    json.serialize(res)
    json.flush()
}

1 个答案:

答案 0 :(得分:0)

在经历了意想不到的痛苦和尴尬的时光之后,我确实知道了。

@Throws(ServletException::class, IOException::class)
override fun doPost(req: HttpServletRequest, resp: HttpServletResponse) {
    val gson = GsonFactory.getDefaultInstance()       
    val json = gson.createJsonGenerator(resp.writer)

    val text = GoogleCloudDialogflowV2IntentMessage()
    // platform has to be set
    text.platform = "ACTIONS_ON_GOOGLE"
    text.simpleResponses = GoogleCloudDialogflowV2IntentMessageSimpleResponses()
     //set both display text and text to speech
    val simpleText = GoogleCloudDialogflowV2IntentMessageSimpleResponse()
                       .setTextToSpeech("text to speech")
                       .setDisplayText("display text")
    text.simpleResponses.simpleResponses = mutableListOf(simpleText)


    val card = GoogleCloudDialogflowV2IntentMessage()
    card.platform = "ACTIONS_ON_GOOGLE"
    // you must have an image and/or formatted text set on the card
    card.basicCard = GoogleCloudDialogflowV2IntentMessageBasicCard().setTitle("Hello World").setFormattedText("The Text")


    val myResponse = GoogleCloudDialogflowV2WebhookResponse()
    // text is necessary and it must be first in the list
    myResponse.fulfillmentMessages = mutableListOf(text, card)

    json.serialize(myResponse)
    json.flush()
}

这是有效的代码,但是如果Google可以发布类似于Node dialogflow API的Java API,那就太好了。或者至少是现有Java JSON对象的基本文档级别。