首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google操作无法解析JSON响应

Google操作无法解析JSON响应
EN

Stack Overflow用户
提问于 2018-01-31 11:29:18
回答 3查看 1.6K关注 0票数 0

我一直在努力让一个助理应用程序做出正确的反应。

这个应用程序是用Google编写的(没有其他组件,如对话框或Converse.ai)。

该应用程序的动作包如下所示:

代码语言:javascript
复制
{
    "actions":[
       {
         "description": "Default Welcome Intent",
         "name": "MAIN",
         "fulfillment": {
            "conversationName": "ACTION_INTENT_MAIN"
          },
          "intent": {
             "name": "actions.intent.MAIN",
             "trigger": {
                 "queryPatterns": [
                    "talk to gactions integration"
                  ]
             }
          }
       }
    ],
    "conversations":{
        "ACTION_INTENT_MAIN": {
           "name": "ACTION_INTENT_MAIN",
           "url": "https://my.custom.end/point"
        }
    },
    "locate":"en"
}

这是我在https://my.custom.end/pont上收到的JSON请求

代码语言:javascript
复制
{
     "user": {
     "userId": "<a-user-id-sent-by-GOOG>",
     "locale": "en-US",
     "lastSeen": "2018-01-31T09:33:07Z"
   },
   "conversation": {
     "conversationId": "1517393479793",
     "type": "NEW"
   },
   "inputs": [
     {
       "intent": "actions.intent.MAIN",
       "rawInputs": [
         {
           "inputType": "KEYBOARD",
           "query": "Talk to Gactions Integration"
         }
       ]
     }
   ],
   "surface": {
     "capabilities": [
       {
         "name": "actions.capability.AUDIO_OUTPUT"
       },
       {
         "name": "actions.capability.SCREEN_OUTPUT"
       },
       {
         "name": "actions.capability.MEDIA_RESPONSE_AUDIO"
       },
       {
         "name": "actions.capability.WEB_BROWSER"
       }
     ]
   },
   "isInSandbox": true,
   "availableSurfaces": [
     {
       "capabilities": [
         {
           "name": "actions.capability.AUDIO_OUTPUT"
         },
         {
           "name": "actions.capability.SCREEN_OUTPUT"
         }
       ]
     }
   ]
 }

在模拟器上,我得到了这样的响应:API Version 2: Failed to parse JSON response string with 'INVALID_ARGUMENT' error: ": Cannot find field.".

我可以确认传入的请求已将google-actions-api-version设置为2

这是Google仿真器上的动作的sharedDebugInfo部分:

代码语言:javascript
复制
[
    {
      "name": "ResponseValidation",
      "subDebugEntry": [
        {
          "debugInfo": "API Version 2: Failed to parse JSON response string with 'INVALID_ARGUMENT' error: \": Cannot find field.\".",
          "name": "UnparseableJsonResponse"
        }
      ]
    }
  ]

信息丰富的错误消息缺少无法找到的字段的名称。

这就是我所做的一切关于这里

更新-2018年2月1日,11:52 AM -这些是调试选项卡的全部内容:

代码语言:javascript
复制
{
  "audioResponse": "//NExAAQaE...",
  "conversationToken": "GidzaW11bG...",
  "debugInfo": {
    "agentToAssistantDebug": {
      "agentToAssistantJson": "{\"conversationToken\":\"{\\\"state\\\":null,\\\"data\\\":{}}\",\"expectUserResponse\":true,\"expectedInputs\":[{\"inputPrompt\":{\"noInputPrompts\":[],\"richInitialPrompt\":{\"items\":[{\"simpleResponse\":{\"textToSpeech\":\"You're now talking to GActions Integration\",\"displayText\":\"You're now talking to GActions Integration\"}},{\"basicCard\":{\"buttons\":[{\"title\":\"Some Reddit to chill\",\"openUrlAction\":{\"url\":\"https://www.reddit.com\"}}],\"formattedText\":\"Here's some simp-wave to relax and chill out to\",\"image\":{\"url\":\"http://tracks.arte.tv/sites/default/files/styles/jscrop_1007x566/public/c_simpsons_2.jpg?itok=INzKpsvK\",\"accessibilityText\":\"A World of Simpsonwave\"},\"title\":\"A World of Simpsonwave\",\"imageDisplayOptions\":\"CROPPED\"}}],\"suggestions\":[]}},\"possibleIntents\":[{\"intent\":\"actions.intent.TEXT\"}]}],\"resetUserStorage\":false,\"userStorage\":\"{}\",\"finalResponse\":null,\"isInSandbox\":true,\"customPushMessage\":null,\"speech\":\"You're now talking to GActions Integration. \",\"displayText\":\"You're now talking to GActions Integration. \"}"
    },
    "assistantToAgentDebug": {
      "assistantToAgentJson": "{\"user\":{\"userId\":\"ABwppHHHw9N9TYh-scJ5GhZtmpfFcQU2xbQBAgW1qhdllI45fimQ5QKFEVRfs2iMm6uCDJIQMApo1UZLmmnif8wqlNARnsVH744\",\"locale\":\"en-US\",\"lastSeen\":\"2018-02-01T06:04:59Z\"},\"conversation\":{\"conversationId\":\"1517465629416\",\"type\":\"NEW\"},\"inputs\":[{\"intent\":\"actions.intent.MAIN\",\"rawInputs\":[{\"inputType\":\"VOICE\",\"query\":\"Talk to Gactions Integration\"}]}],\"surface\":{\"capabilities\":[{\"name\":\"actions.capability.WEB_BROWSER\"},{\"name\":\"actions.capability.AUDIO_OUTPUT\"},{\"name\":\"actions.capability.SCREEN_OUTPUT\"},{\"name\":\"actions.capability.MEDIA_RESPONSE_AUDIO\"}]},\"isInSandbox\":true,\"availableSurfaces\":[{\"capabilities\":[{\"name\":\"actions.capability.SCREEN_OUTPUT\"},{\"name\":\"actions.capability.AUDIO_OUTPUT\"}]}]}",
      "curlCommand": "curl -v https://firedev.arrowai.com/integrations/goog-actions/messages/59f6b4bf8d16126f008b456a/5a619bf72c971189008b4569 -H 'Content-Type: application/json;charset=UTF-8' -H 'Google-Actions-API-Version: 2' -H 'Authorization: eyJhbGciOiJSUzI1NiIsImtpZCI6IjI2YzAxOGIyMzNmZTJlZWY0N2ZlZGJiZGQ5Mzk4MTcwZmM5YjI5ZDgifQ.eyJhdWQiOiJnYWN0aW9ucy1hcnJvd2FpLWludGVncmF0ZSIsImF6cCI6IjQ2NDA5MDk1NDc2Ny0xOWl2bnUxdjFwYXFpdWdodDJqYXJwcTJwaGtmNGRyMS5hcHBzLmdvb2dsZXVzZXJjb250ZW50LmNvbSIsImV4cCI6MTUxNzQ2NTc0OSwiaXNzIjoiaHR0cHM6Ly9hY2NvdW50cy5nb29nbGUuY29tIiwianRpIjoiZTgxNzlmN2FkZGYwYWZmYjYwNzk0MDg5MzIyN2E0MWU1NDZkZWMyOCIsImlhdCI6MTUxNzQ2NTYyOSwibmJmIjoxNTE3NDY1MzI5fQ.rmUxBhIirV0UnZvTLYYw4AtRQfNDF-3O6CaoXxD2BDZSufgfKFHz7aIUgJzUillBm4zOLFiV6SnXndIoYCG6JK60YK5tHLpqbV-P3C-5U3N05RuY7hpj1Q_B027bgUv-p7QWlg-7DmPkROmN3b3a_FsUeWqTxx5Bu5M551k_CpKvSSBzPMEB2Cw9TzGkiY9avYrISTLLxWl2JE7qKAT6P0zBbE6cdO04CxGddGNmEPkckRICsdSJ5j_wmjy3I-ItAgx3dKoDfFLW6gCI4y5MFsGFJY6cFc2e01nodslf9GlQzAhR_a4aHRRIDl47HT_b9aKpyULV3kiRR41mUkyYZw'  -A 'Mozilla/5.0 (compatible; Google-Cloud-Functions/2.1; +http://www.google.com/bot.html)' -X POST -d '{\"user\":{\"userId\":\"ABwppHHHw9N9TYh-scJ5GhZtmpfFcQU2xbQBAgW1qhdllI45fimQ5QKFEVRfs2iMm6uCDJIQMApo1UZLmmnif8wqlNARnsVH744\",\"locale\":\"en-US\",\"lastSeen\":\"2018-02-01T06:04:59Z\"},\"conversation\":{\"conversationId\":\"1517465629416\",\"type\":\"NEW\"},\"inputs\":[{\"intent\":\"actions.intent.MAIN\",\"rawInputs\":[{\"inputType\":\"VOICE\",\"query\":\"Talk to Gactions Integration\"}]}],\"surface\":{\"capabilities\":[{\"name\":\"actions.capability.WEB_BROWSER\"},{\"name\":\"actions.capability.AUDIO_OUTPUT\"},{\"name\":\"actions.capability.SCREEN_OUTPUT\"},{\"name\":\"actions.capability.MEDIA_RESPONSE_AUDIO\"}]},\"isInSandbox\":true,\"availableSurfaces\":[{\"capabilities\":[{\"name\":\"actions.capability.SCREEN_OUTPUT\"},{\"name\":\"actions.capability.AUDIO_OUTPUT\"}]}]}'"
    },
    "sharedDebugInfo": [
      {
        "name": "ResponseValidation",
        "subDebugEntry": [
          {
            "debugInfo": "API Version 2: Failed to parse JSON response string with 'INVALID_ARGUMENT' error: \": Cannot find field.\".",
            "name": "UnparseableJsonResponse"
          }
        ]
      }
    ]
  },
  "response": "Gactions integration isn't responding right now. Try again soon.",
  "visualResponse": {
    "visualElements": []
  }
}

