Dialogflow Fulfillment - 在 Google 电子表格中创建一个新行(使用 Sheetdb io) - 这是代码

Dialogflow Fulfillment - Create a New Row in a Google Spreedsheet (Using Sheetdb io) - Here is the Code

我正在尝试让我的 Dialogflow 代理在 Google Sheet 中使用他们的联系信息创建一个新行。我正在使用 Sheetdb.io 将 sheet 用作 API.

我有它,因此它可以从 Google Spreadsheet 中提取信息,但我无法获取它来创建新行。

下面是我现在拥有的代码。 (我遵循了 Axel Web Technologies 在 YouTube 上的教程)

function createHybridDataHandler(agent){
const{
    Email, FirstName, LastName
} = agent.parameters;

const data = [{
    Email: Email, 
    FirstName: FirstName, 
    LastName: LastName,
    Workout: Email,
    thefirstname: FirstName,
    thelastname: LastName
}];
axios.post('https://sheetdb.io/api/v1/hsb5ovk40x88i', data);
agent.add(`Done`);

}

这是我的 intentMap 代码。

intentMap.set ('CreateHybridData', createHybridDataHandler);

我在 Dialogflow 的索引和包中都设置了 axios。 (使用 axios 成功地从 Google Spreadsheet 中提取数据)

编辑:抱歉,我忘记提供我遇到的错误。您会在下面找到我在检查日志时在 Firebase 中遇到的错误。

dialogflowFirebaseFulfillment
Function execution started

> dialogflowFirebaseFulfillment
Warning, estimating Firebase Config based on GCLOUD_PROJECT. Initializing firebase-admin may fail


> dialogflowFirebaseFulfillment
Dialogflow Request headers: {"host":"us-central1-archie-personal-trainer-pueafg.cloudfunctions.net","user-agent":"Google-Dialogflow","transfer-encoding":"chunked","accept":"*/*","accept-encoding":"gzip,deflate,br","content-type":"application/json","forwarded":"for=\"66.249.84.223\";proto=https","function-execution-id":"e3y2hwjsi092","x-appengine-country":"ZZ","x-appengine-default-version-hostname":"b6b956e25660d8640p-tp.appspot.com","x-appengine-https":"on","x-appengine-request-log-id":"5ee6818900ff04307a9500ff68ef0001737e6236623935366532353636306438363430702d7470000139303866373866633530636138623435363966303164656633393333393136383a3334000100","x-appengine-user-ip":"66.249.84.223","x-cloud-trace-context":"47d4c8b63058cf02e15d32b293b42f1a/16021772533127728568;o=1","x-forwarded-for":"66.249.84.223","x-forwarded-proto":"https","connection":"close"}

dialogflowFirebaseFulfillment
Dialogflow Request body: {"responseId":"e5435eeb-249b-4f78-9a5a-0cd51b312807-2db64ae0","queryResult":{"queryText":"Jordan","parameters":{"Email":"cjgibson440@gmail.com","FirstName":"Michael","LastName":"Jordan"},"allRequiredParamsPresent":true,"outputContexts":[{"name":"projects/archie-personal-trainer-pueafg/agent/sessions/7f183f01-5ae4-830d-5db9-07d689eb2d02/contexts/__system_counters__","parameters":{"no-input":0,"no-match":0,"Email":"cjgibson440@gmail.com","Email.original":"cjgibson440@gmail.com","FirstName":"Michael","FirstName.original":"Michael","LastName":"Jordan","LastName.original":"Jordan"}}],"intent":{"name":"projects/archie-personal-trainer-pueafg/agent/intents/74086fa6-8d30-48a8-a439-397094c4ab7e","displayName":"CreateHybridData"},"intentDetectionConfidence":1,"languageCode":"en"},"originalDetectIntentRequest":{"payload":{}},"session":"projects/archie-personal-trainer-pueafg/agent/sessions/7f183f01-5ae4-830d-5db9-07d689eb2d02"}

dialogflowFirebaseFulfillment
Function execution took 2879 ms, finished with status code: 200

dialogflowFirebaseFulfillment
Unhandled rejection

dialogflowFirebaseFulfillment
Error: Request failed with status code 400 at createError (/srv/node_modules/axios/lib/core/createError.js:16:15) at settle (/srv/node_modules/axios/lib/core/settle.js:17:12) at IncomingMessage.handleStreamEnd (/srv/node_modules/axios/lib/adapters/http.js:236:11) at emitNone (events.js:111:20) at IncomingMessage.emit (events.js:208:7) at endReadableNT (_stream_readable.js:1064:12) at _combinedTickCallback (internal/process/next_tick.js:139:11) at process._tickDomainCallback (internal/process/next_tick.js:219:9)

感谢您的帮助!

错误中最重要的一点是行

Request failed with status code 400

Sheets DB status code documentation 表示错误代码 400 表示 "API could not understand the request",这表明您的数据格式不正确。

POST - create row API 端点表示它需要一个 POST 主体和一个 JSON 对象,该对象包含一个 "data" 字段和一个对象数组.尽管您发送的是对象数组,但它们不是具有数据字段的对象的一部分。

您的代码可能看起来更像:

const data = [{
    Email: Email, 
    FirstName: FirstName, 
    LastName: LastName,
    Workout: Email,
    thefirstname: FirstName,
    thelastname: LastName
}];
const body = {
  data
};
axios.post('https://sheetdb.io/api/v1/hsb5ovk40x88i', body);