【问题标题】:Error while deploying dialog flow sample code部署对话流示例代码时出错
【发布时间】:2018-11-02 00:59:29
【问题描述】:

我正在尝试部署示例天气对话流聊天机器人,

我正在按照对话流程手册中的说明进行部署

为了部署 index.js,我使用以下命令

gcloud beta functions deploy helloHttp --stage-bucket weather-example --trigger-http

部署文件后出现以下错误

ERROR: (gcloud.beta.functions.deploy) OperationError: code=3, message=Function load error: Node.js module defined by file index.js is expected to export function named helloHttp

我不知道如何解决它,我是谷歌云和对话流的新手,

这是我的 index.js 文件,我刚刚添加了我的是否 api 键

'use strict';

const http = require('http');

const host = 'api.worldweatheronline.com';
const wwoApiKey = 'MY wheather key';

exports.weatherWebhook = (req, res) => {
  // Get the city and date from the request
  let city = req.body.queryResult.parameters['geo-city']; // city is a required param

  // Get the date for the weather forecast (if present)
  let date = '';
  if (req.body.queryResult.parameters['date']) {
    date = req.body.queryResult.parameters['date'];
    console.log('Date: ' + date);
  }

  // Call the weather API
  callWeatherApi(city, date).then((output) => {
    res.json({ 'fulfillmentText': output }); // Return the results of the weather API to Dialogflow
  }).catch(() => {
    res.json({ 'fulfillmentText': `I don't know the weather but I hope it's good!` });
  });
};

function callWeatherApi (city, date) {
  return new Promise((resolve, reject) => {
    // Create the path for the HTTP request to get the weather
    let path = '/premium/v1/weather.ashx?format=json&num_of_days=1' +
      '&q=' + encodeURIComponent(city) + '&key=' + wwoApiKey + '&date=' + date;
    console.log('API Request: ' + host + path);

    // Make the HTTP request to get the weather
    http.get({host: host, path: path}, (res) => {
      let body = ''; // var to store the response chunks
      res.on('data', (d) => { body += d; }); // store each response chunk
      res.on('end', () => {
        // After all the data has been received parse the JSON for desired data
        let response = JSON.parse(body);
        let forecast = response['data']['weather'][0];
        let location = response['data']['request'][0];
        let conditions = response['data']['current_condition'][0];
        let currentConditions = conditions['weatherDesc'][0]['value'];

        // Create response
        let output = `Current conditions in the ${location['type']} 
        ${location['query']} are ${currentConditions} with a projected high of
        ${forecast['maxtempC']}°C or ${forecast['maxtempF']}°F and a low of 
        ${forecast['mintempC']}°C or ${forecast['mintempF']}°F on 
        ${forecast['date']}.`;

        // Resolve the promise with the output text
        console.log(output);
        resolve(output);
      });
      res.on('error', (error) => {
        console.log(`Error calling the weather API: ${error}`)
        reject();
      });
    });
  });
}

你能告诉我这里出了什么问题吗?

【问题讨论】:

    标签: node.js google-cloud-platform chatbot dialogflow-es


    【解决方案1】:

    各位朋友,我误用了错误的部署地址, 正确的网址如下:

    gcloud beta functions deploy helloHttp  --entry-point weatherWebhook  --stage-bucket weather-example --trigger-http
    

    【讨论】:

    • 但这是我已经包含在我的答案中,你可以看到答案的第二部分。
    【解决方案2】:

    根据您的 index.js 文件,您正在导出 weatherWebhook 命名的 javascript 函数,因此您应该输入部署函数名称的值GCloud beta 函数的deploy 命令的属性与导出的方法名称相同,因为默认情况下,当触发 Google Cloud Function 时,它会执行同名的 JavaScript 函数,或者如果找不到同名的函数,则会执行函数名为function

    对于这个你的命令,试试这个

    gcloud beta functions deploy weatherWebhook --stage-bucket weather-example --trigger-http
    

    您可以使用 --entry-point 参数告诉 gcp 哪个函数将是您的 Google Cloud 函数的入口点,如果您想要为您的部署使用不同的名称。

    gcloud beta functions deploy helloHttp  --entry-point weatherWebhook  --stage-bucket weather-example --trigger-http
    

    有关谷歌云功能deploy命令的更多信息,click here

    【讨论】:

    • 您好 Akshay,感谢您的回答,但由于我以错误的方式部署它而出现错误,正确的方法是 gcloud beta 功能 deploy weatherWebhook --stage-bucket weather-example --trigger-http
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-02
    • 2015-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-06
    相关资源
    最近更新 更多