简体   繁体   English

Kotlin 在 Android 上使用 Dialogflow 和 Fuel 实现简单的聊天机器人

[英]Kotlin Using Dialogflow with Fuel for simple chatbot on Android

I have been following this tutorial on how to create a simple chatbox in Android Studio.我一直在关注有关如何在 Android Studio 中创建简单聊天框的教程

I have successfully created an entity on Dialogeflow and double checked that I copied the ACCESS_KEY correctly.我已经在 Dialogeflow 上成功创建了一个实体,并仔细检查了我是否正确复制了ACCESS_KEY

Here is my code:这是我的代码:

FuelManager.instance.baseHeaders = mapOf(
            "Authorization" to "Bearer $ACCESS_TOKEN"
    )

    FuelManager.instance.basePath =
            "https://api.dialogflow.com/v1/"

    FuelManager.instance.baseParams = listOf(
            "v" to "20170712",                  // latest protocol
            "sessionId" to UUID.randomUUID(),   // random ID
            "lang" to "en"                      // English language
    )

    val human = ChatUser(
            1,
            "You",
            BitmapFactory.decodeResource(resources,
                    R.drawable.ic_account_circle)
    )

    val agent = ChatUser(
            2,
            "Agent",
            BitmapFactory.decodeResource(resources,
                    R.drawable.ic_account_circle)
    )

    my_chat_view.setOnClickSendButtonListener(
            View.OnClickListener {
                my_chat_view.send(Message.Builder()
                        .setUser(human)
                        .setText(my_chat_view.inputText)
                        .build()
                )

                // More code here
            }
    )
        Fuel.get("/query",
                listOf("query" to my_chat_view.inputText))
                .responseJson { _, _, result ->
                    val reply = result.get().obj()
                            .getJSONObject("result")
                            .getJSONObject("fulfillment")
                            .getString("speech")

                    my_chat_view.send(Message.Builder()
                            .setRight(true)
                            .setUser(agent)
                            .setText(reply)
                            .build()
                    )
                }

I have found that I am receiving a HTTP 404 error, which means that whatever path Fuel is creating is wrong or I'm missing a parameter.我发现我收到了一个 HTTP 404 错误,这意味着 Fuel 创建的任何路径都是错误的,或者我缺少一个参数。

I have double checked that the ACCESS TOKEN is correct and even created another just in case and updated the application.我已经仔细检查了 ACCESS TOKEN 是否正确,甚至创建了另一个以防万一并更新了应用程序。

When I run this program, it crashes the application.当我运行这个程序时,它使应用程序崩溃。 Only when I return the value of the query using a try catch statement did I find out it was because it was returning a 404 error.只有当我使用 try catch 语句返回查询的值时,我才发现这是因为它返回了 404 错误。 And also my logs show this aswell.而且我的日志也显示了这一点。

Anything that I am missing?有什么我想念的吗? This is my first time using Dialogflow.这是我第一次使用 Dialogflow。

I've been pulling my hair over this for the past 3 hours, looking at the logs and playing with try catch statements but still don't know what I'm missing.在过去的 3 个小时里,我一直在纠结这个问题,查看日志并使用 try catch 语句,但仍然不知道我错过了什么。

Dialogflow API Reference tells me that all I need is the Authorization token, Protocol version (I've tried both of them). Dialogflow API Reference告诉我,我需要的只是授权令牌,协议版本(我都试过)。

If anyone can help me out and needs more information let me know.如果有人可以帮助我并需要更多信息,请告诉我。

After searching on stackoverflow, I found this answer that gave me the structure that I needed for my GET request.在 stackoverflow 上搜索后,我找到了这个答案,它为我提供了 GET 请求所需的结构。

And so I removed the FuelManager's basePath and baseParams from my query and hardcoded the GET request in the format specified by the link posted above.因此,我从查询中删除了 FuelManager 的 basePath 和 baseParams,并以上面发布的链接指定的格式对 GET 请求进行了硬编码。

Fuel.get("https://api.dialogflow.com/v1/query?v=20150910&lang=en&query="
                +chatView.inputText+"&sessionId=12345")

My only guess is that the baseParams and basePath where not correctly linked to the Fuel instance.我唯一的猜测是 baseParams 和 basePath 没有正确链接到 Fuel 实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM