【问题标题】:Read from external file into variable for this.response.speak从外部文件读入 this.response.speak 的变量
【发布时间】:2018-06-05 22:22:07
【问题描述】:

我对 Alexa 技能有以下意图,我需要将 .txt 文件从外部 URL 读取到变量中,以便 Alexa 说出它。这就是我目前所拥有的......

 'PlayVoice': function() {
    var url = "https://example.com/myfile.txt";
    var output = 'Okay, here is the text file' + url;
    this.response.speak(output);
    this.emit(':responseReady');
  },

显然,它现在唯一要做的就是读取实际的 URL。

我曾尝试使用 fs.readFile,但只是在 Alexa Skill 中遇到错误。这是我试过的代码:

  'PlayVoice': function() {
    var content;
    fs.readFile('https://example.com/myfile.txt', function read(err, data) {
    content = data;
    this.response.speak(content);
    }
    this.emit(':responseReady');
  },

关于如何简单地将文本文件读入变量的任何帮助我可以通过this.response.speak 让 Alexa 说话?

【问题讨论】:

  • 你得到什么错误?

标签: alexa alexa-skills-kit alexa-skill


【解决方案1】:

您可以使用request 包。

这样的事情应该会有所帮助。

var request = require('request');
request('url/of/the/file', function (error, response, body) {
  console.log('error:', error); // Print the error if one occurred
  console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
  console.log('body:', body); // contents of your file.
});

来源:https://www.npmjs.com/package/request#super-simple-to-use

您还需要将包 request 添加到技能的 lambda 中。 为此,请在您的代码所在的文件夹(lambda_function.js 和所有其他文件)中安装请求包。然后创建所有文件的 zip(不是文件所在的文件夹)并将其上传到您的 aws lambda。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    • 2014-06-18
    相关资源
    最近更新 更多