在Google Assistant中创建表格卡

时间:2019-02-06 14:50:13

标签: dialogflow actions-on-google google-assistant-sdk inline-editing

下面是我正在运行以在Google Assistant中显示表格的完整代码。

'use strict';
const {Table} = require('actions-on-google');
process.env.DEBUG = 'dialogflow:debug'; 

exports.dialogflowFirebaseFulfillment = 
          functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });

function showTable(){
   const conv = agent.conv();
   agent.add("this is sample table");
   agent.add(new Table({
   dividers: true,
   columns: ['header 1', 'header 2', 'header 3'],
   rows: [
      ['row 1 item 1', 'row 1 item 2', 'row 1 item 3'],
      ['row 2 item 1', 'row 2 item 2', 'row 2 item 3'],
   ],
   }));
 }
 let intentMap = new Map();
 intentMap.set('TableView',showTable); //TableView is my intent name
 agent.handleRequest(intentMap);
});

运行上面的代码时,它向我显示下面的错误

TypeError: Table is not a constructor
    at showTable (/user_code/index.js:74:15)
    at WebhookClient.handleRequest (/user_code/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:303:44)
    at exports.dialogflowFirebaseFulfillment.functions.https.onRequest (/user_code/index.js:102:9)
    at cloudFunction (/user_code/node_modules/firebase-functions/lib/providers/https.js:57:9)
    at /var/tmp/worker/worker.js:735:7
    at /var/tmp/worker/worker.js:718:11
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
    at process._tickDomainCallback (internal/process/next_tick.js:128:9)

2 个答案:

答案 0 :(得分:0)

最可能的原因是您没有导入Table对象,例如

const {Table} = require('actions-on-google');

答案 1 :(得分:0)

我遇到了这个问题,一个可能的原因可能是actions-on-google文件中package.json的错误版本。通常,我们曾经将package.json从现有示例复制到我们的新项目中。因此旧版本的版本为2.0.0-alpha.4。由于表卡是在该版本之后添加的,因此对话框流将引发错误。您可以使用版本2.6.0。这对我有用。

下面是我的index.js文件。

'use strict';
const {dialogflow,Table} = require('actions-on-google');
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion,List,Image} = require('dialogflow-fulfillment');

const app= dialogflow({debug:true});

app.intent('Table View Sample',(conv) =>{
  
  conv.ask('This is a simple table example.');
conv.ask(new Table({
  dividers: true,
  columns: ['header 1', 'header 2', 'header 3'],
  rows: [
    ['row 1 item 1', 'row 1 item 2', 'row 1 item 3'],
    ['row 2 item 1', 'row 2 item 2', 'row 2 item 3'],
  ],
}));
});

exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);

下面是我的package.json文件。

{
  "name": "dialogflowFirebaseFulfillment",
  "description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase",
  "version": "0.0.1",
  "private": true,
  "license": "Apache Version 2.0",
  "author": "Google Inc.",
  "engines": {
    "node": "~6.0"
  },
  "scripts": {
    "start": "firebase serve --only functions:dialogflowFirebaseFulfillment",
    "deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment"
  },
  "dependencies": {
    "actions-on-google": "2.6.0",
    "firebase-admin": "^4.2.1",
    "firebase-functions": "^0.5.7",
    "dialogflow": "^0.1.0",
    "dialogflow-fulfillment": "0.3.0-beta.3"
  }
}

希望这对某人有帮助!

谢谢。