这是agentToAssistantDebug对象:

代码语言:javascript
复制
{
  "conversationToken": "{\"state\":null,\"data\":{}}",
  "expectUserResponse": true,
  "expectedInputs": [
    {
      "inputPrompt": {
        "noInputPrompts": [],
        "richInitialPrompt": {
          "items": [
            {
              "simpleResponse": {
                "textToSpeech": "You're now talking to GActions Integration",
                "displayText": "You're now talking to GActions Integration"
              }
            },
            {
              "basicCard": {
                "buttons": [
                  {
                    "title": "Some Reddit to chill",
                    "openUrlAction": {
                      "url": "https://www.reddit.com"
                    }
                  }
                ],
                "formattedText": "Here's some simp-wave to relax and chill out to",
                "image": {
                  "url": "http://tracks.arte.tv/sites/default/files/styles/jscrop_1007x566/public/c_simpsons_2.jpg?itok=INzKpsvK",
                  "accessibilityText": "A World of Simpsonwave"
                },
                "title": "A World of Simpsonwave",
                "imageDisplayOptions": "CROPPED"
              }
            }
          ],
          "suggestions": []
        }
      },
      "possibleIntents": [
        {
          "intent": "actions.intent.TEXT"
        }
      ]
    }
  ],
  "resetUserStorage": false,
  "userStorage": "{}",
  "finalResponse": null,
  "isInSandbox": true,
  "customPushMessage": null,
  "speech": "You're now talking to GActions Integration. ",
  "displayText": "You're now talking to GActions Integration. "
}

此外,这是正在生成的JSON响应:

代码语言:javascript
复制
{
   "conversationToken": "{\"state\":null,\"data\":{}}",
   "expectUserResponse": true,
   "expectedInputs": [
     {
       "inputPrompt": {
         "noInputPrompts": [],
         "richInitialPrompt": {
           "items": [
             {
               "simpleResponse": {
                 "textToSpeech": "You're now talking to GActions Integration",
                 "displayText": "You're now talking to GActions Integration"
               }
             },
             {
               "basicCard": {
                 "buttons": [
                   {
                     "title": "Some Reddit to chill",
                     "openUrlAction": {
                       "url": "https://www.reddit.com"
                     }
                   }
                 ],
                 "formattedText": "Here's some simp-wave to relax and chill out to",
                 "image": {
                   "url": "http://tracks.arte.tv/sites/default/files/styles/jscrop_1007x566/public/c_simpsons_2.jpg?itok=INzKpsvK",
                   "accessibilityText": "A World of Simpsonwave"
                 },
                 "title": "A World of Simpsonwave",
                 "imageDisplayOptions": "CROPPED"
               }
             }
           ],
           "suggestions": []
         }
       },
       "possibleIntents": [
         {
           "intent": "actions.intent.TEXT"
         }
       ]
     }
   ],
   "resetUserStorage": false,
   "userStorage": "{}",
   "finalResponse": null,
   "isInSandbox": true,
   "customPushMessage": null,
   "speech": "You're now talking to GActions Integration. ",
   "displayText": "You're now talking to GActions Integration. "
 }
EN

回答 3

Stack Overflow用户

发布于 2018-02-01 08:25:36

