生成动态Twiml响应并保存在Nodejs服务器

时间:2017-02-13 22:20:12

标签: javascript node.js twilio

所以我正在按照教程创建twilio的XML文档,如response, (https://www.twilio.com/blog/2013/03/introducing-the-twilio-module-for-node-js.html

但我想在我的服务器上生成该文件以便稍后访问,而不是将其作为回复发送。

类似于 - localhost / files / USERNAME / FILENAME.xml

这是我当前的代码,它将文件作为响应发送。

  var resp = new twilio.TwimlResponse();

  resp.say({voice:'woman'}, 'Thanks for calling!');

  //Render the TwiML document using "toString"
  res.writeHead(200, {
      'Content-Type':'text/xml'
  });

  res.end(resp.toString());

2 个答案:

答案 0 :(得分:0)

您可以使用内置于节点的fs库将Twillio的响应保存到本地主机。特别: https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback

将.xml文件保存到localhost后,您可以使用res.sendFile()的快速路由发送回.xml文件。 https://expressjs.com/en/api.html#res.sendFile

答案 1 :(得分:0)

Twilio开发者传道者在这里。

正如番茄指出的那样,您可以使用Node中的fs库中的构建。你可以这样做:

var resp = new twilio.TwimlResponse();

resp.say({voice:'woman'}, 'Thanks for calling!');

//Render the TwiML document using "toString"
res.writeHead(200, {
    'Content-Type':'text/xml'
});

// Save the XML to disk, then return the response to Twilio.
fs.writeFile('twilio-response.xml', resp.toString(), function(err) {
  res.end(resp.toString());
});

您可能希望为每个响应生成唯一的文件名,以便它们不会被覆盖。

如果这有帮助,Twilio会保存您发回的TwiML响应,您可以在call log中检索这些响应。

相关问题