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



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

但我想在我的服务器上生成文件以供以后访问,而不是将其作为响应发送。

类似的东西 - 本地主机/文件/用户名/文件名

.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());
您可以使用

node 中内置的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

Twilio开发者布道者在这里。

正如 tomato 指出的那样,您可以使用 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 响应,您可以在通话记录中检索这些回复。

最新更新