这个问题似乎与您的响应JSON有关。正如您在文档中看到的那样,有关响应JSON。

代码语言:javascript
复制
"speech": "You're now talking to GActions Integration. ",
"displayText": "You're now talking to GActions Integration. "

以上是无效的参数。相反,您的响应JSON应该如下所示:

代码语言:javascript
复制
{
  "conversationToken": "{\"state\":null,\"data\":{}}",
  "expectUserResponse": true,
  "expectedInputs": [
    {
      "inputPrompt": {
        "noInputPrompts": [],
        "richInitialPrompt": {
          "items": [
            {
              "simpleResponse": {
                "textToSpeech": "You're now talking to GActions Integration",
                "displayText": "You're now talking to GActions Integration"
              }
            },
            {
              "basicCard": {
                "buttons": [
                  {
                    "title": "Some Reddit to chill",
                    "openUrlAction": {
                      "url": "https://www.reddit.com"
                    }
                  }
                ],
                "formattedText": "Here's some simp-wave to relax and chill out to",
                "image": {
                  "url": "http://tracks.arte.tv/sites/default/files/styles/jscrop_1007x566/public/c_simpsons_2.jpg?itok=INzKpsvK",
                  "accessibilityText": "A World of Simpsonwave"
                },
                "title": "A World of Simpsonwave",
                "imageDisplayOptions": "CROPPED"
              }
            }
          ],
          "suggestions": []
        }
      },
      "possibleIntents": [
        {
          "intent": "actions.intent.TEXT"
        }
      ]
    }
  ],
  "resetUserStorage": false,
  "userStorage": "{}",
  "finalResponse": null,
  "isInSandbox": true,
  "customPushMessage": null
}
票数 2
EN

Stack Overflow用户

发布于 2018-01-31 16:54:41

如果使用nodejs,请编辑,检查来自google函数的响应是否正确json,以及服务器是否回答正确的json。也许它改变了格式等等?

尝试使用action.json样式,就像第一个样式一样。和更重要的问题是,您对请求的响应如何?尝试使用下面最后一段代码创建的json样式。

代码语言:javascript
复制
{
  "locale": "en",
  "actions": [
    {
      "name": "text",
      "intent": {
        "name": "actions.intent.TEXT",
        "trigger": {
          "queryPatterns": [
            "some text"
          ]
        }
      },
      "fulfillment": {
        "conversationName": "conv name"
      }
    },
    {
      "description": "Default Welcome Intent",
      "name": "MAIN",
      "fulfillment": {
        "conversationName": "conv name"
      },
      "intent": {
        "name": "actions.intent.MAIN",
        "trigger": {
          "queryPatterns": [
            "open my test app",
            "open hi fish"
          ]
        }
      }
    }
  ],
  "types": [],
  "conversations": {
    "conv name": {
      "name": "conv name",
      "url": "https://yourendpoint.com/something",
      "fulfillmentApiVersion": 2,
      "in_dialog_intents": [
        {
          "name": "actions.intent.NO_INPUT"
        },
      ]
    }
  }
}

这里是一个创建有效响应的函数:

代码语言:javascript
复制
/**
message = string
slots = array
state = object
**/

    function answerWithMessage(message,slots,state){   
           let display = message.replace(/<[^>]*>/g, '');   
           let voicemessage = message.toLowerCase();

          let jsonResponse = {
            conversationToken: JSON.stringify(state),
            expectUserResponse: true,
            expectedInputs: [
              {
                inputPrompt: {
                  richInitialPrompt: {
                    items: [
                      {
                        simpleResponse: {
                          ssml: voicemessage,
                          displayText: display,
                        },
                      }
                    ],
                  }
                },
                possibleIntents: [
                  {
                    intent: "actions.intent.TEXT"
                  }
                ],
               speechBiasingHints: slots
              }
            ]   
         };   
       return JSON.stringify(jsonResponse,null, 4); 
    }

下面是一个例子,比如google:https://github.com/haukedau/googleActionsInit,但是答案并不完全正确,但是它会做同样的工作,上面的响应消息样式会更好。

票数 0
EN

Stack Overflow用户

发布于 2018-06-07 13:10:44

嗨,我想你的问题是你只需要调用一个对象

代码语言:javascript
复制
    "inputPrompt": { 

和一个数组,但两者处于相同的位置,请看下面的解释。

代码语言:javascript
复制
    "noInputPrompts": [],

1

我希望我能帮你。

诚挚的问候

帕特里克

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48541087

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档