生成动态Twiml响应和保存的NodeJS服务器上
问题描述:
所以我下面的教程创建twilio的XML文档等为response
, (https://www.twilio.com/blog/2013/03/introducing-the-twilio-module-for-node-js.html)生成动态Twiml响应和保存的NodeJS服务器上
而不是发送它作为回应,我想在我的服务器上生成稍后访问的文件。
像 - 本地主机/文件/ 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());
答
您可以保存从Twillio到localhost使用fs
库内置到节点的响应。具体做法是: https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback
一旦你有你的.xml文件保存到本地主机,你可以使用一个明确的路线与res.sendFile()
发送回.xml
文件。 https://expressjs.com/en/api.html#res.sendFile
答
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中检索这些响应。