簡體   English   中英

生成動態Twiml響應並保存在Nodejs服務器上

[英]generating dynamic Twiml response and save on Nodejs server

因此,我正在按照教程創建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());

您可以使用節點內置的fs庫將響應從Twillio保存到本地主機。 具體來說: https : //nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback

將.xml文件保存到localhost后,可以使用帶有發回.xml文件的res.sendFile()的快速路由。 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響應,您可以在呼叫日志中檢索這些響應。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM