【问题标题】:Dialogflow webhook fulfillment parameter not accessible无法访问 Dialogflow Webhook 实现参数
【发布时间】:2020-04-14 02:23:46
【问题描述】:

我想在我的 webhook 实现中输入一个参数。

这是我的代码:

const bodyParser = require('body-parser')
var request = require('request-promise-native');
const { dialogflow } = require('actions-on-google');
const assistant = dialogflow({
  clientId: "30xxxxx08407-rv9kxxxxxxxxuuq8f9ul2eg.apps.googleusercontent.com"
});


module.exports = (app) => {
  const logger = console;

assistant.intent('Sales', conv => {
 const pcode = agent.parameters['PCODE'];
 console.log(pcode)

    const token = '3369708919812376';
    const serviceID = '502';
    const P_STATE_CD = 'ALL';
    const P_FO_CD = 'ALL';
    const P_DISTT_CD = 'ALL';
    const P_DATE = '16/12/2019';
    const P_PRD_GROUP = 'UREA';
    const P_PERSONAL_NO = '106296';

        var data = {"token" : token,"serviceID" : serviceID,"P_STATE_CD" : P_STATE_CD,"P_FO_CD" : P_FO_CD,"P_DISTT_CD" : P_DISTT_CD,"P_DATE" : P_DATE,"P_PRD_GROUP" : P_PRD_GROUP,"P_PERSONAL_NO" : P_PERSONAL_NO };
        var sdata = JSON.stringify(data);

                    const options = {
                        method: 'POST',
                        uri: 'http://Webservice/resources/webservice/service' ,
                        body: JSON.parse(sdata) ,
                        json: true
                    }
        return request(options)
            .then( body => {
                 var unit = body
                 console.log(body)
                 unit.intent = "Sales"
                 unit.value1 = unit.saleInfo[0].QMTD
                 unit.value2 = unit.saleInfo[0].QYTD
                 unit.value3 = unit.saleInfo[0].O_UOM
                 unit.value4 = null
                 unit.value5 = null

                 delete unit.saleInfo
                 var unit2 = JSON.stringify(unit)
                console.log(unit2)
          conv.ask(unit2);
              })
              .catch( err => {
               console.error( err );
               conv.ask('Something went wrong. What should I do now?');
                 });
  })

我尝试使用const pcode = agent.parameters.PCODE,但它不起作用。给我错误:

ReferenceError: 代理未定义 在 assistant.intent.conv (/home/dbalounge/GoogleDF/service.js:15:16) 在功能。 (/home/dbalounge/GoogleDF/node_modules/actions-on-google/dist/service/dialogflow/dialogflow.js:151:27) 在 Generator.next () 在 /home/dbalounge/GoogleDF/node_modules/actions-on-google/dist/service/dialogflow/dialogflow.js:22:71 在新的承诺 () 在 __awaiter (/home/dbalounge/GoogleDF/node_modules/actions-on-google/dist/service/dialogflow/dialogflow.js:18:12) 在 Function.handler (/home/dbalounge/GoogleDF/node_modules/actions-on-google/dist/service/dialogflow/dialogflow.js:85:16) 在对象。 (/home/dbalounge/GoogleDF/node_modules/actions-on-google/dist/assistant.js:55:32) 在 Generator.next () 在 /home/dbalounge/GoogleDF/node_modules/actions-on-google/dist/assistant.js:22:71

【问题讨论】:

    标签: node.js dialogflow-es google-api-nodejs-client


    【解决方案1】:

    agent 未在您的代码中的任何位置定义,这就是您得到的原因:

    ReferenceError: agent is not defined
    

    在任何情况下,如果您使用assistant.parameters 也将不起作用。 Dialogflow Intent 参数可以通过.intent 回调的第二个参数访问。

    assistant.intent('Sales', (conv, params) => {
         const pcode = params.PCODE;
         /* ... */
    })
    

    更多信息您可以查看docs

    【讨论】:

    • 您好先生,它可以工作,但是当我尝试将参数放入 apis var data = {"token" : pcode}; var sdata = JSON.stringify(data); const options = { method: 'POST', uri: 'Webservice/resources/webservice/service' , body: JSON.parse(sdata) , json: true } 比它给出这样的错误 TypeError: Cannot read property '0' of undefined 。 @marcos casagrande
    • 请为您的新问题创建一个新问题,我很乐意为您提供帮助。把链接贴在这里,我去看看
    • 你好,这是链接,请帮我解决这个问题link@marcos
    • 抱歉,但请相信我这是错误的...我感谢您的工作...感谢您的宝贵回复@marcos
    猜你喜欢
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-02
    • 1970-01-01
    相关资源
    最近更新 更多